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

119 lines
3.1 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-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
@StateObject private var viewModel = TimelineViewModel()
@Binding var timeline: TimelineFilter
2022-11-21 08:31:32 +00:00
public init(timeline: Binding<TimelineFilter>) {
_timeline = timeline
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 {
LazyVStack {
tagHeaderView
.padding(.bottom, 16)
2022-12-27 06:51:44 +00:00
.id(Constants.scrollToTop)
2022-12-25 17:43:15 +00:00
StatusesListView(fetcher: viewModel)
}
.padding(.top, DS.Constants.layoutPadding)
}
if viewModel.pendingStatusesEnabled {
2022-12-25 17:43:15 +00:00
makePendingNewPostsView(proxy: proxy)
}
2022-11-25 11:00:01 +00:00
}
2022-11-21 08:31:32 +00:00
}
.navigationTitle(timeline.title())
.navigationBarTitleDisplayMode(.inline)
2022-12-20 14:37:51 +00:00
.onAppear {
2022-11-29 11:18:06 +00:00
viewModel.client = client
viewModel.timeline = timeline
}
.refreshable {
await viewModel.fetchStatuses(userIntent: true)
}
.onChange(of: watcher.latestEvent?.id) { 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
viewModel.timeline = timeline
}
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 {
Button {
2022-12-27 06:51:44 +00:00
proxy.scrollTo(Constants.scrollToTop)
2022-12-27 08:11:12 +00:00
withAnimation {
viewModel.displayPendingStatuses()
}
2022-12-25 17:43:15 +00:00
} label: {
Text(viewModel.pendingStatusesButtonTitle)
2022-12-25 17:43:15 +00:00
}
.buttonStyle(.bordered)
.background(.thinMaterial)
.cornerRadius(8)
.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 {
await viewModel.unfollowTag(id: tag.name)
} else {
await viewModel.followTag(id: tag.name)
}
}
} label: {
Text(tag.following ? "Following": "Follow")
}.buttonStyle(.bordered)
}
.padding(.horizontal, DS.Constants.layoutPadding)
.padding(.vertical, 8)
.background(.gray.opacity(0.15))
}
}
2022-11-21 08:31:32 +00:00
}