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
|
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 appAccounts: AppAccountsManager
|
|
|
|
@EnvironmentObject private var theme: Theme
|
2022-12-30 08:36:22 +01:00
|
|
|
@EnvironmentObject private var currentAccount: CurrentAccount
|
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
|
2022-12-26 07:36:54 +01:00
|
|
|
@State private var timeline: TimelineFilter = .home
|
2022-12-31 12:28:27 +01:00
|
|
|
@State private var scrollToTopSignal: Int = 0
|
2023-01-01 09:19:00 +01:00
|
|
|
@State private var isAddAccountSheetDisplayed = false
|
2023-01-01 14:02:11 +01:00
|
|
|
@State private var accountsViewModel: [AppAccountViewModel] = []
|
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-01 14:02:11 +01:00
|
|
|
ToolbarItem(placement: .principal) {
|
|
|
|
timelineFilterButton
|
|
|
|
}
|
2022-12-23 15:53:02 +01:00
|
|
|
if client.isAuth {
|
|
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
2023-01-01 14:02:11 +01:00
|
|
|
accountButton
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
2023-01-01 14:02:11 +01:00
|
|
|
statusEditorToolbarItem(routeurPath: routeurPath)
|
2023-01-01 09:19:00 +01:00
|
|
|
} else {
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
|
|
addAccountButton
|
|
|
|
}
|
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
|
|
|
}
|
2023-01-01 14:02:11 +01:00
|
|
|
.sheet(isPresented: $isAddAccountSheetDisplayed) {
|
|
|
|
AddAccountView()
|
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
.onAppear {
|
2022-12-27 08:31:47 +01:00
|
|
|
routeurPath.client = client
|
2022-12-29 06:55:53 +01:00
|
|
|
timeline = client.isAuth ? .home : .pub
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
2022-11-29 11:46:02 +01:00
|
|
|
.environmentObject(routeurPath)
|
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
|
|
|
}
|
|
|
|
}
|
2022-11-29 11:46:02 +01:00
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
private var timelineFilterButton: some View {
|
|
|
|
Menu {
|
2023-01-01 14:02:11 +01:00
|
|
|
ForEach(TimelineFilter.availableTimeline(client: client), id: \.self) { timeline in
|
2022-12-26 07:36:54 +01:00
|
|
|
Button {
|
|
|
|
self.timeline = timeline
|
|
|
|
} label: {
|
2022-12-28 19:10:13 +01:00
|
|
|
Label(timeline.title(), systemImage: timeline.iconName() ?? "")
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
2023-01-01 14:02:11 +01:00
|
|
|
HStack {
|
|
|
|
Text(timeline.title())
|
|
|
|
Image(systemName: "chevron.down")
|
|
|
|
.resizable()
|
|
|
|
.aspectRatio(contentMode: .fit)
|
|
|
|
.frame(width: 12)
|
|
|
|
.offset(y: 2)
|
|
|
|
}
|
|
|
|
.font(.headline)
|
|
|
|
.foregroundColor(theme.labelColor)
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
2023-01-01 14:02:11 +01:00
|
|
|
.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 09:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private var addAccountButton: some View {
|
|
|
|
Button {
|
|
|
|
isAddAccountSheetDisplayed = true
|
|
|
|
} label: {
|
|
|
|
Image(systemName: "person.badge.plus")
|
|
|
|
}
|
2022-12-26 07:36:54 +01:00
|
|
|
}
|
2022-11-29 11:46:02 +01:00
|
|
|
}
|