Files
Reflect/Shared/views/SettingsView.swift
Trey t 55bcb460ba create contentview view model
add background to content view
make contentview list split by year / month
test data will add 120 days instead of 25

closed #35
closed #36
2022-01-20 16:12:49 -06:00

212 lines
6.4 KiB
Swift

//
// SettingsView.swift
// Feels
//
// Created by Trey Tartt on 1/8/22.
//
import SwiftUI
struct SettingsView: View {
@Environment(\.dismiss) var dismiss
let editedDataClosure: (() -> Void)
@AppStorage("notificationDate") private var notificationDate = Date() {
didSet {
if self.showReminder {
LocalNotification.scheduleReminder(atTime: self.notificationDate)
}
}
}
@AppStorage("showReminder") private var showReminder: Bool = false
var body: some View {
ZStack {
Color(UIColor.secondarySystemBackground)
VStack {
closeButtonView
.padding()
notificationCell
randomShitCell
addTestDataCell
clearDB
whyBackgroundMode
changeIcon
Spacer()
}
.padding()
}
}
private var closeButtonView: some View {
HStack{
Spacer()
Button(action: {
dismiss()
}, label: {
Text("Exit")
.font(.body)
.foregroundColor(Color(UIColor.systemBlue))
})
}
}
private var notificationCell: some View {
ZStack {
Color(UIColor.systemBackground)
VStack {
Toggle("Would you like to be reminded?", isOn: $showReminder)
.onChange(of: showReminder, perform: { value in
self.maybeNotificaiotns(areEnabled: value)
})
.padding()
DatePicker("", selection: $notificationDate, displayedComponents: .hourAndMinute)
.onChange(of: notificationDate, perform: { value in
self.updateNotificationTimes(toDate: value)
})
.disabled(showReminder == false)
.padding()
}
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
}
private var randomShitCell: some View {
ZStack {
Color(UIColor.systemBackground)
VStack {
Button(action: {
}, label: {
Text("Special thanks to")
})
.padding()
}
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
}
private var addTestDataCell: some View {
ZStack {
Color(UIColor.systemBackground)
Button(action: {
PersistenceController.shared.populateTestData()
editedDataClosure()
}, label: {
Text("Add test data")
})
.padding()
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
}
private var clearDB: some View {
ZStack {
Color(UIColor.systemBackground)
Button(action: {
PersistenceController.shared.clearDB()
editedDataClosure()
}, label: {
Text("Clear DB")
})
.padding()
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
}
private var whyBackgroundMode: some View {
ZStack {
Color(UIColor.systemBackground)
Text("we do bg mode b/c we can")
.padding()
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
}
let iconSets: [(String,String)] = [
("PurpleFeelsAppIcon", "PurpleAppIcon"),
("RedFeelsAppIcon", "RedAppIcon")
]
private var changeIcon: some View {
ZStack {
Color(UIColor.systemBackground)
VStack {
Text("Change Icon")
HStack {
Button(action: {
UIApplication.shared.setAlternateIconName(nil)
}, label: {
Image("FeelsAppIcon", bundle: .main)
.resizable()
.frame(width: 50, height:50)
.cornerRadius(10)
})
.padding()
ForEach(iconSets, id: \.self.0){ iconSet in
Button(action: {
UIApplication.shared.setAlternateIconName(iconSet.1) { (error) in
// FIXME: Handle error
}
}, label: {
Image(iconSet.0, bundle: .main)
.resizable()
.frame(width: 50, height:50)
.cornerRadius(10)
})
.padding()
}
}
}
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
}
private func updateNotificationTimes(toDate date: Date) {
LocalNotification.removeNotificaiton()
LocalNotification.scheduleReminder(atTime: date)
}
private func maybeNotificaiotns(areEnabled: Bool) {
if areEnabled {
LocalNotification.testIfEnabled(completion: { result in
switch result{
case .success(_):
LocalNotification.scheduleReminder(atTime: self.notificationDate)
case .failure(let error):
print(error)
// show error
break
}
})
} else {
LocalNotification.removeNotificaiton()
}
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView(editedDataClosure: {
})
SettingsView(editedDataClosure: {
})
.preferredColorScheme(.dark)
}
}