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

332 lines
9.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-12-30 13:54:09 +00:00
@AppStorage("timeline_pinned_filters") private var pinnedFilters: [TimelineFilter] = []
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,
2023-12-30 13:54:09 +00:00
pinnedFilters: $pinnedFilters,
2023-09-22 17:33:53 +00:00
selectedTagGroup: $selectedTagGroup,
scrollToTopSignal: $scrollToTopSignal,
canFilterTimeline: canFilterTimeline)
.withAppRouter()
.withSheetDestinations(sheetDestinations: $routerPath.presentedSheet)
.toolbar {
toolbarView
}
2024-01-23 07:51:58 +00:00
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.30), 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 let .tagGroup(title, _, _):
2024-02-14 11:48:14 +00:00
if let group = tagGroups.first(where: { $0.title == title }) {
2024-01-01 18:00:23 +00:00
selectedTagGroup = group
}
2023-09-22 20:41:06 +00:00
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 {
2024-01-22 05:38:56 +00:00
headerGroup
2023-12-30 13:54:09 +00:00
timelineFiltersButtons
listsFiltersButons
tagsFiltersButtons
localTimelinesFiltersButtons
tagGroupsFiltersButtons
2024-01-22 05:38:56 +00:00
Divider()
contentFilterButton
2023-12-30 13:54:09 +00:00
}
private var addAccountButton: some View {
Button {
routerPath.presentedSheet = .addAccount
} label: {
Image(systemName: "person.badge.plus")
}
.accessibilityLabel("accessibility.tabs.timeline.add-account")
}
@ToolbarContentBuilder
private var toolbarView: some ToolbarContent {
if canFilterTimeline {
ToolbarTitleMenu {
timelineFilterButton
}
}
if client.isAuth {
ToolbarTab(routerPath: $routerPath)
} else {
ToolbarItem(placement: .navigationBarTrailing) {
addAccountButton
}
}
switch timeline {
case let .list(list):
ToolbarItem {
Button {
routerPath.presentedSheet = .listEdit(list: list)
} label: {
Image(systemName: "list.bullet")
}
}
case let .remoteLocal(server, _):
ToolbarItem {
Menu {
ForEach(RemoteTimelineFilter.allCases, id: \.self) { filter in
Button {
timeline = .remoteLocal(server: server, filter: filter)
} label: {
Label(filter.localizedTitle(), systemImage: filter.iconName())
}
}
} label: {
Image(systemName: "line.3.horizontal.decrease.circle")
}
}
default:
ToolbarItem {
EmptyView()
}
}
}
2024-02-14 11:48:14 +00:00
2023-12-30 13:54:09 +00:00
@ViewBuilder
2024-01-22 05:38:56 +00:00
private var headerGroup: some View {
ControlGroup {
if timeline.supportNewestPagination {
Button {
timeline = .latest
} label: {
Label(TimelineFilter.latest.localizedTitle(), systemImage: TimelineFilter.latest.iconName())
}
}
if timeline == .home {
Button {
timeline = .resume
} label: {
VStack {
Label(TimelineFilter.resume.localizedTitle(),
2023-12-30 15:16:19 +00:00
systemImage: TimelineFilter.resume.iconName())
}
}
}
2024-01-22 05:38:56 +00:00
pinButton
}
2023-12-30 13:54:09 +00:00
}
2024-02-14 11:48:14 +00:00
2023-12-30 13:54:09 +00:00
@ViewBuilder
2024-01-22 05:38:56 +00:00
private var pinButton: some View {
2024-02-14 11:48:14 +00:00
let index = pinnedFilters.firstIndex(where: { $0.id == timeline.id })
2023-12-30 13:54:09 +00:00
Button {
withAnimation {
if let index {
pinnedFilters.remove(at: index)
} else {
pinnedFilters.append(timeline)
}
}
} label: {
2024-02-14 11:48:14 +00:00
if index != nil {
2023-12-30 13:54:09 +00:00
Label("status.action.unpin", systemImage: "pin.slash")
} else {
Label("status.action.pin", systemImage: "pin")
}
}
}
2024-02-14 11:48:14 +00:00
2023-12-30 13:54:09 +00:00
private var timelineFiltersButtons: some View {
2023-01-01 17:31:23 +00:00
ForEach(TimelineFilter.availableTimeline(client: client), id: \.self) { timeline in
Button {
self.timeline = timeline
} label: {
2023-12-30 15:16:19 +00:00
Label(timeline.localizedTitle(), systemImage: timeline.iconName())
}
}
2023-12-30 13:54:09 +00:00
}
2024-02-14 11:48:14 +00:00
2023-12-30 13:54:09 +00:00
@ViewBuilder
private var listsFiltersButons: some View {
Menu("timeline.filter.lists") {
Button {
routerPath.presentedSheet = .listCreate
} label: {
Label("account.list.create", systemImage: "plus")
}
ForEach(currentAccount.sortedLists) { list in
2023-11-28 08:18:52 +00:00
Button {
timeline = .list(list: list)
2023-11-28 08:18:52 +00:00
} label: {
Label(list.title, systemImage: "list.bullet")
2023-11-28 08:18:52 +00:00
}
}
}
2023-12-30 13:54:09 +00:00
}
2024-02-14 11:48:14 +00:00
2023-12-30 13:54:09 +00:00
@ViewBuilder
private var tagsFiltersButtons: some View {
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-12-30 13:54:09 +00:00
}
2024-02-14 11:48:14 +00:00
2023-12-30 13:54:09 +00:00
private var localTimelinesFiltersButtons: some View {
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-12-30 13:54:09 +00:00
}
2024-02-14 11:48:14 +00:00
2023-12-30 13:54:09 +00:00
private var tagGroupsFiltersButtons: some View {
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 {
timeline = .tagGroup(title: group.title, tags: group.tags, symbolName: group.symbolName)
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
}
}
2024-02-14 11:48:14 +00:00
2024-01-11 17:55:35 +00:00
private var contentFilterButton: some View {
Button(action: {
routerPath.presentedSheet = .timelineContentFilter
}, label: {
Label("timeline.content-filter.title", systemSymbol: .line3HorizontalDecrease)
})
}
2023-01-17 10:36:01 +00:00
2023-09-19 16:33:13 +00:00
private func resetTimelineFilter() {
if client.isAuth, canFilterTimeline {
timeline = lastTimelineFilter
} else if !client.isAuth {
2023-09-19 16:33:13 +00:00
timeline = .federated
}
}
2022-11-29 10:46:02 +00:00
}