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-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-12 18:34:48 +01:00
|
|
|
|
|
|
|
var onTintChange: ((TintColor) -> Void)?
|
2023-01-13 13:37:01 +01:00
|
|
|
var onThemeChange: ((Theme) -> Void)?
|
|
|
|
|
2023-01-11 13:16:43 +01:00
|
|
|
var body: some View {
|
|
|
|
NavigationView {
|
|
|
|
List {
|
2023-01-13 13:37:01 +01:00
|
|
|
// Accounts.
|
|
|
|
AccountsSection()
|
|
|
|
|
|
|
|
// Themes.
|
|
|
|
ThemeSection { theme in
|
|
|
|
changeTheme(theme: theme)
|
2023-01-12 18:34:48 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 13:37:01 +01:00
|
|
|
// Accents.
|
|
|
|
AccentsSection { color in
|
|
|
|
self.onTintChange?(color)
|
2023-01-12 18:34:48 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 13:37:01 +01:00
|
|
|
// Other.
|
2023-01-12 18:34:48 +01:00
|
|
|
Section("Other") {
|
|
|
|
Text("Third party") // Link to dependeinces
|
|
|
|
Text("Report a bug")
|
|
|
|
Text("Follow me on Mastodon")
|
2023-01-11 13:16:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 13:37:01 +01:00
|
|
|
// Version.
|
2023-01-12 18:34:48 +01:00
|
|
|
Section() {
|
2023-01-13 13:37:01 +01:00
|
|
|
HStack {
|
|
|
|
Text("Version")
|
|
|
|
Spacer()
|
2023-01-14 08:52:51 +01:00
|
|
|
Text(appVersion ?? String.empty())
|
2023-01-13 13:37:01 +01:00
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
}
|
2023-01-11 13:16:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(alignment: .topLeading)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
|
|
Button("Close", role: .cancel) {
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.task {
|
2023-01-13 13:37:01 +01:00
|
|
|
self.appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
|
2023-01-11 13:16:43 +01:00
|
|
|
}
|
2023-01-13 13:37:01 +01:00
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification), perform: { _ in
|
|
|
|
self.theme = applicationState.theme.colorScheme() ?? self.getSystemColorScheme()
|
|
|
|
})
|
2023-01-11 13:16:43 +01:00
|
|
|
.navigationBarTitle(Text("Settings"), displayMode: .inline)
|
2023-01-13 13:37:01 +01:00
|
|
|
.preferredColorScheme(self.theme)
|
2023-01-11 13:16:43 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-13 13:37:01 +01:00
|
|
|
|
|
|
|
private func changeTheme(theme: Theme) {
|
|
|
|
// Change theme of current modal screen (unformtunatelly it's not changed autmatically_
|
|
|
|
self.theme = theme.colorScheme() ?? self.getSystemColorScheme()
|
|
|
|
|
|
|
|
self.applicationState.theme = theme
|
|
|
|
ApplicationSettingsHandler.shared.setDefaultTheme(theme: theme)
|
|
|
|
onThemeChange?(theme)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSystemColorScheme() -> ColorScheme {
|
|
|
|
return UITraitCollection.current.userInterfaceStyle == .light ? .light : .dark
|
|
|
|
}
|
2023-01-11 13:16:43 +01:00
|
|
|
}
|