Impressia/Vernissage/VernissageApp.swift

88 lines
3.4 KiB
Swift
Raw Normal View History

2022-12-28 09:37:16 +01:00
//
// https://mczachurski.dev
// Copyright © 2022 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
@main
2023-01-03 14:09:22 +01:00
struct VernissageApp: App {
2022-12-31 16:31:05 +01:00
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let coreDataHandler = CoreDataHandler.shared
let applicationState = ApplicationState.shared
@State var applicationViewMode: ApplicationViewMode = .loading
2023-01-12 18:34:48 +01:00
@State var tintColor = ApplicationState.shared.tintColor.color()
2023-01-13 13:37:01 +01:00
@State var theme = ApplicationState.shared.theme.colorScheme()
2022-12-30 18:20:54 +01:00
2022-12-28 09:37:16 +01:00
var body: some Scene {
WindowGroup {
2022-12-29 17:27:15 +01:00
NavigationStack {
2022-12-31 16:31:05 +01:00
switch applicationViewMode {
case .loading:
2023-01-06 13:44:02 +01:00
LoadingView()
2022-12-31 16:31:05 +01:00
case .signIn:
SignInView { viewMode in
applicationViewMode = viewMode
}
.environment(\.managedObjectContext, coreDataHandler.container.viewContext)
.environmentObject(applicationState)
case .mainView:
2023-01-12 18:34:48 +01:00
MainView { color in
self.tintColor = color.color()
2023-01-13 13:37:01 +01:00
} onThemeChange: { theme in
self.theme = theme.colorScheme()
2023-01-12 18:34:48 +01:00
}
.environment(\.managedObjectContext, coreDataHandler.container.viewContext)
.environmentObject(applicationState)
2022-12-31 16:31:05 +01:00
}
}
2023-01-12 18:34:48 +01:00
.tint(self.tintColor)
2023-01-13 13:37:01 +01:00
.preferredColorScheme(self.theme)
2023-01-12 18:34:48 +01:00
.task {
let defaultSettings = ApplicationSettingsHandler.shared.getDefaultSettings()
2023-01-13 13:37:01 +01:00
2023-01-12 18:34:48 +01:00
if let tintColor = TintColor(rawValue: Int(defaultSettings.tintColor)) {
self.applicationState.tintColor = tintColor
self.tintColor = tintColor.color()
}
2023-01-13 13:37:01 +01:00
if let theme = Theme(rawValue: Int(defaultSettings.theme)) {
self.applicationState.theme = theme
self.theme = theme.colorScheme()
}
2023-01-03 14:09:22 +01:00
await AuthorizationService.shared.verifyAccount({ accountData in
guard let accountData = accountData else {
self.applicationViewMode = .signIn
return
}
2022-12-31 16:31:05 +01:00
self.applicationState.accountData = accountData
2023-01-03 14:09:22 +01:00
self.applicationViewMode = .mainView
})
2022-12-29 17:27:15 +01:00
}
.navigationViewStyle(.stack)
2023-01-06 13:05:21 +01:00
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
try? HapticService.shared.start()
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in
HapticService.shared.stop()
}
2022-12-28 09:37:16 +01:00
}
}
2022-12-31 16:31:05 +01:00
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let sceneConfig: UISceneConfiguration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfig.delegateClass = SceneDelegate.self
return sceneConfig
}
2022-12-28 09:37:16 +01:00
}