IceCubesApp/Packages/Timeline/Sources/Timeline/TimelineView.swift

168 lines
4.7 KiB
Swift
Raw Normal View History

2022-11-21 08:31:32 +00:00
import SwiftUI
import Network
2022-12-17 12:37:46 +00:00
import Models
import Shimmer
2022-12-18 19:30:19 +00:00
import Status
2022-12-19 11:28:55 +00:00
import DesignSystem
import Env
2022-11-21 08:31:32 +00:00
public struct TimelineView: View {
2022-12-27 06:51:44 +00:00
private enum Constants {
static let scrollToTop = "top"
}
2022-12-26 07:24:55 +00:00
@Environment(\.scenePhase) private var scenePhase
2022-12-29 09:39:34 +00:00
@EnvironmentObject private var theme: Theme
2022-12-25 18:18:19 +00:00
@EnvironmentObject private var account: CurrentAccount
@EnvironmentObject private var watcher: StreamWatcher
2022-11-29 11:18:06 +00:00
@EnvironmentObject private var client: Client
@EnvironmentObject private var routerPath: RouterPath
2022-12-31 11:28:27 +00:00
2022-11-29 11:18:06 +00:00
@StateObject private var viewModel = TimelineViewModel()
2022-12-31 11:28:27 +00:00
@State private var scrollProxy: ScrollViewProxy?
@Binding var timeline: TimelineFilter
2022-12-31 11:28:27 +00:00
@Binding var scrollToTopSignal: Int
2022-11-21 08:31:32 +00:00
private let feedbackGenerator = UIImpactFeedbackGenerator()
2022-12-31 11:28:27 +00:00
public init(timeline: Binding<TimelineFilter>, scrollToTopSignal: Binding<Int>) {
_timeline = timeline
2022-12-31 11:28:27 +00:00
_scrollToTopSignal = scrollToTopSignal
2022-12-20 14:37:51 +00:00
}
2022-11-21 08:31:32 +00:00
public var body: some View {
2022-12-25 17:43:15 +00:00
ScrollViewReader { proxy in
ZStack(alignment: .top) {
ScrollView {
2022-12-31 14:41:44 +00:00
Rectangle()
.frame(height: 0)
.id(Constants.scrollToTop)
2022-12-25 17:43:15 +00:00
LazyVStack {
tagHeaderView
.padding(.bottom, 16)
switch viewModel.timeline {
case .remoteLocal:
StatusesListView(fetcher: viewModel, isRemote: true)
default:
StatusesListView(fetcher: viewModel)
}
2022-12-25 17:43:15 +00:00
}
.padding(.top, .layoutPadding + (!viewModel.pendingStatuses.isEmpty ? 28 : 0))
2022-12-25 17:43:15 +00:00
}
2022-12-29 09:39:34 +00:00
.background(theme.primaryBackgroundColor)
if viewModel.pendingStatusesEnabled {
2022-12-25 17:43:15 +00:00
makePendingNewPostsView(proxy: proxy)
}
2022-11-25 11:00:01 +00:00
}
2022-12-31 11:28:27 +00:00
.onAppear {
scrollProxy = proxy
}
2022-11-21 08:31:32 +00:00
}
.navigationTitle(timeline.title())
.navigationBarTitleDisplayMode(.inline)
2022-12-20 14:37:51 +00:00
.onAppear {
if viewModel.client == nil {
viewModel.client = client
viewModel.timeline = timeline
}
}
.refreshable {
feedbackGenerator.impactOccurred(intensity: 0.3)
await viewModel.fetchStatuses(userIntent: true)
feedbackGenerator.impactOccurred(intensity: 0.7)
}
.onChange(of: watcher.latestEvent?.id) { _ in
if let latestEvent = watcher.latestEvent {
2022-12-25 18:18:19 +00:00
viewModel.handleEvent(event: latestEvent, currentAccount: account)
}
}
2022-12-31 11:28:27 +00:00
.onChange(of: scrollToTopSignal, perform: { _ in
withAnimation {
scrollProxy?.scrollTo(Constants.scrollToTop, anchor: .top)
}
})
.onChange(of: timeline) { newTimeline in
switch newTimeline {
case let .remoteLocal(server):
viewModel.client = Client(server: server)
default:
viewModel.client = client
}
viewModel.timeline = newTimeline
}
2022-12-26 07:24:55 +00:00
.onChange(of: scenePhase, perform: { scenePhase in
switch scenePhase {
case .active:
Task {
await viewModel.fetchStatuses(userIntent: false)
}
default:
break
}
})
}
2022-12-25 17:43:15 +00:00
@ViewBuilder
private func makePendingNewPostsView(proxy: ScrollViewProxy) -> some View {
if !viewModel.pendingStatuses.isEmpty {
2023-01-05 06:07:28 +00:00
HStack(spacing: 6) {
Button {
withAnimation {
proxy.scrollTo(Constants.scrollToTop)
viewModel.displayPendingStatuses()
}
} label: {
Text(viewModel.pendingStatusesButtonTitle)
}
.buttonStyle(.bordered)
.background(.thinMaterial)
.cornerRadius(8)
if viewModel.pendingStatuses.count > 1 {
Button {
withAnimation {
viewModel.dequeuePendingStatuses()
}
} label: {
2023-01-07 12:44:13 +00:00
Image(systemName: "text.insert")
2023-01-05 06:07:28 +00:00
}
.buttonStyle(.bordered)
.background(.thinMaterial)
.cornerRadius(8)
2022-12-27 08:11:12 +00:00
}
2022-12-25 17:43:15 +00:00
}
.padding(.top, 6)
}
}
2022-12-21 11:39:29 +00:00
@ViewBuilder
private var tagHeaderView: some View {
if let tag = viewModel.tag {
HStack {
VStack(alignment: .leading, spacing: 4) {
Text("#\(tag.name)")
.font(.headline)
2022-12-24 12:41:25 +00:00
Text("\(tag.totalUses) recent posts from \(tag.totalAccounts) participants")
2022-12-21 11:39:29 +00:00
.font(.footnote)
.foregroundColor(.gray)
}
Spacer()
Button {
Task {
if tag.following {
2023-01-04 17:37:58 +00:00
viewModel.tag = await account.unfollowTag(id: tag.name)
2022-12-21 11:39:29 +00:00
} else {
2023-01-04 17:37:58 +00:00
viewModel.tag = await account.followTag(id: tag.name)
2022-12-21 11:39:29 +00:00
}
}
} label: {
Text(tag.following ? "Following": "Follow")
}.buttonStyle(.bordered)
}
.padding(.horizontal, .layoutPadding)
2022-12-21 11:39:29 +00:00
.padding(.vertical, 8)
2022-12-29 09:39:34 +00:00
.background(theme.secondaryBackgroundColor)
2022-12-21 11:39:29 +00:00
}
}
2022-11-21 08:31:32 +00:00
}