IceCubesApp/IceCubesApp/App/IceCubesApp.swift

133 lines
3.8 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()
2023-01-01 17:31:23 +00:00
@StateObject private var currentInstance = 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()
@State private var selectedTab: Tab? = .timeline
2022-12-24 10:50:05 +00:00
@State private var popToRootTab: Tab = .other
private var availableTabs: [Tab] {
appAccountsManager.currentClient.isAuth ? Tab.loggedInTabs() : Tab.loggedOutTab()
}
2022-12-01 08:05:26 +00:00
var body: some Scene {
WindowGroup {
appView
2022-12-24 13:55:04 +00:00
.tint(theme.tintColor)
2022-12-22 10:19:56 +00:00
.onAppear {
2022-12-25 16:39:12 +00:00
setNewClientsInEnv(client: appAccountsManager.currentClient)
2022-12-29 09:39:34 +00:00
setBarsColor(color: theme.primaryBackgroundColor)
2022-12-22 10:19:56 +00:00
}
.preferredColorScheme(theme.selectedScheme == ColorScheme.dark ? .dark : .light)
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)
2023-01-01 17:31:23 +00:00
.environmentObject(currentInstance)
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-29 09:39:34 +00:00
.onChange(of: appAccountsManager.currentClient) { newClient in
setNewClientsInEnv(client: newClient)
if newClient.isAuth {
watcher.watch(stream: .user)
}
}
.onChange(of: theme.primaryBackgroundColor) { newValue in
setBarsColor(color: newValue)
}
2022-12-01 08:05:26 +00:00
}
2022-12-25 16:39:12 +00:00
@ViewBuilder
private var appView: some View {
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
splitView
} else {
tabBarView
}
}
private var tabBarView: some View {
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) {
if let selectedTab {
popToRootTab = selectedTab
}
}
}
selectedTab = newTab
})) {
ForEach(availableTabs) { tab in
tab.makeContentView(popToRootTab: $popToRootTab)
.tabItem {
tab.label
}
.tag(tab)
.badge(tab == .notifications ? watcher.unreadNotificationsCount : 0)
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .tabBar)
}
}
}
private var splitView: some View {
NavigationSplitView {
List(availableTabs, selection: $selectedTab) { tab in
NavigationLink(value: tab) {
tab.label
}
}
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
} detail: {
selectedTab?.makeContentView(popToRootTab: $popToRootTab)
}
}
2022-12-25 16:39:12 +00:00
private func setNewClientsInEnv(client: Client) {
currentAccount.setClient(client: client)
2023-01-01 17:31:23 +00:00
currentInstance.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-29 09:39:34 +00:00
private func setBarsColor(color: Color) {
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().barTintColor = UIColor(color)
}
2022-12-01 08:05:26 +00:00
}