Impressia/Vernissage/VernissageApp.swift

134 lines
5.1 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
2023-01-24 20:38:21 +01:00
import Nuke
import NukeUI
2022-12-28 09:37:16 +01:00
@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
2023-01-23 18:01:27 +01:00
@StateObject var applicationState = ApplicationState.shared
2022-12-31 16:31:05 +01:00
@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
2023-01-23 18:01:27 +01:00
@StateObject private var routerPath = RouterPath()
2022-12-28 09:37:16 +01:00
var body: some Scene {
WindowGroup {
2023-01-23 18:01:27 +01:00
NavigationStack(path: $routerPath.path) {
2022-12-31 16:31:05 +01:00
switch applicationViewMode {
case .loading:
2023-01-06 13:44:02 +01:00
LoadingView()
2023-01-23 18:01:27 +01:00
.withAppRouteur()
.withSheetDestinations(sheetDestinations: $routerPath.presentedSheet)
2022-12-31 16:31:05 +01:00
case .signIn:
SignInView { viewMode in
applicationViewMode = viewMode
}
2023-01-23 18:01:27 +01:00
.withAppRouteur()
.withSheetDestinations(sheetDestinations: $routerPath.presentedSheet)
2022-12-31 16:31:05 +01:00
case .mainView:
2023-01-23 18:01:27 +01:00
MainView()
.withAppRouteur()
.withSheetDestinations(sheetDestinations: $routerPath.presentedSheet)
2022-12-31 16:31:05 +01:00
}
}
2023-01-23 18:01:27 +01:00
.environment(\.managedObjectContext, coreDataHandler.container.viewContext)
.environmentObject(applicationState)
.environmentObject(routerPath)
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 {
2023-01-14 20:45:49 +01:00
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.label
UIPageControl.appearance().pageIndicatorTintColor = UIColor.secondaryLabel
2023-01-24 20:38:21 +01:00
// Set custom configurations for Nuke image/data loaders.
self.setImagePipelines()
2023-01-24 12:22:53 +01:00
2023-01-24 20:38:21 +01:00
// Load user preferences from database.
self.loadUserPreferences()
2023-01-13 13:37:01 +01:00
2023-01-24 20:38:21 +01:00
// Verify access token correctness.
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
2023-01-23 18:01:27 +01:00
Task { @MainActor in
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()
}
2023-01-23 18:01:27 +01:00
.onChange(of: applicationState.theme) { newValue in
self.theme = newValue.colorScheme()
}
.onChange(of: applicationState.tintColor) { newValue in
self.tintColor = newValue.color()
}
2022-12-28 09:37:16 +01:00
}
}
2023-01-24 20:38:21 +01:00
private func loadUserPreferences() {
let defaultSettings = ApplicationSettingsHandler.shared.getDefaultSettings()
if let tintColor = TintColor(rawValue: Int(defaultSettings.tintColor)) {
self.applicationState.tintColor = tintColor
self.tintColor = tintColor.color()
}
if let theme = Theme(rawValue: Int(defaultSettings.theme)) {
self.applicationState.theme = theme
self.theme = theme.colorScheme()
}
if let avatarShape = AvatarShape(rawValue: Int(defaultSettings.avatarShape)) {
self.applicationState.avatarShape = avatarShape
}
}
private func setImagePipelines() {
let pipeline = ImagePipeline {
$0.dataLoader = DataLoader(configuration: {
// Disable disk caching built into URLSession
let conf = DataLoader.defaultConfiguration
conf.urlCache = nil
return conf
}())
$0.imageCache = ImageCache.shared
$0.dataCache = try! DataCache(name: "dev.mczachurski.Vernissage.DataCache")
}
ImagePipeline.shared = pipeline
}
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
}