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
|
2022-12-25 11:46:42 +00:00
|
|
|
@EnvironmentObject private var watcher: StreamWatcher
|
2022-11-29 11:18:06 +00:00
|
|
|
@EnvironmentObject private var client: Client
|
2023-01-02 18:23:44 +00:00
|
|
|
@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
|
|
|
|
2023-01-31 11:17:03 +00:00
|
|
|
@State private var wasBackgrounded: Bool = false
|
2023-02-03 15:24:09 +00:00
|
|
|
@State private var collectionView: UICollectionView?
|
2023-02-01 11:49:59 +00:00
|
|
|
|
2022-12-26 06:36:54 +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>) {
|
2022-12-26 06:36:54 +00:00
|
|
|
_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) {
|
2023-01-30 20:41:42 +00:00
|
|
|
List {
|
|
|
|
if viewModel.tag == nil {
|
|
|
|
scrollToTopView
|
|
|
|
} else {
|
2022-12-25 17:43:15 +00:00
|
|
|
tagHeaderView
|
|
|
|
}
|
2023-01-30 20:41:42 +00:00
|
|
|
switch viewModel.timeline {
|
|
|
|
case .remoteLocal:
|
2023-02-15 07:46:14 +00:00
|
|
|
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath, isRemote: true)
|
2023-01-30 20:41:42 +00:00
|
|
|
default:
|
2023-02-15 07:46:14 +00:00
|
|
|
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath)
|
2023-01-30 20:41:42 +00:00
|
|
|
}
|
2022-12-25 17:43:15 +00:00
|
|
|
}
|
2023-02-03 15:24:09 +00:00
|
|
|
.id(client.id)
|
2023-01-30 20:41:42 +00:00
|
|
|
.environment(\.defaultMinListRowHeight, 1)
|
|
|
|
.listStyle(.plain)
|
|
|
|
.scrollContentBackground(.hidden)
|
2022-12-29 09:39:34 +00:00
|
|
|
.background(theme.primaryBackgroundColor)
|
2023-02-03 15:24:09 +00:00
|
|
|
.introspect(selector: TargetViewSelector.ancestorOrSiblingContaining,
|
|
|
|
customize: { (collectionView: UICollectionView) in
|
2023-02-04 16:17:38 +00:00
|
|
|
self.collectionView = collectionView
|
|
|
|
})
|
2022-12-29 05:55:53 +00:00
|
|
|
if viewModel.pendingStatusesEnabled {
|
2023-02-01 11:43:11 +00:00
|
|
|
PendingStatusesObserverView(observer: viewModel.pendingStatusesObserver)
|
2022-12-25 17:43:15 +00:00
|
|
|
}
|
2022-11-25 11:00:01 +00:00
|
|
|
}
|
2023-02-03 15:24:09 +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)
|
2023-02-04 13:05:30 +00:00
|
|
|
viewModel.scrollToIndexAnimated = false
|
|
|
|
viewModel.scrollToIndex = nil
|
2023-02-01 11:43:11 +00:00
|
|
|
}
|
2022-12-31 11:28:27 +00:00
|
|
|
}
|
2023-02-01 11:43:11 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 12:52:13 +00:00
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
2022-12-20 14:37:51 +00:00
|
|
|
.onAppear {
|
2023-02-04 15:54:03 +00:00
|
|
|
viewModel.isTimelineVisible = true
|
2023-01-06 11:14:05 +00:00
|
|
|
if viewModel.client == nil {
|
|
|
|
viewModel.client = client
|
|
|
|
viewModel.timeline = timeline
|
2023-01-31 07:04:35 +00:00
|
|
|
} else {
|
|
|
|
Task {
|
2023-01-31 16:43:06 +00:00
|
|
|
await viewModel.fetchStatuses()
|
2023-01-31 07:04:35 +00:00
|
|
|
}
|
2023-01-06 11:14:05 +00:00
|
|
|
}
|
2022-11-21 12:52:13 +00:00
|
|
|
}
|
2023-02-04 15:54:03 +00:00
|
|
|
.onDisappear {
|
|
|
|
viewModel.isTimelineVisible = false
|
|
|
|
}
|
2022-11-21 12:52:13 +00:00
|
|
|
.refreshable {
|
2023-02-06 16:53:27 +00:00
|
|
|
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
|
2023-01-31 16:43:06 +00:00
|
|
|
await viewModel.fetchStatuses()
|
2023-02-06 16:53:27 +00:00
|
|
|
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.7))
|
2022-11-21 12:52:13 +00:00
|
|
|
}
|
2023-01-06 11:14:05 +00:00
|
|
|
.onChange(of: watcher.latestEvent?.id) { _ in
|
2022-12-25 11:46:42 +00:00
|
|
|
if let latestEvent = watcher.latestEvent {
|
2022-12-25 18:18:19 +00:00
|
|
|
viewModel.handleEvent(event: latestEvent, currentAccount: account)
|
2022-12-25 11:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-26 06:36:54 +00:00
|
|
|
.onChange(of: timeline) { newTimeline in
|
2023-01-06 11:14:05 +00:00
|
|
|
switch newTimeline {
|
2023-02-06 11:24:48 +00:00
|
|
|
case let .remoteLocal(server, _):
|
2023-01-06 11:14:05 +00:00
|
|
|
viewModel.client = Client(server: server)
|
|
|
|
default:
|
|
|
|
viewModel.client = client
|
|
|
|
}
|
|
|
|
viewModel.timeline = newTimeline
|
2022-12-26 06:36:54 +00:00
|
|
|
}
|
2023-02-04 20:21:36 +00:00
|
|
|
.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:
|
2023-01-31 11:17:03 +00:00
|
|
|
if wasBackgrounded {
|
|
|
|
wasBackgrounded = false
|
|
|
|
Task {
|
2023-01-31 16:43:06 +00:00
|
|
|
await viewModel.fetchStatuses()
|
2023-01-31 11:17:03 +00:00
|
|
|
}
|
2022-12-26 07:24:55 +00:00
|
|
|
}
|
2023-01-31 11:17:03 +00:00
|
|
|
case .background:
|
|
|
|
wasBackgrounded = true
|
2023-02-01 11:49:59 +00:00
|
|
|
|
2022-12-26 07:24:55 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2022-11-21 12:52:13 +00:00
|
|
|
}
|
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 {
|
2023-01-30 20:41:42 +00:00
|
|
|
VStack(alignment: .leading) {
|
2022-12-21 11:39:29 +00:00
|
|
|
Spacer()
|
2023-01-30 20:41:42 +00:00
|
|
|
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
|
|
|
}
|
2023-01-30 20:41:42 +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
|
|
|
}
|
2023-01-30 20:41:42 +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
|
|
|
|
2023-01-30 20:41:42 +00:00
|
|
|
private var scrollToTopView: some View {
|
2023-02-01 11:49:59 +00:00
|
|
|
HStack { EmptyView() }
|
2023-01-30 20:41:42 +00:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
.listRowSeparator(.hidden)
|
|
|
|
.listRowInsets(.init())
|
|
|
|
.frame(height: .layoutPadding)
|
|
|
|
.id(Constants.scrollToTop)
|
2023-02-01 11:43:11 +00:00
|
|
|
.onAppear {
|
|
|
|
viewModel.scrollToTopVisible = true
|
|
|
|
}
|
|
|
|
.onDisappear {
|
|
|
|
viewModel.scrollToTopVisible = false
|
|
|
|
}
|
2023-01-30 20:41:42 +00:00
|
|
|
}
|
2022-11-21 08:31:32 +00:00
|
|
|
}
|