Impressia/Vernissage/Views/SettingsView/SettingsView.swift

110 lines
4.0 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.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-11 13:16:43 +01:00
//
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-03-18 18:40:07 +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.
2023-02-21 07:05:06 +01:00
AccountsSectionView()
2023-03-18 18:40:07 +01:00
// General.
GeneralSectionView()
2023-01-23 18:01:27 +01:00
// Accents.
2023-02-21 07:05:06 +01:00
AccentsSectionView()
2023-01-24 12:22:53 +01:00
// Avatar shapes.
2023-02-21 07:05:06 +01:00
AvatarShapesSectionView()
2023-01-23 18:01:27 +01:00
2023-03-05 09:53:06 +01:00
// Media settings view.
MediaSettingsView()
// Haptics.
HapticsSectionView()
2023-02-08 21:09:51 +01:00
// Support.
SupportView()
2023-01-23 18:01:27 +01:00
// Other.
2023-02-21 07:05:06 +01:00
OtherSectionView()
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) {
2023-03-13 13:53:36 +01:00
Button(NSLocalizedString("settings.title.close", comment: "Close"), role: .cancel) {
2023-02-23 08:09:02 +01:00
self.dismiss()
2023-01-23 18:01:27 +01:00
}
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()
})
2023-03-13 13:53:36 +01:00
.navigationTitle("settings.navigationBar.title")
2023-02-21 08:36:14 +01:00
.navigationBarTitleDisplayMode(.inline)
2023-01-23 18:01:27 +01:00
.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-23 08:09:02 +01:00
.onChange(of: applicationState.account) { newValue in
if newValue == nil {
self.dismiss()
}
}
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 {
2023-03-13 13:53:36 +01:00
Text("settings.title.version", comment: "Version")
2023-02-08 21:09:51 +01:00
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
}