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

207 lines
6.2 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import DesignSystem
import Env
2023-02-04 16:17:38 +00:00
import Introspect
2022-12-17 12:37:46 +00:00
import Models
2023-01-17 10:36:01 +00:00
import Network
2022-12-17 12:37:46 +00:00
import Shimmer
2022-12-18 19:30:19 +00:00
import Status
2023-01-17 10:36:01 +00:00
import SwiftUI
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"
}
2023-01-17 10:36:01 +00:00
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
2023-01-17 10:36:01 +00:00
2022-11-29 11:18:06 +00:00
@StateObject private var viewModel = TimelineViewModel()
2023-02-01 11:49:59 +00:00
@State private var wasBackgrounded: Bool = false
@State private var collectionView: UICollectionView?
2023-02-01 11:49:59 +00:00
@Binding var timeline: TimelineFilter
2022-12-31 11:28:27 +00:00
@Binding var scrollToTopSignal: Int
2023-01-17 10:36:01 +00:00
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
}
2023-01-17 10:36:01 +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) {
List {
if viewModel.tag == nil {
scrollToTopView
} else {
2022-12-25 17:43:15 +00:00
tagHeaderView
}
switch viewModel.timeline {
case .remoteLocal:
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath, isRemote: true)
default:
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath)
}
2022-12-25 17:43:15 +00:00
}
.id(client.id)
.environment(\.defaultMinListRowHeight, 1)
.listStyle(.plain)
.scrollContentBackground(.hidden)
2022-12-29 09:39:34 +00:00
.background(theme.primaryBackgroundColor)
.introspect(selector: TargetViewSelector.ancestorOrSiblingContaining,
customize: { (collectionView: UICollectionView) in
2023-02-04 16:17:38 +00:00
self.collectionView = collectionView
})
if viewModel.pendingStatusesEnabled {
PendingStatusesObserverView(observer: viewModel.pendingStatusesObserver)
2022-12-25 17:43:15 +00:00
}
2022-11-25 11:00:01 +00:00
}
.onChange(of: viewModel.scrollToIndex) { index in
2023-02-05 07:13:38 +00:00
if let collectionView,
let index,
let rows = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: 0),
2023-02-12 15:29:41 +00:00
rows > index
{
2023-02-05 07:13:38 +00:00
collectionView.scrollToItem(at: .init(row: index, section: 0),
at: .top,
animated: viewModel.scrollToIndexAnimated)
viewModel.scrollToIndexAnimated = false
viewModel.scrollToIndex = nil
}
2022-12-31 11:28:27 +00:00
}
.onChange(of: scrollToTopSignal, perform: { _ in
withAnimation {
proxy.scrollTo(Constants.scrollToTop, anchor: .top)
}
})
2022-11-21 08:31:32 +00:00
}
2023-02-06 11:24:48 +00:00
.toolbar {
ToolbarItem(placement: .principal) {
VStack(alignment: .center) {
switch timeline {
case let .remoteLocal(_, filter):
Text(filter.localizedTitle())
.font(.headline)
Text(timeline.localizedTitle())
.font(.caption)
.foregroundColor(.gray)
default:
Text(timeline.localizedTitle())
.font(.headline)
}
}
}
}
.navigationBarTitleDisplayMode(.inline)
2022-12-20 14:37:51 +00:00
.onAppear {
viewModel.isTimelineVisible = true
if viewModel.client == nil {
viewModel.client = client
viewModel.timeline = timeline
2023-01-31 07:04:35 +00:00
} else {
Task {
await viewModel.fetchStatuses()
2023-01-31 07:04:35 +00:00
}
}
}
.onDisappear {
viewModel.isTimelineVisible = false
}
.refreshable {
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
await viewModel.fetchStatuses()
HapticManager.shared.fireHaptic(of: .dataRefresh(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)
}
}
.onChange(of: timeline) { newTimeline in
switch newTimeline {
2023-02-06 11:24:48 +00:00
case let .remoteLocal(server, _):
viewModel.client = Client(server: server)
default:
viewModel.client = client
}
viewModel.timeline = newTimeline
}
.onChange(of: viewModel.timeline, perform: { newValue in
timeline = newValue
})
2022-12-26 07:24:55 +00:00
.onChange(of: scenePhase, perform: { scenePhase in
switch scenePhase {
case .active:
if wasBackgrounded {
wasBackgrounded = false
Task {
await viewModel.fetchStatuses()
}
2022-12-26 07:24:55 +00:00
}
case .background:
wasBackgrounded = true
2023-02-01 11:49:59 +00:00
2022-12-26 07:24:55 +00:00
default:
break
}
})
}
2023-01-17 10:36:01 +00:00
2022-12-21 11:39:29 +00:00
@ViewBuilder
private var tagHeaderView: some View {
if let tag = viewModel.tag {
VStack(alignment: .leading) {
2022-12-21 11:39:29 +00:00
Spacer()
HStack {
VStack(alignment: .leading, spacing: 4) {
Text("#\(tag.name)")
.font(.scaledHeadline)
Text("timeline.n-recent-from-n-participants \(tag.totalUses) \(tag.totalAccounts)")
.font(.scaledFootnote)
.foregroundColor(.gray)
2022-12-21 11:39:29 +00:00
}
Spacer()
Button {
Task {
if tag.following {
viewModel.tag = await account.unfollowTag(id: tag.name)
} else {
viewModel.tag = await account.followTag(id: tag.name)
}
}
} label: {
Text(tag.following ? "account.follow.following" : "account.follow.follow")
}.buttonStyle(.bordered)
}
Spacer()
2022-12-21 11:39:29 +00:00
}
.listRowBackground(theme.secondaryBackgroundColor)
.listRowSeparator(.hidden)
.listRowInsets(.init(top: 8,
leading: .layoutPadding,
bottom: 8,
trailing: .layoutPadding))
2022-12-21 11:39:29 +00:00
}
}
2023-02-01 11:49:59 +00:00
private var scrollToTopView: some View {
2023-02-01 11:49:59 +00:00
HStack { EmptyView() }
.listRowBackground(theme.primaryBackgroundColor)
.listRowSeparator(.hidden)
.listRowInsets(.init())
.frame(height: .layoutPadding)
.id(Constants.scrollToTop)
.onAppear {
viewModel.scrollToTopVisible = true
}
.onDisappear {
viewModel.scrollToTopVisible = false
}
}
2022-11-21 08:31:32 +00:00
}