2023-12-29 11:17:37 +01:00
|
|
|
//Made by Lumaa
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-02-04 08:43:56 +01:00
|
|
|
///
|
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-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 {
|
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)
|
|
|
|
|
2024-02-10 02:22:39 +01:00
|
|
|
DiscoveryView()
|
2024-02-04 08:43:56 +01:00
|
|
|
.background(Color.appBackground)
|
|
|
|
.tag(TabDestination.search)
|
|
|
|
|
|
|
|
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-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
|
|
|
}
|
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-18 12:44:58 +01:00
|
|
|
.environment(huggingFace)
|
2024-02-04 08:43:56 +01:00
|
|
|
.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 {
|
2024-02-20 22:31:51 +01:00
|
|
|
//TODO: Like AccMan > .static
|
2024-01-21 13:18:25 +01:00
|
|
|
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 }
|
2024-02-04 10:44:51 +01:00
|
|
|
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
|
|
|
})
|
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-17 02:06:06 +01:00
|
|
|
let cli = Client(server: appAccount!.server, oauthToken: appAccount!.oauthToken)
|
|
|
|
accountManager.setClient(cli)
|
|
|
|
uniNavigator.client = cli
|
2024-02-04 08:43:56 +01:00
|
|
|
|
|
|
|
// 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
|
2024-01-02 14:23:36 +01:00
|
|
|
UINavigationBar.appearance().tintColor = UIColor.label
|
2023-12-29 11:17:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
ContentView()
|
|
|
|
}
|