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

217 lines
6.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 SwiftUI
import Timeline
2022-11-29 10:46:02 +00:00
2023-01-22 05:38:30 +00:00
struct TimelineTab: View {
2023-01-29 10:52:11 +00:00
@EnvironmentObject private var appAccount: AppAccountsManager
@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
@StateObject 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-01-16 13:40:23 +00:00
@State private var timeline: TimelineFilter
2022-12-31 11:28:27 +00:00
@State private var scrollToTopSignal: Int = 0
@AppStorage("last_timeline_filter") public var lastTimelineFilter: TimelineFilter = 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
self.timeline = timeline ?? .home
_popToRootTab = popToRootTab
}
2023-01-17 10:36:01 +00:00
2022-11-29 10:46:02 +00:00
var body: some View {
NavigationStack(path: $routerPath.path) {
2022-12-31 11:28:27 +00:00
TimelineView(timeline: $timeline, scrollToTopSignal: $scrollToTopSignal)
.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-01-16 13:40:23 +00:00
if !didAppear && canFilterTimeline {
didAppear = true
if(client.isAuth) {
timeline = lastTimelineFilter
} else {
timeline = .federated
}
}
Task {
await currentAccount.fetchLists()
}
if !client.isAuth {
routerPath.presentedSheet = .addAccount
2023-01-12 05:30:43 +00:00
}
}
.onChange(of: client.isAuth, perform: { isAuth in
if(client.isAuth) {
timeline = lastTimelineFilter
} else {
timeline = .federated
}
})
.onChange(of: currentAccount.account?.id, perform: { _ in
if(client.isAuth && canFilterTimeline) {
timeline = lastTimelineFilter
} else {
timeline = .federated
}
})
2022-12-24 10:50:05 +00:00
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
if popToRootTab == .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: currentAccount.account?.id) { _ in
routerPath.path = []
}
.onChange(of: timeline) { timeline in
if(timeline == .home || timeline == .federated || timeline == .local) {
lastTimelineFilter = timeline
}
}
.withSafariRouter()
.environmentObject(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 == .home {
Button {
self.timeline = .latest
} label: {
Label(TimelineFilter.latest.localizedTitle(), systemImage: TimelineFilter.latest.iconName() ?? "")
}
2023-02-06 11:24:48 +00:00
.keyboardShortcut("r", modifiers: .command)
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 {
let sortedLists = currentAccount.lists.sorted { $0.title.lowercased() < $1.title.lowercased() }
Menu("timeline.filter.lists") {
ForEach(sortedLists) { list in
Button {
timeline = .list(list: list)
} label: {
Label(list.title, systemImage: "list.bullet")
}
}
}
}
2023-01-17 10:36:01 +00:00
2023-01-04 17:37:58 +00:00
if !currentAccount.tags.isEmpty {
let sortedTags = currentAccount.tags.sorted { $0.name.lowercased() < $1.name.lowercased() }
Menu("timeline.filter.tags") {
ForEach(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-01-06 16:14:34 +00:00
ForEach(preferences.remoteLocalTimelines, id: \.self) { server in
Button {
2023-02-06 11:24:48 +00:00
timeline = .remoteLocal(server: server, filter: .local)
2023-01-06 16:14:34 +00:00
} label: {
2023-02-06 11:24:48 +00:00
VStack {
Label(server, systemImage: "dot.radiowaves.right")
}
}
}
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-01-17 10:36:01 +00:00
private var addAccountButton: some View {
Button {
routerPath.presentedSheet = .addAccount
} label: {
Image(systemName: "person.badge.plus")
}
}
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)
} 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()
}
}
}
2022-11-29 10:46:02 +00:00
}