IceCubesApp/IceCubesApp/App/IceCubesApp.swift

89 lines
2.6 KiB
Swift
Raw Normal View History

2022-12-01 08:05:26 +00:00
import SwiftUI
import Timeline
import Network
import KeychainSwift
2022-12-22 09:53:36 +00:00
import Env
2022-12-24 13:55:04 +00:00
import DesignSystem
2022-12-01 08:05:26 +00:00
@main
struct IceCubesApp: App {
2022-12-17 12:37:46 +00:00
public static let defaultServer = "mastodon.social"
2022-12-24 13:55:04 +00:00
@Environment(\.scenePhase) private var scenePhase
2022-12-01 08:05:26 +00:00
@StateObject private var appAccountsManager = AppAccountsManager()
@StateObject private var currenInstance = CurrentInstance()
2022-12-22 10:19:56 +00:00
@StateObject private var currentAccount = CurrentAccount()
@StateObject private var watcher = StreamWatcher()
2022-12-22 09:53:36 +00:00
@StateObject private var quickLook = QuickLook()
2022-12-24 13:55:04 +00:00
@StateObject private var theme = Theme()
2022-12-24 10:50:05 +00:00
@State private var selectedTab: Tab = .timeline
@State private var popToRootTab: Tab = .other
2022-12-01 08:05:26 +00:00
var body: some Scene {
WindowGroup {
2022-12-24 10:50:05 +00: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 08:25:26 +00:00
ForEach(appAccountsManager.currentClient.isAuth ? Tab.loggedInTabs() : Tab.loggedOutTab()) { tab in
tab.makeContentView(popToRootTab: $popToRootTab)
.tabItem {
2022-12-27 08:25:26 +00:00
tab.label
}
2022-12-27 08:25:26 +00:00
.tag(tab)
.badge(tab == .notifications ? watcher.unreadNotificationsCount : 0)
2022-12-20 14:37:51 +00:00
}
2022-12-01 08:05:26 +00:00
}
2022-12-24 13:55:04 +00:00
.tint(theme.tintColor)
2022-12-22 10:19:56 +00:00
.onChange(of: appAccountsManager.currentClient) { newClient in
2022-12-25 16:39:12 +00:00
setNewClientsInEnv(client: newClient)
if newClient.isAuth {
watcher.watch(stream: .user)
}
2022-12-22 10:19:56 +00:00
}
.onAppear {
2022-12-25 16:39:12 +00:00
setNewClientsInEnv(client: appAccountsManager.currentClient)
2022-12-22 10:19:56 +00:00
}
2022-12-01 08:05:26 +00:00
.environmentObject(appAccountsManager)
.environmentObject(appAccountsManager.currentClient)
2022-12-22 09:53:36 +00:00
.environmentObject(quickLook)
2022-12-22 10:19:56 +00:00
.environmentObject(currentAccount)
.environmentObject(currenInstance)
2022-12-24 13:55:04 +00:00
.environmentObject(theme)
.environmentObject(watcher)
2022-12-22 10:19:56 +00:00
.quickLookPreview($quickLook.url, in: quickLook.urls)
2022-12-01 08:05:26 +00:00
}
.onChange(of: scenePhase, perform: { scenePhase in
2022-12-25 16:39:12 +00:00
handleScenePhase(scenePhase: scenePhase)
})
2022-12-01 08:05:26 +00:00
}
2022-12-25 16:39:12 +00:00
private func setNewClientsInEnv(client: Client) {
currentAccount.setClient(client: client)
currenInstance.setClient(client: client)
2022-12-25 16:39:12 +00: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 08:05:26 +00:00
}