IceCubes/IceCubesApp/App/IceCubesApp.swift

89 lines
2.6 KiB
Swift
Raw Normal View History

2022-12-01 09:05:26 +01:00
import SwiftUI
import Timeline
import Network
import KeychainSwift
2022-12-22 10:53:36 +01:00
import Env
2022-12-24 14:55:04 +01:00
import DesignSystem
2022-12-01 09:05:26 +01:00
@main
struct IceCubesApp: App {
2022-12-17 13:37:46 +01:00
public static let defaultServer = "mastodon.social"
2022-12-24 14:55:04 +01:00
@Environment(\.scenePhase) private var scenePhase
2022-12-01 09:05:26 +01:00
@StateObject private var appAccountsManager = AppAccountsManager()
@StateObject private var currenInstance = CurrentInstance()
2022-12-22 11:19:56 +01:00
@StateObject private var currentAccount = CurrentAccount()
@StateObject private var watcher = StreamWatcher()
2022-12-22 10:53:36 +01:00
@StateObject private var quickLook = QuickLook()
2022-12-24 14:55:04 +01:00
@StateObject private var theme = Theme()
2022-12-24 11:50:05 +01:00
@State private var selectedTab: Tab = .timeline
@State private var popToRootTab: Tab = .other
2022-12-01 09:05:26 +01:00
var body: some Scene {
WindowGroup {
2022-12-24 11:50:05 +01:00
TabView(selection: .init(get: {
selectedTab
}, set: { newTab in
if newTab == selectedTab {
/// Stupid hack to trigger onChange binding in tab views.
popToRootTab = .other
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
popToRootTab = selectedTab
}
}
selectedTab = newTab
})) {
2022-12-27 09:25:26 +01:00
ForEach(appAccountsManager.currentClient.isAuth ? Tab.loggedInTabs() : Tab.loggedOutTab()) { tab in
tab.makeContentView(popToRootTab: $popToRootTab)
.tabItem {
2022-12-27 09:25:26 +01:00
tab.label
.badge(tab == .notifications ? watcher.unreadNotificationsCount : 0)
}
2022-12-27 09:25:26 +01:00
.tag(tab)
2022-12-20 15:37:51 +01:00
}
2022-12-01 09:05:26 +01:00
}
2022-12-24 14:55:04 +01:00
.tint(theme.tintColor)
2022-12-22 11:19:56 +01:00
.onChange(of: appAccountsManager.currentClient) { newClient in
2022-12-25 17:39:12 +01:00
setNewClientsInEnv(client: newClient)
if newClient.isAuth {
watcher.watch(stream: .user)
}
2022-12-22 11:19:56 +01:00
}
.onAppear {
2022-12-25 17:39:12 +01:00
setNewClientsInEnv(client: appAccountsManager.currentClient)
2022-12-22 11:19:56 +01:00
}
2022-12-01 09:05:26 +01:00
.environmentObject(appAccountsManager)
.environmentObject(appAccountsManager.currentClient)
2022-12-22 10:53:36 +01:00
.environmentObject(quickLook)
2022-12-22 11:19:56 +01:00
.environmentObject(currentAccount)
.environmentObject(currenInstance)
2022-12-24 14:55:04 +01:00
.environmentObject(theme)
.environmentObject(watcher)
2022-12-22 11:19:56 +01:00
.quickLookPreview($quickLook.url, in: quickLook.urls)
2022-12-01 09:05:26 +01:00
}
.onChange(of: scenePhase, perform: { scenePhase in
2022-12-25 17:39:12 +01:00
handleScenePhase(scenePhase: scenePhase)
})
2022-12-01 09:05:26 +01:00
}
2022-12-25 17:39:12 +01:00
private func setNewClientsInEnv(client: Client) {
currentAccount.setClient(client: client)
currenInstance.setClient(client: client)
2022-12-25 17:39:12 +01:00
watcher.setClient(client: client)
}
private func handleScenePhase(scenePhase: ScenePhase) {
switch scenePhase {
case .background:
watcher.stopWatching()
case .active:
watcher.watch(stream: .user)
case .inactive:
break
default:
break
}
}
2022-12-01 09:05:26 +01:00
}