2023-12-29 11:17:37 +01:00
|
|
|
//Made by Lumaa
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-02-04 08:43:56 +01:00
|
|
|
// 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 {
|
2024-01-04 22:19:35 +01:00
|
|
|
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
|
|
|
|
|
2024-01-21 13:18:25 +01:00
|
|
|
@State private var preferences: UserPreferences = .defaultPreferences
|
2024-02-04 10:44:51 +01:00
|
|
|
@StateObject private var uniNavigator = UniversalNavigator() // "Universal Path" (POSS 1)
|
2024-01-02 14:23:36 +01:00
|
|
|
@State private var accountManager: AccountManager = AccountManager()
|
2023-12-29 11:17:37 +01:00
|
|
|
|
|
|
|
var body: some View {
|
2024-02-04 08:43:56 +01:00
|
|
|
ZStack {
|
2024-02-04 10:44:51 +01:00
|
|
|
TabView(selection: $uniNavigator.selectedTab, content: {
|
2024-02-04 08:43:56 +01:00
|
|
|
if accountManager.getAccount() != nil {
|
2024-02-04 10:44:51 +01:00
|
|
|
TimelineView(timelineModel: FetchTimeline(client: accountManager.forceClient()))
|
2023-12-29 11:17:37 +01:00
|
|
|
.background(Color.appBackground)
|
2024-02-04 08:43:56 +01:00
|
|
|
.tag(TabDestination.timeline)
|
|
|
|
|
|
|
|
Text(String("Search"))
|
|
|
|
.background(Color.appBackground)
|
|
|
|
.tag(TabDestination.search)
|
|
|
|
|
|
|
|
//TODO: Messaging UI in Activity tab
|
|
|
|
NotificationsView()
|
|
|
|
.background(Color.appBackground)
|
|
|
|
.tag(TabDestination.activity)
|
|
|
|
|
2024-02-04 10:44:51 +01:00
|
|
|
AccountView(account: accountManager.forceAccount())
|
2024-01-13 13:15:46 +01:00
|
|
|
.background(Color.appBackground)
|
2024-02-04 08:43:56 +01:00
|
|
|
.tag(TabDestination.profile)
|
2024-01-13 13:15:46 +01:00
|
|
|
} else {
|
|
|
|
ZStack {
|
|
|
|
Color.appBackground
|
|
|
|
.ignoresSafeArea()
|
|
|
|
}
|
|
|
|
}
|
2024-02-04 08:43:56 +01:00
|
|
|
})
|
|
|
|
}
|
2023-12-29 11:17:37 +01:00
|
|
|
.overlay(alignment: .bottom) {
|
2024-02-04 10:44:51 +01:00
|
|
|
if uniNavigator.showTabbar {
|
|
|
|
TabsView(selectedTab: $uniNavigator.selectedTab) {
|
|
|
|
uniNavigator.presentedSheet = .post(content: "", replyId: nil, editId: nil)
|
2024-02-04 08:43:56 +01:00
|
|
|
}
|
2023-12-29 11:17:37 +01:00
|
|
|
.safeAreaPadding(.vertical)
|
2024-02-04 10:44:51 +01:00
|
|
|
.offset(y: uniNavigator.showTabbar ? 0 : -20)
|
2023-12-29 11:17:37 +01:00
|
|
|
.zIndex(10)
|
2024-02-04 08:43:56 +01:00
|
|
|
}
|
2023-12-29 11:17:37 +01:00
|
|
|
}
|
2024-02-04 10:44:51 +01:00
|
|
|
.withSheets(sheetDestination: $uniNavigator.presentedSheet)
|
|
|
|
.withCovers(sheetDestination: $uniNavigator.presentedCover)
|
|
|
|
.environment(uniNavigator)
|
2024-02-04 08:43:56 +01:00
|
|
|
.environment(accountManager)
|
2024-01-04 22:19:35 +01:00
|
|
|
.environment(appDelegate)
|
2024-02-04 08:43:56 +01:00
|
|
|
.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 }
|
2024-02-04 10:44:51 +01:00
|
|
|
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 }
|
2024-02-04 10:44:51 +01:00
|
|
|
uniNavigator.presentedSheet = .safari(url: url)
|
2024-01-21 13:08:43 +01:00
|
|
|
})
|
2024-01-02 14:23:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func recognizeAccount() async {
|
2024-02-04 08:43:56 +01:00
|
|
|
let appAccount: AppAccount? = AppAccount.loadAsCurrent()
|
|
|
|
if appAccount == nil {
|
2024-02-04 10:50:21 +01:00
|
|
|
uniNavigator.presentedCover = .welcome
|
2024-01-02 14:23:36 +01:00
|
|
|
} else {
|
2024-02-04 08:43:56 +01:00
|
|
|
//TODO: Fix this? (Fatal error: calling into SwiftUI on a non-main thread is not supported)
|
|
|
|
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
|
2024-01-02 14:23:36 +01:00
|
|
|
UINavigationBar.appearance().tintColor = UIColor.label
|
2023-12-29 11:17:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
ContentView()
|
|
|
|
}
|