Files
Reflect/Shared/views/SettingsView/SettingsView.swift
2022-02-20 14:33:58 -06:00

228 lines
7.3 KiB
Swift

//
// SettingsView.swift
// Feels
//
// Created by Trey Tartt on 1/8/22.
//
import SwiftUI
import CloudKitSyncMonitor
struct SettingsView: View {
@Environment(\.dismiss) var dismiss
let editedDataClosure: (() -> Void)
@State private var showOnboarding = false
@State private var showSpecialThanks = false
@State private var showWhyBGMode = false
@ObservedObject var syncMonitor = SyncMonitor.shared
@AppStorage(UserDefaultsStore.Keys.useCloudKit.rawValue, store: GroupUserDefaults.groupDefaults) private var useCloudKit = false
@AppStorage(UserDefaultsStore.Keys.deleteEnable.rawValue, store: GroupUserDefaults.groupDefaults) private var deleteEnabled = true
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
var body: some View {
ScrollView {
VStack {
Group {
closeButtonView
.padding()
cloudKitEnable
canDelete
showOnboardingButton
whyBackgroundMode
specialThanksCell
}
Group {
addTestDataCell
clearDB
if useCloudKit {
cloudKitStatus
}
}
Spacer()
}
.padding()
}.sheet(isPresented: $showOnboarding) {
OnboardingMain(onboardingData: UserDefaultsStore.getOnboarding(),
updateBoardingDataClosure: { onboardingData in
OnboardingDataDataManager.shared.updateOnboardingData(onboardingData: onboardingData)
showOnboarding = false
})
}
.background(
theme.currentTheme.bg
.edgesIgnoringSafeArea(.all)
)
}
private var closeButtonView: some View {
HStack{
Spacer()
Button(action: {
dismiss()
}, label: {
Text(String(localized: "settings_view_exit"))
.font(.body)
.foregroundColor(Color(UIColor.systemBlue))
})
}
}
private var specialThanksCell: some View {
ZStack {
theme.currentTheme.secondaryBGColor
VStack {
Button(action: {
withAnimation{
showSpecialThanks.toggle()
}
}, label: {
Text(String(localized: "settings_view_special_thanks_to_title"))
})
.padding()
if showSpecialThanks {
Text(String(localized: "settings_view_special_thanks_to_body"))
.padding()
}
}
}
.frame(minWidth: 0, maxWidth: .infinity)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
private var addTestDataCell: some View {
ZStack {
theme.currentTheme.secondaryBGColor
Button(action: {
PersistenceController.shared.populateTestData()
editedDataClosure()
}, label: {
Text("Add test data")
})
.padding()
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
private var clearDB: some View {
ZStack {
theme.currentTheme.secondaryBGColor
Button(action: {
PersistenceController.shared.clearDB()
editedDataClosure()
}, label: {
Text("Clear DB")
})
.padding()
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
private var whyBackgroundMode: some View {
ZStack {
theme.currentTheme.secondaryBGColor
VStack {
Button(action: {
withAnimation{
showWhyBGMode.toggle()
}
}, label: {
Text(String(localized: "settings_view_why_bg_mode_title"))
})
.padding()
if showWhyBGMode {
Text(String(localized: "settings_view_why_bg_mode_body"))
.padding()
}
}
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
private var showOnboardingButton: some View {
ZStack {
theme.currentTheme.secondaryBGColor
Button(action: {
showOnboarding.toggle()
}, label: {
Text(String(localized: "settings_view_show_onboarding"))
})
.padding()
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
private var cloudKitEnable: some View {
ZStack {
theme.currentTheme.secondaryBGColor
VStack {
Toggle(String(localized: "settings_use_cloudkit_title"),
isOn: $useCloudKit)
.onChange(of: useCloudKit) { value in
PersistenceController.shared.switchContainer()
}
.padding()
Text(String(localized: "settings_use_cloudkit_body"))
}
.padding(.bottom)
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
private var cloudKitStatus: some View {
ZStack {
theme.currentTheme.secondaryBGColor
VStack {
Image(systemName: syncMonitor.syncStateSummary.symbolName)
.foregroundColor(syncMonitor.syncStateSummary.symbolColor)
Text( syncMonitor.syncStateSummary.isBroken ? "cloudkit is broken" : "cloudkit is good")
}
.padding()
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
private var canDelete: some View {
ZStack {
theme.currentTheme.secondaryBGColor
VStack {
Toggle(String(localized: "settings_use_delete_enable"),
isOn: $deleteEnabled)
.padding()
}
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView(editedDataClosure: {
})
SettingsView(editedDataClosure: {
})
.preferredColorScheme(.dark)
}
}