IceCubesApp/IceCubesApp/App/Tabs/Timeline/TimelineTab.swift

285 lines
8.1 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import AppAccount
2022-12-24 10:50:05 +00:00
import Combine
import DesignSystem
2023-01-17 10:36:01 +00:00
import Env
import Models
2023-01-17 10:36:01 +00:00
import Network
import SwiftData
2023-01-17 10:36:01 +00:00
import SwiftUI
import Timeline
2022-11-29 10:46:02 +00:00
2023-09-19 07:18:20 +00:00
@MainActor
2023-01-22 05:38:30 +00:00
struct TimelineTab: View {
2023-09-22 10:49:25 +00:00
@Environment(\.modelContext) private var context
@Environment(AppAccountsManager.self) private var appAccount
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
@Environment(CurrentAccount.self) private var currentAccount
2023-09-19 07:18:20 +00:00
@Environment(UserPreferences.self) private var preferences
@Environment(Client.self) private var client
@State private var routerPath = RouterPath()
2022-12-27 08:25:26 +00:00
@Binding var popToRootTab: Tab
2023-01-17 10:36:01 +00:00
@State private var didAppear: Bool = false
2023-09-22 10:49:25 +00:00
@State private var timeline: TimelineFilter = .home
2023-09-22 17:33:53 +00:00
@State private var selectedTagGroup: TagGroup?
2022-12-31 11:28:27 +00:00
@State private var scrollToTopSignal: Int = 0
2023-09-22 10:49:25 +00:00
@Query(sort: \LocalTimeline.creationDate, order: .reverse) var localTimelines: [LocalTimeline]
2023-09-22 17:33:53 +00:00
@Query(sort: \TagGroup.creationDate, order: .reverse) var tagGroups: [TagGroup]
2023-09-22 17:33:53 +00:00
@AppStorage("last_timeline_filter") var lastTimelineFilter: TimelineFilter = .home
2023-01-17 10:36:01 +00:00
2023-01-16 13:40:23 +00:00
private let canFilterTimeline: Bool
2023-01-17 10:36:01 +00:00
2023-01-16 13:40:23 +00:00
init(popToRootTab: Binding<Tab>, timeline: TimelineFilter? = nil) {
canFilterTimeline = timeline == nil
_popToRootTab = popToRootTab
2023-09-25 12:12:35 +00:00
_timeline = .init(initialValue: timeline ?? .home)
2023-01-16 13:40:23 +00:00
}
2023-01-17 10:36:01 +00:00
2022-11-29 10:46:02 +00:00
var body: some View {
NavigationStack(path: $routerPath.path) {
2023-09-22 17:33:53 +00:00
TimelineView(timeline: $timeline,
selectedTagGroup: $selectedTagGroup,
scrollToTopSignal: $scrollToTopSignal,
canFilterTimeline: canFilterTimeline)
.withAppRouter()
.withSheetDestinations(sheetDestinations: $routerPath.presentedSheet)
.toolbar {
toolbarView
}
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .navigationBar)
.id(client.id)
2022-11-29 10:46:02 +00:00
}
.onAppear {
routerPath.client = client
2023-09-16 12:15:03 +00:00
if !didAppear, canFilterTimeline {
didAppear = true
2023-02-12 15:29:41 +00:00
if client.isAuth {
timeline = lastTimelineFilter
} else {
2023-02-12 15:29:41 +00:00
timeline = .federated
}
}
Task {
await currentAccount.fetchLists()
}
if !client.isAuth {
routerPath.presentedSheet = .addAccount
2023-01-12 05:30:43 +00:00
}
}
.onChange(of: client.isAuth) {
2023-09-19 16:33:13 +00:00
resetTimelineFilter()
}
.onChange(of: currentAccount.account?.id) {
2023-09-19 16:33:13 +00:00
resetTimelineFilter()
}
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .timeline {
if routerPath.path.isEmpty {
2022-12-31 11:28:27 +00:00
scrollToTopSignal += 1
} else {
routerPath.path = []
2022-12-31 11:28:27 +00:00
}
2022-12-24 10:50:05 +00:00
}
}
.onChange(of: client.id) {
routerPath.path = []
}
.onChange(of: timeline) { _, newValue in
if client.isAuth, canFilterTimeline {
lastTimelineFilter = newValue
}
2023-09-22 20:41:06 +00:00
switch newValue {
case .tagGroup:
break
default:
selectedTagGroup = nil
}
}
2023-10-26 04:23:00 +00:00
.onReceive(NotificationCenter.default.publisher(for: .refreshTimeline)) { _ in
timeline = .latest
}
.onReceive(NotificationCenter.default.publisher(for: .trendingTimeline)) { _ in
timeline = .trending
}
.onReceive(NotificationCenter.default.publisher(for: .localTimeline)) { _ in
timeline = .local
}
.onReceive(NotificationCenter.default.publisher(for: .federatedTimeline)) { _ in
timeline = .federated
}
.onReceive(NotificationCenter.default.publisher(for: .homeTimeline)) { _ in
timeline = .home
}
.withSafariRouter()
.environment(routerPath)
2022-11-29 10:46:02 +00:00
}
2023-01-17 10:36:01 +00:00
@ViewBuilder
private var timelineFilterButton: some View {
if timeline.supportNewestPagination {
Button {
2023-09-16 12:15:03 +00:00
timeline = .latest
} label: {
Label(TimelineFilter.latest.localizedTitle(), systemImage: TimelineFilter.latest.iconName() ?? "")
}
if timeline == .home {
Button {
timeline = .resume
} label: {
VStack {
Label(TimelineFilter.resume.localizedTitle(),
systemImage: TimelineFilter.resume.iconName() ?? "")
}
}
}
Divider()
}
2023-01-01 17:31:23 +00:00
ForEach(TimelineFilter.availableTimeline(client: client), id: \.self) { timeline in
Button {
self.timeline = timeline
} label: {
Label(timeline.localizedTitle(), systemImage: timeline.iconName() ?? "")
}
}
if !currentAccount.lists.isEmpty {
Menu("timeline.filter.lists") {
ForEach(currentAccount.sortedLists) { list in
Button {
timeline = .list(list: list)
} label: {
Label(list.title, systemImage: "list.bullet")
}
}
2023-11-28 08:18:52 +00:00
Button {
routerPath.presentedSheet = .listCreate
} label: {
Label("account.list.create", systemImage: "plus")
}
}
}
2023-01-17 10:36:01 +00:00
2023-01-04 17:37:58 +00:00
if !currentAccount.tags.isEmpty {
Menu("timeline.filter.tags") {
ForEach(currentAccount.sortedTags) { tag in
2023-01-04 17:37:58 +00:00
Button {
timeline = .hashtag(tag: tag.name, accountId: nil)
} label: {
Label("#\(tag.name)", systemImage: "number")
}
}
}
}
2023-01-22 05:38:30 +00:00
Menu("timeline.filter.local") {
2023-09-22 10:49:25 +00:00
ForEach(localTimelines) { remoteLocal in
2023-01-06 16:14:34 +00:00
Button {
2023-09-22 10:49:25 +00:00
timeline = .remoteLocal(server: remoteLocal.instance, filter: .local)
2023-01-06 16:14:34 +00:00
} label: {
2023-02-06 11:24:48 +00:00
VStack {
2023-09-22 10:49:25 +00:00
Label(remoteLocal.instance, systemImage: "dot.radiowaves.right")
2023-02-06 11:24:48 +00:00
}
}
}
2023-01-06 16:14:34 +00:00
Button {
routerPath.presentedSheet = .addRemoteLocalTimeline
2023-01-06 16:14:34 +00:00
} label: {
Label("timeline.filter.add-local", systemImage: "badge.plus.radiowaves.right")
2023-01-06 16:14:34 +00:00
}
}
2023-07-19 05:46:25 +00:00
Menu("timeline.filter.tag-groups") {
2023-09-22 17:33:53 +00:00
ForEach(tagGroups) { group in
2023-07-19 05:46:25 +00:00
Button {
2023-09-22 17:33:53 +00:00
selectedTagGroup = group
timeline = .tagGroup(title: group.title, tags: group.tags)
2023-07-19 05:46:25 +00:00
} label: {
VStack {
2023-09-22 17:33:53 +00:00
let icon = group.symbolName.isEmpty ? "number" : group.symbolName
2023-07-19 05:46:25 +00:00
Label(group.title, systemImage: icon)
}
2023-07-19 05:46:25 +00:00
}
}
Button {
routerPath.presentedSheet = .addTagGroup
} label: {
Label("timeline.filter.add-tag-groups", systemImage: "plus")
}
2023-07-19 05:46:25 +00:00
}
}
2023-01-17 10:36:01 +00:00
private var addAccountButton: some View {
Button {
routerPath.presentedSheet = .addAccount
} label: {
Image(systemName: "person.badge.plus")
}
.accessibilityLabel("accessibility.tabs.timeline.add-account")
}
2023-01-17 10:36:01 +00:00
@ToolbarContentBuilder
private var toolbarView: some ToolbarContent {
2023-01-16 13:40:23 +00:00
if canFilterTimeline {
ToolbarTitleMenu {
timelineFilterButton
}
}
if client.isAuth {
2023-01-16 21:01:04 +00:00
if UIDevice.current.userInterfaceIdiom != .pad {
2023-01-16 20:15:33 +00:00
ToolbarItem(placement: .navigationBarLeading) {
AppAccountsSelectorView(routerPath: routerPath)
.id(currentAccount.account?.id)
2023-01-16 20:15:33 +00:00
}
}
statusEditorToolbarItem(routerPath: routerPath,
visibility: preferences.postVisibility)
2023-09-16 12:15:03 +00:00
if UIDevice.current.userInterfaceIdiom == .pad, !preferences.showiPadSecondaryColumn {
SecondaryColumnToolbarItem()
}
} else {
ToolbarItem(placement: .navigationBarTrailing) {
addAccountButton
}
}
switch timeline {
case let .list(list):
ToolbarItem {
Button {
routerPath.presentedSheet = .listEdit(list: list)
} label: {
Image(systemName: "list.bullet")
}
}
2023-02-06 11:24:48 +00:00
case let .remoteLocal(server, _):
ToolbarItem {
2023-02-06 11:24:48 +00:00
Menu {
ForEach(RemoteTimelineFilter.allCases, id: \.self) { filter in
Button {
timeline = .remoteLocal(server: server, filter: filter)
} label: {
Label(filter.localizedTitle(), systemImage: filter.iconName())
}
}
} label: {
2023-02-06 11:24:48 +00:00
Image(systemName: "line.3.horizontal.decrease.circle")
}
}
default:
ToolbarItem {
EmptyView()
}
}
}
2023-09-19 16:33:13 +00:00
private func resetTimelineFilter() {
if client.isAuth, canFilterTimeline {
timeline = lastTimelineFilter
} else {
timeline = .federated
}
}
2022-11-29 10:46:02 +00:00
}