-
Notifications
You must be signed in to change notification settings - Fork 12
/
SettingsView.swift
285 lines (259 loc) · 7.4 KB
/
SettingsView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// SettingsView.swift
// my41
//
// Created by Miroslav Perovic on 28.1.21..
// Copyright © 2021 iPera. All rights reserved.
//
import SwiftUI
struct MODsViewStyle: ViewModifier {
func body(content: Content) -> some View {
return content
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(lineWidth: 2)
.foregroundColor(.white)
)
.shadow(
color: Color.gray.opacity(0.4),
radius: 3, x: 1, y: 2
)
.padding(5)
}
}
struct SettingsView: View {
@EnvironmentObject var calculator: Calculator
@ObservedObject var settingsState = SettingsState()
@State private var selectedCalculator = UserDefaults.standard.string(forKey: hpCalculatorType) ?? HPCalculator.hp41cx.rawValue
@State private var sound = true
@State private var syncTime = true
@State private var module: MOD?
@Binding var showSettings: Bool
@State private var showAlert = false
@State private var showList1 = false
@State private var showList2 = false
@State private var showList3 = false
@State private var showList4 = false
private let mods = MODs.getModFiles()
var body: some View {
GeometryReader { geometry in
VStack(alignment: .leading) {
HStack {
Text("Calculator")
Spacer(minLength: 40)
Picker("Calculator", selection: $selectedCalculator) {
ForEach(HPCalculator.allCases, id: \.self) {
Text($0.rawValue).tag($0.rawValue)
}
}
.pickerStyle(SegmentedPickerStyle())
}
Toggle(isOn: $sound){
Text("Sound")
}
Toggle(isOn: $syncTime){
Text("Synchronyze time with device")
}
Text("MODs")
.padding(.top, 20)
VStack {
HStack {
MODsView(port: .port1, settingsState: settingsState)
.onPress {port in
showList1 = true
}
.sheet(isPresented: $showList1) {
MODList(settingsState: settingsState, showList: $showList1, port: .port1)
}
.modifier(MODsViewStyle())
MODsView(port: .port2, settingsState: settingsState)
.onPress {port in
showList2 = true
}
.sheet(isPresented: $showList2) {
MODList(settingsState: settingsState, showList: $showList2, port: .port2)
}
.modifier(MODsViewStyle())
}
HStack {
MODsView(port: .port3, settingsState: settingsState)
.onPress {port in
showList3 = true
}
.sheet(isPresented: $showList3) {
MODList(settingsState: settingsState, showList: $showList3, port: .port3)
}
.modifier(MODsViewStyle())
MODsView(port: .port4, settingsState: settingsState)
.onPress {port in
showList4 = true
}
.sheet(isPresented: $showList4) {
MODList(settingsState: settingsState, showList: $showList4, port: .port4)
}
.modifier(MODsViewStyle())
}
}
.padding(.top, 10)
.padding(.bottom, 15)
.frame(height: geometry.size.height * 0.455)
HStack {
Spacer()
Button(action: {
showAlert.toggle()
}) {
Text("Reset Calculator")
}
.foregroundColor(.white)
.alert(isPresented: $showAlert) {
Alert(
title: Text("Reset Calculator"),
message: Text("This operation will clear all programs and memory registers"),
primaryButton: .default(Text("Continue")) {
calculator.resetCalculator(restoringMemory: false)
showSettings = false
},
secondaryButton: .cancel(Text("Cancel"))
)
}
Spacer()
}
Spacer()
HStack {
Spacer()
Button(action: {
applyChanges()
}, label: {
Text("Apply")
.fontWeight(.semibold)
})
.foregroundColor(.white)
}
}
.padding(.all)
}
}
private func applyChanges() {
var needsRestart = false
let defaults = UserDefaults.standard
// Sound settings
if sound {
SOUND = true
} else {
SOUND = false
}
defaults.set(SOUND, forKey: "sound")
// Calculator timer
if syncTime {
SYNCHRONYZE = true
} else {
SYNCHRONYZE = false
}
defaults.set(SYNCHRONYZE, forKey: "synchronyzeTime")
// Calculator type
let calculatorType = defaults.string(forKey: hpCalculatorType)
if calculatorType != selectedCalculator {
defaults.set(selectedCalculator, forKey: hpCalculatorType)
needsRestart = true
}
// Modules
if let module1 = settingsState.module1 {
// We have something in Port1
let moduleName = mods.key(from: module1)
if let dModuleName = defaults.string(forKey: HPPort.port1.rawValue) {
// And we had something in Port1 at the begining
if moduleName != dModuleName {
// This is different module
defaults.set(moduleName, forKey: HPPort.port1.rawValue)
needsRestart = true
}
} else {
// Port1 was empty
defaults.set(moduleName, forKey: HPPort.port1.rawValue)
needsRestart = true
}
} else {
// Port1 is empty now
if let _ = defaults.string(forKey: HPPort.port1.rawValue) {
// But we had something in Port1
defaults.removeObject(forKey: HPPort.port1.rawValue)
}
}
if let module2 = settingsState.module2 {
// We have something in Port2
let moduleName = mods.key(from: module2)
if let dModuleName = defaults.string(forKey: HPPort.port2.rawValue) {
// And we had something in Port2 at the begining
if moduleName != dModuleName {
// This is different module
defaults.set(moduleName, forKey: HPPort.port2.rawValue)
needsRestart = true
}
} else {
// Port2 was empty
defaults.set(moduleName, forKey: HPPort.port2.rawValue)
needsRestart = true
}
} else {
// Port2 is empty now
if let _ = defaults.string(forKey: HPPort.port2.rawValue) {
// But we had something in Port2
defaults.removeObject(forKey: HPPort.port2.rawValue)
}
}
if let module3 = settingsState.module3 {
// We have something in Port3
let moduleName = mods.key(from: module3)
if let dModuleName = defaults.string(forKey: HPPort.port3.rawValue) {
// And we had something in Port3 at the begining
if moduleName != dModuleName {
// This is different module
defaults.set(moduleName, forKey: HPPort.port3.rawValue)
needsRestart = true
}
} else {
// Port3 was empty
defaults.set(moduleName, forKey: HPPort.port3.rawValue)
needsRestart = true
}
} else {
// Port3 is empty now
if let _ = defaults.string(forKey: HPPort.port3.rawValue) {
// But we had something in Port3
defaults.removeObject(forKey: HPPort.port3.rawValue)
}
}
if let module3 = settingsState.module3 {
// We have something in Port4
let moduleName = mods.key(from: module3)
if let dModuleName = defaults.string(forKey: HPPort.port4.rawValue) {
// And we had something in Port4 at the begining
if moduleName != dModuleName {
// This is different module
defaults.set(moduleName, forKey: HPPort.port4.rawValue)
needsRestart = true
}
} else {
// Port4 was empty
defaults.set(moduleName, forKey: HPPort.port4.rawValue)
needsRestart = true
}
} else {
// Port4 is empty now
if let _ = defaults.string(forKey: HPPort.port4.rawValue) {
// But we had something in Port4
defaults.removeObject(forKey: HPPort.port4.rawValue)
}
}
defaults.synchronize()
if needsRestart {
calculator.resetCalculator(restoringMemory: true)
}
showSettings = false
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView(showSettings: .constant(true))
}
}