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

177 lines
4.9 KiB
Swift
Raw Normal View History

2022-11-29 10:46:02 +00:00
import SwiftUI
import Timeline
2022-12-22 09:53:36 +00:00
import Env
2022-11-29 11:18:06 +00:00
import Network
2022-12-24 10:50:05 +00:00
import Combine
import DesignSystem
import Models
2022-11-29 10:46:02 +00:00
2022-12-01 08:05:26 +00:00
struct TimelineTab: View {
@EnvironmentObject private var theme: Theme
2022-12-30 07:36:22 +00:00
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var preferences: UserPreferences
2022-12-23 14:53:02 +00:00
@EnvironmentObject private var client: Client
2022-11-29 10:46:02 +00:00
@StateObject private var routeurPath = RouterPath()
2022-12-27 08:25:26 +00:00
@Binding var popToRootTab: Tab
@State private var didAppear: Bool = false
@State private var timeline: TimelineFilter = .home
2022-12-31 11:28:27 +00:00
@State private var scrollToTopSignal: Int = 0
@State private var newlyAddedLocalTimeline: String = ""
@State private var isAddRemoteLocalTimelinePresented: Bool = false
2022-11-29 10:46:02 +00:00
var body: some View {
NavigationStack(path: $routeurPath.path) {
2022-12-31 11:28:27 +00:00
TimelineView(timeline: $timeline, scrollToTopSignal: $scrollToTopSignal)
2022-11-29 10:46:02 +00:00
.withAppRouteur()
2022-12-20 08:37:07 +00:00
.withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet)
.toolbar {
toolbarView
}
2022-12-30 07:36:22 +00:00
.id(currentAccount.account?.id)
2022-11-29 10:46:02 +00:00
}
.sheet(isPresented: $isAddRemoteLocalTimelinePresented) {
AddRemoteTimelineVIew(addedInstance: $newlyAddedLocalTimeline)
}
.onAppear {
routeurPath.client = client
if !didAppear {
didAppear = true
timeline = client.isAuth ? .home : .federated
}
Task {
await currentAccount.fetchLists()
}
}
.onChange(of: client.isAuth, perform: { isAuth in
timeline = isAuth ? .home : .federated
})
.onChange(of: currentAccount.account?.id, perform: { _ in
timeline = client.isAuth ? .home : .federated
})
2022-12-24 10:50:05 +00:00
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
if popToRootTab == .timeline {
2022-12-31 11:28:27 +00:00
if routeurPath.path.isEmpty {
scrollToTopSignal += 1
} else {
routeurPath.path = []
}
2022-12-24 10:50:05 +00:00
}
}
.onChange(of: currentAccount.account?.id) { _ in
routeurPath.path = []
}
.onChange(of: isAddRemoteLocalTimelinePresented) { isPresented in
if !isPresented && !newlyAddedLocalTimeline.isEmpty {
preferences.remoteLocalTimelines.append(newlyAddedLocalTimeline)
timeline = .remoteLocal(server: newlyAddedLocalTimeline)
newlyAddedLocalTimeline = ""
}
}
.environmentObject(routeurPath)
2022-11-29 10:46:02 +00:00
}
@ViewBuilder
private var timelineFilterButton: some View {
2023-01-01 17:31:23 +00:00
ForEach(TimelineFilter.availableTimeline(client: client), id: \.self) { timeline in
Button {
self.timeline = timeline
} label: {
Label(timeline.title(), systemImage: timeline.iconName() ?? "")
}
}
if !currentAccount.lists.isEmpty {
Menu("Lists") {
ForEach(currentAccount.lists) { list in
Button {
timeline = .list(list: list)
} label: {
Label(list.title, systemImage: "list.bullet")
}
}
}
}
2023-01-04 17:37:58 +00:00
if !currentAccount.tags.isEmpty {
Menu("Followed Tags") {
ForEach(currentAccount.tags) { tag in
Button {
timeline = .hashtag(tag: tag.name, accountId: nil)
} label: {
Label("#\(tag.name)", systemImage: "number")
}
}
}
}
if !preferences.remoteLocalTimelines.isEmpty {
Menu("Local Timelines") {
ForEach(preferences.remoteLocalTimelines, id: \.self) { server in
Button {
timeline = .remoteLocal(server: server)
} label: {
Label(server, systemImage: "dot.radiowaves.right")
}
}
}
}
Button {
isAddRemoteLocalTimelinePresented = true
} label: {
Label("Add a local timeline", systemImage: "badge.plus.radiowaves.right")
}
}
private var addAccountButton: some View {
Button {
routeurPath.presentedSheet = .addAccount
} label: {
Image(systemName: "person.badge.plus")
}
}
@ToolbarContentBuilder
private var toolbarView: some ToolbarContent {
ToolbarTitleMenu {
timelineFilterButton
}
if client.isAuth {
ToolbarItem(placement: .navigationBarLeading) {
AppAccountsSelectorView(routeurPath: routeurPath)
}
statusEditorToolbarItem(routeurPath: routeurPath, visibility: .pub)
} else {
ToolbarItem(placement: .navigationBarTrailing) {
addAccountButton
}
}
switch timeline {
case let .list(list):
ToolbarItem {
Button {
routeurPath.presentedSheet = .listEdit(list: list)
} label: {
Image(systemName: "list.bullet")
}
}
case let .remoteLocal(server):
ToolbarItem {
Button {
preferences.remoteLocalTimelines.removeAll(where: { $0 == server })
timeline = client.isAuth ? .home : .federated
} label: {
Image(systemName: "pin.slash")
}
}
default:
ToolbarItem {
EmptyView()
}
}
}
2022-11-29 10:46:02 +00:00
}