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
|
2022-12-25 11:46:42 +00:00
|
|
|
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
|
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
|
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?
|
2022-12-26 06:36:54 +00:00
|
|
|
@Binding var timeline: TimelineFilter
|
2022-12-31 11:28:27 +00:00
|
|
|
@Binding var scrollToTopSignal: Int
|
2022-11-21 08:31:32 +00:00
|
|
|
|
2022-12-29 08:10:59 +00:00
|
|
|
private let feedbackGenerator = UIImpactFeedbackGenerator()
|
|
|
|
|
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
|
|
|
}
|
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)
|
2023-01-06 11:14:05 +00:00
|
|
|
switch viewModel.timeline {
|
|
|
|
case .remoteLocal:
|
|
|
|
StatusesListView(fetcher: viewModel, isRemote: true)
|
|
|
|
default:
|
|
|
|
StatusesListView(fetcher: viewModel)
|
|
|
|
}
|
2022-12-25 17:43:15 +00:00
|
|
|
}
|
2023-01-10 13:44:34 +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)
|
2022-12-29 05:55:53 +00:00
|
|
|
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
|
|
|
}
|
2022-12-26 06:36:54 +00:00
|
|
|
.navigationTitle(timeline.title())
|
2022-11-21 12:52:13 +00:00
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
2022-12-20 14:37:51 +00:00
|
|
|
.onAppear {
|
2023-01-06 11:14:05 +00:00
|
|
|
if viewModel.client == nil {
|
|
|
|
viewModel.client = client
|
|
|
|
viewModel.timeline = timeline
|
|
|
|
}
|
2022-11-21 12:52:13 +00:00
|
|
|
}
|
|
|
|
.refreshable {
|
2022-12-29 08:10:59 +00:00
|
|
|
feedbackGenerator.impactOccurred(intensity: 0.3)
|
2022-12-28 18:10:13 +00:00
|
|
|
await viewModel.fetchStatuses(userIntent: true)
|
2022-12-29 08:10:59 +00:00
|
|
|
feedbackGenerator.impactOccurred(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-31 11:28:27 +00:00
|
|
|
.onChange(of: scrollToTopSignal, perform: { _ in
|
|
|
|
withAnimation {
|
|
|
|
scrollProxy?.scrollTo(Constants.scrollToTop, anchor: .top)
|
|
|
|
}
|
|
|
|
})
|
2022-12-26 06:36:54 +00:00
|
|
|
.onChange(of: timeline) { newTimeline in
|
2023-01-06 11:14:05 +00:00
|
|
|
switch newTimeline {
|
|
|
|
case let .remoteLocal(server):
|
|
|
|
viewModel.client = Client(server: server)
|
|
|
|
default:
|
|
|
|
viewModel.client = client
|
|
|
|
}
|
|
|
|
viewModel.timeline = newTimeline
|
2022-12-26 06:36:54 +00:00
|
|
|
}
|
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-11-21 12:52:13 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2023-01-03 06:41:29 +00:00
|
|
|
.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
|
|
|
}
|