-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditCardView.swift
277 lines (245 loc) · 10.8 KB
/
EditCardView.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
//
// EditCardView.swift
// Shoo
//
// Created by Alexander Schiff on 5/19/20.
// Copyright © 2020 Alexander Schiff. All rights reserved.
//
import SwiftUI
import Foundation
struct EditCardView: View {
@State private var tapped = false
@EnvironmentObject var fire: Fire
@Environment(\.presentationMode) var presentationMode
//time variables
@State private var time: Double = Date().timeIntervalSince1970
//@State private var selection: Int = 0
@State private var defaultEnd: Double = Date().timeIntervalSince1970 //default end is now
@State private var start: Double = Date().timeIntervalSince1970 //start is always now
@State private var addReason = ""
@State private var reasons = ["👩💻 Working", "📺 Watching TV", "🏃♂️ Exercising", "📱 On the phone"]
@State private var newStatus: Status = .green
@State private var newReason: String = ""
@State private var custom = false
@State private var customColor: Color = Color.gray
func addReasonFunc() {
let nR = addReason.trimmingCharacters(in: .whitespacesAndNewlines)
guard nR.count > 0 else {
addReason = ""
return
}
if let i = reasons.firstIndex(of: nR) {
self.reasons.remove(at: i)
reasons.insert(nR, at: 0)
} else {
reasons.insert(nR, at: 0)
self.fire.saveCustomReasons(reasons: reasons)
}
newReason = nR
addReason = ""
}
var displayTime: String{
switch self.fire.timeSelection{
case 0:
return "10 minutes"
case 1:
return "15 minutes"
case 2:
return "20 minutes"
case 3:
return "30 minutes"
case 4:
return "45 minutes"
case 5:
return "1 hour"
case 6:
return "2 hours"
case 7:
return "3 hours"
case 8:
return "4 hours"
case 9:
return "A while"
case 10:
return "All day"
default:
return "A while"
}
}
@ObservedObject private var keyboard = KeyboardResponder()
var body: some View {
VStack(alignment: .leading){
HandleView()
Spacer()
// MARK: - Person View
PersonView(id: self.fire.profile.uid, name: self.fire.profile.name, reason: self.newReason, status: self.newStatus, end: self.time, start: self.start, pushToken: self.fire.profile.pushToken).environmentObject(fire)
.shadow(radius: 3, y: 1)
.highPriorityGesture(TapGesture()) //to override tap expansion
Spacer()
// MARK: - Statuses
VStack(alignment: .leading, spacing: 10){
Text("Your status")
.font(.headline)
HStack{
Button("Free"){
self.newStatus = .green
buttonPressHaptic(self.fire.reduceHaptics)
}
.buttonStyle(StatusButtonStyle(color: Color.green, selected: newStatus == .green))
Button("Quiet"){
self.newStatus = .yellow
buttonPressHaptic(self.fire.reduceHaptics)
}
.buttonStyle(StatusButtonStyle(color: Color.yellow, selected: newStatus == .yellow))
Button("Shoo"){
self.newStatus = .red
buttonPressHaptic(self.fire.reduceHaptics)
}
.buttonStyle(StatusButtonStyle(color: Color.red, selected: newStatus == .red))
Spacer()
}
.font(.headline)
.foregroundColor(Color(UIColor.systemBackground))
}
.padding()
// MARK: - REASONS
VStack(alignment: .leading){
Text("What's happening")
.font(.headline)
.padding(.leading)
ScrollView(.horizontal, showsIndicators: false){
HStack(alignment: .center, spacing: 0){
ZStack{
TextField("+ Custom", text: $addReason, onEditingChanged: {_ in self.customColor = Color.blue }, onCommit: {
self.customColor = Color.gray
self.addReasonFunc()
})
.textFieldStyle(PlainTextFieldStyle())
.font(.headline)
.padding(14)
.padding(.trailing, 40)
.background(Color.gray.opacity(0.4))
//.overlay(RoundedRectangle(cornerRadius: 20, style: .continuous).stroke(Color.blue, lineWidth: 2))
.clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous))
//.shadow(radius: 2, y: 2)
.disableAutocorrection(false)
.padding([.leading, .bottom])
HStack{
Spacer()
Button(action: {
buttonPressHaptic(self.fire.reduceHaptics)
UIApplication.shared.endEditing() // Call to dismiss keyboard
self.customColor = Color.gray
self.addReasonFunc()
}){
Image(systemName: "checkmark.circle")
.padding([.trailing, .bottom])
.font(.title)
.foregroundColor(customColor)
}
.padding(.leading, 20)
}
}
.fixedSize(horizontal: true, vertical: true)
.padding(.top, 5)
ForEach(reasons, id: \.self){ reason in
Text(reason)
.reasonStyle(selected: reason == self.newReason)
.padding([.leading, .bottom])
.onTapGesture {
buttonPressHaptic(self.fire.reduceHaptics)
self.newReason = reason
}
.padding(.top, 5)
}
Rectangle()
.frame(width: 14, height: 1)
.opacity(0)
}
}
}
// MARK: - TIME SLIDER
VStack(alignment: .leading){
Text("For how long?")
.font(.headline)
VStack(alignment: .leading, spacing: 0){
HStack{
Image(systemName: "timer")
Text("\(self.displayTime)")
.font(.headline)
.fontWeight(.bold)
}
TimeSliderView(time: self.$time)
.frame(height: 60)
.padding([.top, .bottom, .trailing])
}
}.padding(.leading)
//MARK: - UPDATE BUTTONS
HStack{
Button(action: {
buttonPressHaptic(self.fire.reduceHaptics)
self.fire.saveState(user: self.fire.profile, status: self.newStatus, reason: self.newReason, end: self.time)
self.presentationMode.wrappedValue.dismiss()
}){
Text("Update")
.font(.system(size: 16, weight: .bold, design: .default))
.padding(.horizontal)
}
.buttonStyle(WideButtonStyle(color: Color.gray.opacity(0.4)))
Spacer()
Button(action: {
buttonPressHaptic(self.fire.reduceHaptics)
self.fire.saveState(user: self.fire.profile, status: self.newStatus, reason: self.newReason, end: self.time)
self.fire.remindHouse()
self.presentationMode.wrappedValue.dismiss()
/*
withAnimation(.linear(duration: 0.3)){
self.tapped = true
}
//dismiss edit card view after pressing update, but with time to see checkmark animation
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.presentationMode.wrappedValue.dismiss()
}*/
}){
if !self.tapped{
Text("Update & Notify All")
.font(.system(size: 16, weight: .bold, design: .default))
.padding(.horizontal)
} else{
HStack{
Spacer()
Image(systemName: "checkmark.circle")
.font(.title)
Spacer()
}
.transition(.opacity)
}
}
.buttonStyle(WideButtonStyle(color: getColor(status: self.newStatus)))
}
.padding(.horizontal)
Spacer()
}
.background(Color(UIColor.secondarySystemBackground))
.edgesIgnoringSafeArea(.bottom)
.onDisappear {
//Want to not save their status if they dismiss, only save if they press
//self.fire.saveState(user: self.fire.profile, status: self.newStatus, reason: self.newReason, end: self.time)
self.fire.saveCustomReasons(reasons: self.reasons)
}
.onAppear {
self.newReason = self.fire.profile.reason
self.newStatus = self.fire.profile.status
self.reasons = self.fire.getCustomReasons()
self.defaultEnd = self.fire.profile.end
self.time = self.fire.profile.end
self.start = self.fire.profile.start
}
.offset(y: -0.3 * keyboard.currentHeight)
}
}
struct EditCardView_Previews: PreviewProvider {
static var previews: some View {
EditCardView().environmentObject(Fire())
}
}