Bubble/Threaded/Views/ContentView.swift

120 lines
4.6 KiB
Swift
Raw Normal View History

2023-12-29 11:17:37 +01:00
//Made by Lumaa
import SwiftUI
// TODO: Make some sort of "Universal Navigation"?
/// Details: Fix bugs about `navigator.path` when tapping on any element that adds to it.
/// Possibility 1: Put a `NavigationStack` in the parent view of the tabs' view with no title
/// Possibility 2: Make another `Navigator` but universally
///
2023-12-29 11:17:37 +01:00
struct ContentView: View {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
2024-01-21 13:18:25 +01:00
@State private var preferences: UserPreferences = .defaultPreferences
@StateObject private var uniNavigator = UniversalNavigator() // "Universal Path" (POSS 1)
2024-02-11 17:00:29 +01:00
@StateObject private var accountManager: AccountManager = AccountManager.shared
2023-12-29 11:17:37 +01:00
var body: some View {
ZStack {
TabView(selection: $uniNavigator.selectedTab, content: {
if accountManager.getAccount() != nil {
TimelineView(timelineModel: FetchTimeline(client: accountManager.forceClient()))
2023-12-29 11:17:37 +01:00
.background(Color.appBackground)
.tag(TabDestination.timeline)
DiscoveryView()
.background(Color.appBackground)
.tag(TabDestination.search)
NotificationsView()
.background(Color.appBackground)
.tag(TabDestination.activity)
AccountView(account: accountManager.forceAccount())
2024-01-13 13:15:46 +01:00
.background(Color.appBackground)
.tag(TabDestination.profile)
2024-01-13 13:15:46 +01:00
} else {
ZStack {
Color.appBackground
.ignoresSafeArea()
}
}
})
}
2023-12-29 11:17:37 +01:00
.overlay(alignment: .bottom) {
if uniNavigator.showTabbar {
TabsView(selectedTab: $uniNavigator.selectedTab) {
uniNavigator.presentedSheet = .post(content: "", replyId: nil, editId: nil)
}
2023-12-29 11:17:37 +01:00
.safeAreaPadding(.vertical)
.offset(y: uniNavigator.showTabbar ? 0 : -20)
2023-12-29 11:17:37 +01:00
.zIndex(10)
}
2023-12-29 11:17:37 +01:00
}
.withSheets(sheetDestination: $uniNavigator.presentedSheet)
.withCovers(sheetDestination: $uniNavigator.presentedCover)
.environment(uniNavigator)
.environment(accountManager)
.environment(appDelegate)
.environmentObject(preferences)
2024-01-10 17:40:05 +01:00
.onAppear {
2024-01-21 13:18:25 +01:00
do {
preferences = try UserPreferences.loadAsCurrent() ?? .defaultPreferences
} catch {
print(error)
}
2024-01-10 17:40:05 +01:00
if accountManager.getClient() == nil {
Task {
await recognizeAccount()
}
}
}
2024-01-21 13:08:43 +01:00
.environment(\.openURL, OpenURLAction { url in
// Open internal URL.
2024-01-21 13:18:25 +01:00
guard preferences.browserType == .inApp else { return .systemAction }
uniNavigator.presentedSheet = .safari(url: url)
2024-01-21 13:08:43 +01:00
return OpenURLAction.Result.handled
})
.onOpenURL(perform: { url in
2024-01-21 13:18:25 +01:00
guard preferences.browserType == .inApp else { return }
uniNavigator.presentedSheet = .safari(url: url)
2024-01-21 13:08:43 +01:00
})
}
func recognizeAccount() async {
let appAccount: AppAccount? = AppAccount.loadAsCurrent()
if appAccount == nil {
2024-02-04 10:50:21 +01:00
uniNavigator.presentedCover = .welcome
} else {
accountManager.setClient(.init(server: appAccount!.server, oauthToken: appAccount!.oauthToken))
// Check if token is still working
let fetched: Account? = await accountManager.fetchAccount()
if fetched == nil {
accountManager.clear()
appAccount!.clear()
2024-02-04 10:50:21 +01:00
uniNavigator.presentedCover = .welcome
2023-12-29 11:17:37 +01:00
}
}
}
init() {
let appearance = UITabBarAppearance()
appearance.configureWithTransparentBackground()
appearance.stackedLayoutAppearance.normal.iconColor = .white
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
appearance.stackedLayoutAppearance.selected.iconColor = UIColor(Color.accentColor)
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(Color.accentColor)]
UITabBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().tintColor = UIColor.label
2023-12-29 11:17:37 +01:00
}
}
#Preview {
ContentView()
}