Impressia/Vernissage/Views/SettingsView.swift

98 lines
3.5 KiB
Swift
Raw Normal View History

2023-01-11 13:16:43 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
struct SettingsView: View {
@EnvironmentObject var applicationState: ApplicationState
2023-02-13 21:10:07 +01:00
@EnvironmentObject var routerPath: RouterPath
2023-02-14 07:32:00 +01:00
@EnvironmentObject var tipsStore: TipsStore
2023-02-13 21:10:07 +01:00
2023-01-13 13:37:01 +01:00
@Environment(\.colorScheme) var colorScheme
2023-01-11 13:16:43 +01:00
@Environment(\.dismiss) private var dismiss
2023-01-13 13:37:01 +01:00
@State private var theme: ColorScheme?
@State private var appVersion: String?
2023-01-23 11:42:28 +01:00
@State private var appBundleVersion: String?
2023-01-13 13:37:01 +01:00
2023-01-11 13:16:43 +01:00
var body: some View {
2023-01-23 18:01:27 +01:00
NavigationStack {
NavigationView {
List {
// Accounts.
AccountsSection()
// Themes.
2023-01-24 12:22:53 +01:00
ThemeSection()
2023-01-23 18:01:27 +01:00
// Accents.
2023-01-24 12:22:53 +01:00
AccentsSection()
// Avatar shapes.
AvatarShapesSection()
2023-01-23 18:01:27 +01:00
2023-02-08 21:09:51 +01:00
// Support.
SupportView()
2023-01-23 18:01:27 +01:00
// Other.
2023-02-08 21:09:51 +01:00
OtherSection()
2023-01-23 18:01:27 +01:00
// Version.
2023-02-08 21:09:51 +01:00
self.version()
2023-01-11 13:16:43 +01:00
}
2023-01-23 18:01:27 +01:00
.frame(alignment: .topLeading)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Close", role: .cancel) {
dismiss()
}
2023-01-11 13:16:43 +01:00
}
}
2023-01-23 18:01:27 +01:00
.task {
self.appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
self.appBundleVersion = Bundle.main.infoDictionary?["CFBundleVersion"] as? String
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification), perform: { _ in
self.theme = applicationState.theme.colorScheme() ?? self.getSystemColorScheme()
})
.navigationBarTitle(Text("Settings"), displayMode: .inline)
.preferredColorScheme(self.theme)
2023-01-11 13:16:43 +01:00
}
2023-01-23 18:01:27 +01:00
.withAppRouteur()
2023-02-13 21:10:07 +01:00
.withOverlayDestinations(overlayDestinations: $routerPath.presentedOverlay)
2023-01-11 13:16:43 +01:00
}
2023-01-24 12:22:53 +01:00
.onChange(of: self.applicationState.theme) { newValue in
// Change theme of current modal screen (unformtunatelly it's not changed autmatically.
self.theme = self.applicationState.theme.colorScheme() ?? self.getSystemColorScheme()
}
2023-02-14 07:32:00 +01:00
.onChange(of: tipsStore.status) { status in
2023-02-13 21:10:07 +01:00
if status == .successful {
withAnimation(.spring()) {
self.routerPath.presentedOverlay = .successPayment
2023-02-14 07:32:00 +01:00
self.tipsStore.reset()
2023-02-13 21:10:07 +01:00
}
}
}
2023-02-14 07:32:00 +01:00
.alert(isPresented: $tipsStore.hasError, error: tipsStore.error) { }
2023-01-11 13:16:43 +01:00
}
2023-02-08 21:09:51 +01:00
@ViewBuilder
private func version() -> some View {
Section() {
HStack {
Text("Version")
Spacer()
Text("\(appVersion ?? String.empty()) (\(appBundleVersion ?? String.empty()))")
.foregroundColor(.accentColor)
}
}
}
2023-01-24 12:22:53 +01:00
2023-02-08 21:09:51 +01:00
private func getSystemColorScheme() -> ColorScheme {
2023-01-13 13:37:01 +01:00
return UITraitCollection.current.userInterfaceStyle == .light ? .light : .dark
}
2023-01-11 13:16:43 +01:00
}