2022-11-29 11:46:02 +01:00
|
|
|
import SwiftUI
|
|
|
|
import Timeline
|
2022-12-22 10:53:36 +01:00
|
|
|
import Env
|
2022-11-29 12:18:06 +01:00
|
|
|
import Network
|
2022-12-24 11:50:05 +01:00
|
|
|
import Combine
|
2023-01-01 14:02:11 +01:00
|
|
|
import DesignSystem
|
2023-01-02 19:23:44 +01:00
|
|
|
import Models
|
2023-01-12 18:17:21 +01:00
|
|
|
import AppAccount
|
2022-11-29 11:46:02 +01:00
|
|
|
|
2022-12-01 09:05:26 +01:00
|
|
|
struct TimelineTab: View {
|
2023-01-01 14:02:11 +01:00
|
|
|
@EnvironmentObject private var theme: Theme
|
2022-12-30 08:36:22 +01:00
|
|
|
@EnvironmentObject private var currentAccount: CurrentAccount
|
2023-01-06 12:14:05 +01:00
|
|
|
@EnvironmentObject private var preferences: UserPreferences
|
2022-12-23 15:53:02 +01:00
|
|
|
@EnvironmentObject private var client: Client
|
2022-11-29 11:46:02 +01:00
|
|
|
@StateObject private var routeurPath = RouterPath()
|
2022-12-27 09:25:26 +01:00
|
|
|
@Binding var popToRootTab: Tab
|
2023-01-02 19:23:44 +01:00
|
|
|
|
2023-01-06 12:14:05 +01:00
|
|
|
@State private var didAppear: Bool = false
|
2023-01-16 14:40:23 +01:00
|
|
|
@State private var timeline: TimelineFilter
|
2022-12-31 12:28:27 +01:00
|
|
|
@State private var scrollToTopSignal: Int = 0
|
2023-01-16 14:40:23 +01:00
|
|
|
|
|
|
|
private let canFilterTimeline: Bool
|
|
|
|
|
|
|
|
init(popToRootTab: Binding<Tab>, timeline: TimelineFilter? = nil) {
|
|
|
|
canFilterTimeline = timeline == nil
|
|
|
|
self.timeline = timeline ?? .home
|
|
|
|
_popToRootTab = popToRootTab
|
|
|
|
}
|
2023-01-06 17:14:34 +01:00
|
|
|
|
2022-11-29 11:46:02 +01:00
|
|
|
var body: some View {
|
|
|
|
NavigationStack(path: $routeurPath.path) {
|
2022-12-31 12:28:27 +01:00
|
|
|
TimelineView(timeline: $timeline, scrollToTopSignal: $scrollToTopSignal)
|
2022-11-29 11:46:02 +01:00
|
|
|
.withAppRouteur()
|
2022-12-20 09:37:07 +01:00
|
|
|
.withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet)
|
2022-12-23 10:41:55 +01:00
|
|
|
.toolbar {
|
2023-01-06 12:14:05 +01:00
|
|
|
toolbarView
|
2022-12-23 10:41:55 +01:00
|
|
|
}
|
2022-12-30 08:36:22 +01:00
|
|
|
.id(currentAccount.account?.id)
|
2022-11-29 11:46:02 +01:00
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
.onAppear {
|
2022-12-27 08:31:47 +01:00
|
|
|
routeurPath.client = client
|
2023-01-16 14:40:23 +01:00
|
|
|
if !didAppear && canFilterTimeline {
|
2023-01-06 12:14:05 +01:00
|
|
|
didAppear = true
|
|
|
|
timeline = client.isAuth ? .home : .federated
|
|
|
|
}
|
2023-01-02 19:23:44 +01:00
|
|
|
Task {
|
|
|
|
await currentAccount.fetchLists()
|
|
|
|
}
|
2023-01-12 06:30:43 +01:00
|
|
|
if !client.isAuth {
|
|
|
|
routeurPath.presentedSheet = .addAccount
|
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
.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 11:50:05 +01:00
|
|
|
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
|
|
|
|
if popToRootTab == .timeline {
|
2022-12-31 12:28:27 +01:00
|
|
|
if routeurPath.path.isEmpty {
|
|
|
|
scrollToTopSignal += 1
|
|
|
|
} else {
|
|
|
|
routeurPath.path = []
|
|
|
|
}
|
2022-12-24 11:50:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-02 19:23:44 +01:00
|
|
|
.onChange(of: currentAccount.account?.id) { _ in
|
|
|
|
routeurPath.path = []
|
|
|
|
}
|
2023-01-08 19:56:16 +01:00
|
|
|
.withSafariRouteur()
|
2023-01-06 12:14:05 +01:00
|
|
|
.environmentObject(routeurPath)
|
2022-11-29 11:46:02 +01:00
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
|
|
|
|
|
2023-01-02 19:23:44 +01:00
|
|
|
@ViewBuilder
|
2022-12-26 07:36:54 +01:00
|
|
|
private var timelineFilterButton: some View {
|
2023-01-01 18:31:23 +01:00
|
|
|
ForEach(TimelineFilter.availableTimeline(client: client), id: \.self) { timeline in
|
|
|
|
Button {
|
|
|
|
self.timeline = timeline
|
|
|
|
} label: {
|
|
|
|
Label(timeline.title(), systemImage: timeline.iconName() ?? "")
|
2023-01-01 14:02:11 +01:00
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
2023-01-02 19:23:44 +01:00
|
|
|
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 18:37:58 +01: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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
|
2023-01-06 17:14:34 +01:00
|
|
|
Menu("Local Timelines") {
|
|
|
|
ForEach(preferences.remoteLocalTimelines, id: \.self) { server in
|
|
|
|
Button {
|
|
|
|
timeline = .remoteLocal(server: server)
|
|
|
|
} label: {
|
|
|
|
Label(server, systemImage: "dot.radiowaves.right")
|
2023-01-06 12:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 17:14:34 +01:00
|
|
|
Button {
|
|
|
|
routeurPath.presentedSheet = .addRemoteLocalTimeline
|
|
|
|
} label: {
|
|
|
|
Label("Add a local timeline", systemImage: "badge.plus.radiowaves.right")
|
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
}
|
2023-01-01 14:02:11 +01:00
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
|
|
|
|
private var addAccountButton: some View {
|
2023-01-01 14:02:11 +01:00
|
|
|
Button {
|
2023-01-06 12:14:05 +01:00
|
|
|
routeurPath.presentedSheet = .addAccount
|
2023-01-01 14:02:11 +01:00
|
|
|
} label: {
|
2023-01-06 12:14:05 +01:00
|
|
|
Image(systemName: "person.badge.plus")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ToolbarContentBuilder
|
|
|
|
private var toolbarView: some ToolbarContent {
|
2023-01-16 14:40:23 +01:00
|
|
|
if canFilterTimeline {
|
|
|
|
ToolbarTitleMenu {
|
|
|
|
timelineFilterButton
|
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
}
|
|
|
|
if client.isAuth {
|
2023-01-16 21:15:33 +01:00
|
|
|
if !ProcessInfo.processInfo.isiOSAppOnMac {
|
|
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
|
|
AppAccountsSelectorView(routeurPath: routeurPath)
|
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
}
|
2023-01-09 20:46:01 +01:00
|
|
|
statusEditorToolbarItem(routeurPath: routeurPath,
|
|
|
|
visibility: preferences.serverPreferences?.postVisibility ?? .pub)
|
2023-01-06 12:14:05 +01:00
|
|
|
} else {
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
|
|
addAccountButton
|
2023-01-01 14:02:11 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
switch timeline {
|
|
|
|
case let .list(list):
|
|
|
|
ToolbarItem {
|
|
|
|
Button {
|
|
|
|
routeurPath.presentedSheet = .listEdit(list: list)
|
|
|
|
} label: {
|
|
|
|
Image(systemName: "list.bullet")
|
2023-01-01 14:02:11 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
case let .remoteLocal(server):
|
|
|
|
ToolbarItem {
|
2023-01-01 14:02:11 +01:00
|
|
|
Button {
|
2023-01-06 12:14:05 +01:00
|
|
|
preferences.remoteLocalTimelines.removeAll(where: { $0 == server })
|
|
|
|
timeline = client.isAuth ? .home : .federated
|
2023-01-01 14:02:11 +01:00
|
|
|
} label: {
|
2023-01-06 12:14:05 +01:00
|
|
|
Image(systemName: "pin.slash")
|
2023-01-01 14:02:11 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
default:
|
|
|
|
ToolbarItem {
|
|
|
|
EmptyView()
|
2023-01-01 14:02:11 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
2022-11-29 11:46:02 +01:00
|
|
|
}
|