I'm trying to implement a simple toggle switch but I'm having trouble saving the new toggle/switch status as when I change views and go back into the setting, it's defaulted back to the off switch. Can you tell me what I'm doing wrong?
struct StudyMode: View {
@State private var overdueFirst = UserDefaults.standard.bool(forKey: "Overdue First")
@EnvironmentObject var settings: UserSettings
var body: some View {
VStack {
HStack {
Toggle(isOn: $overdueFirst) {
Text("Overdue cards first")
}
.onTapGesture {
var newSwitch = false
if self.overdueFirst == false {
newSwitch = true
}
UserDefaults.standard.set(newSwitch, forKey: "Overdue First")
}
}
Spacer()
Text("By enabling this option, the cards will be ordered such that you will revise all overdue cards before you start learning new words.")
.font(.system(size: 12))
}
}
}