IceCubesApp/IceCubesApp/App/Tabs/TimelineTab.swift

138 lines
3.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
2022-11-29 10:46:02 +00:00
2022-12-01 08:05:26 +00:00
struct TimelineTab: View {
@EnvironmentObject private var appAccounts: AppAccountsManager
@EnvironmentObject private var theme: Theme
2022-12-30 07:36:22 +00:00
@EnvironmentObject private var currentAccount: CurrentAccount
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 timeline: TimelineFilter = .home
2022-12-31 11:28:27 +00:00
@State private var scrollToTopSignal: Int = 0
2023-01-01 08:19:00 +00:00
@State private var isAddAccountSheetDisplayed = false
@State private var accountsViewModel: [AppAccountViewModel] = []
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 {
ToolbarItem(placement: .principal) {
timelineFilterButton
}
2022-12-23 14:53:02 +00:00
if client.isAuth {
ToolbarItem(placement: .navigationBarLeading) {
accountButton
}
statusEditorToolbarItem(routeurPath: routeurPath)
2023-01-01 08:19:00 +00:00
} else {
ToolbarItem(placement: .navigationBarTrailing) {
addAccountButton
}
}
}
2022-12-30 07:36:22 +00:00
.id(currentAccount.account?.id)
2022-11-29 10:46:02 +00:00
}
.sheet(isPresented: $isAddAccountSheetDisplayed) {
AddAccountView()
}
.onAppear {
routeurPath.client = client
timeline = client.isAuth ? .home : .pub
}
2022-11-29 10:46:02 +00:00
.environmentObject(routeurPath)
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
}
}
2022-11-29 10:46:02 +00:00
}
private var timelineFilterButton: some View {
Menu {
ForEach(TimelineFilter.availableTimeline(client: client), id: \.self) { timeline in
Button {
self.timeline = timeline
} label: {
Label(timeline.title(), systemImage: timeline.iconName() ?? "")
}
}
} label: {
HStack {
Text(timeline.title())
Image(systemName: "chevron.down")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 12)
.offset(y: 2)
}
.font(.headline)
.foregroundColor(theme.labelColor)
}
.menuStyle(.button)
}
private var accountButton: some View {
Button {
if let account = currentAccount.account {
routeurPath.navigate(to: .accountDetailWithAccount(account: account))
}
} label: {
if let avatar = currentAccount.account?.avatar {
AvatarView(url: avatar, size: .badge)
}
}
.onAppear {
if accountsViewModel.isEmpty || appAccounts.availableAccounts.count != accountsViewModel.count {
accountsViewModel = []
for account in appAccounts.availableAccounts {
let viewModel: AppAccountViewModel = .init(appAccount: account)
accountsViewModel.append(viewModel)
Task {
await viewModel.fetchAccount()
}
}
}
}
.contextMenu {
ForEach(accountsViewModel, id: \.appAccount.id) { viewModel in
Button {
appAccounts.currentAccount = viewModel.appAccount
} label: {
HStack {
if viewModel.account?.id == currentAccount.account?.id {
Image(systemName: "checkmark.circle.fill")
}
Text("\(viewModel.account?.displayName ?? "")")
}
}
}
Button {
isAddAccountSheetDisplayed = true
} label: {
Label("Add Account", systemImage: "person.badge.plus")
}
}
2023-01-01 08:19:00 +00:00
}
private var addAccountButton: some View {
Button {
isAddAccountSheetDisplayed = true
} label: {
Image(systemName: "person.badge.plus")
}
}
2022-11-29 10:46:02 +00:00
}