Bubble/Threaded/Views/ContentView.swift

130 lines
4.8 KiB
Swift
Raw Normal View History

2023-12-29 11:17:37 +01:00
//Made by Lumaa
import SwiftUI
///
2023-12-29 11:17:37 +01:00
struct ContentView: View {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
2024-02-18 12:44:58 +01:00
private var huggingFace: HuggingFace = HuggingFace()
2024-01-21 13:18:25 +01:00
@State private var preferences: UserPreferences = .defaultPreferences
2024-02-15 22:37:58 +01:00
@StateObject private var uniNavigator = UniversalNavigator()
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) {
2024-02-15 23:22:42 +01:00
TabsView(selectedTab: $uniNavigator.selectedTab, postButton: {
uniNavigator.presentedSheet = .post(content: "", replyId: nil, editId: nil)
})
.safeAreaPadding(.vertical, 10)
2024-02-15 22:42:26 +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)
2024-02-18 12:44:58 +01:00
.environment(huggingFace)
.environmentObject(preferences)
2024-02-17 02:06:06 +01:00
.navigationBarTitleDisplayMode(.inline)
2024-01-10 17:40:05 +01:00
.onAppear {
2024-02-20 22:31:44 +01:00
showNew()
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-02-18 12:44:58 +01:00
_ = HuggingFace.getToken()
2024-01-10 17:40:05 +01:00
}
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-17 02:06:06 +01:00
// let handled = uniNavigator.handle(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-02-17 02:06:06 +01:00
// let handled = uniNavigator.handle(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 {
2024-02-17 02:06:06 +01:00
let cli = Client(server: appAccount!.server, oauthToken: appAccount!.oauthToken)
accountManager.setClient(cli)
uniNavigator.client = cli
// 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
}
}
}
2024-02-20 22:31:44 +01:00
func showNew() {
let lastVersion = UserDefaults.standard.string(forKey: "lastVersion")
if lastVersion == nil || lastVersion != AppInfo.appVersion {
UserDefaults.standard.setValue(AppInfo.appVersion, forKey: "lastVersion")
uniNavigator.presentedSheet = .update
}
}
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()
}