-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContentView.swift
183 lines (161 loc) · 6.24 KB
/
ContentView.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
//
// ContentView.swift
// P17M Flashzilla
//
// Created by Julian Moorhouse on 06/05/2021.
//
import SwiftUI
extension View {
func stacked(at position: Int, in total: Int) -> some View {
let offset = CGFloat(total - position)
// Move down 10 points for each position in the array
return self.offset(CGSize(width: 0, height: offset * 10))
}
}
struct ContentView: View {
@Environment(\.accessibilityDifferentiateWithoutColor) var differentiateWithoutColor
@Environment(\.accessibilityEnabled) var accessibilityEnabled
//@State private var cards = [Card](repeating: Card.example, count: 10)
@State private var cards = [Card]()
@State private var isActive = true
@State private var timeRemaining = 100
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State private var showingEditScreen = false
var body: some View {
ZStack {
Image(decorative: "background")
.resizable()
//.scaledToFill() // iOS 14 issues ? or image wrong size for iPhone 8 ?
.edgesIgnoringSafeArea(.all)
VStack {
Text("Time: \(timeRemaining)")
.font(.largeTitle)
.foregroundColor(.white)
.padding(.horizontal, 20)
.padding(.vertical, 5)
.background(
Capsule()
.fill(Color.black)
.opacity(0.75)
)
ZStack {
ForEach(0 ..< cards.count, id: \.self) { index in
CardView(card: self.cards[index]) {
withAnimation {
self.removeCard(at: index)
}
}
.stacked(at: index, in: self.cards.count)
// Only allow the top card to be moved
.allowsHitTesting(index == self.cards.count - 1)
// Stop voiceover reading out all cards
.accessibility(hidden: index < self.cards.count - 1)
}
}
.allowsHitTesting(timeRemaining > 0)
if cards.isEmpty {
Button("Start Again", action: resetCards)
.padding()
.background(Color.white)
.foregroundColor(.black)
.clipShape(Capsule())
}
}
VStack {
HStack {
Spacer()
Button(action: {
self.showingEditScreen = true
}) {
Image(systemName: "plus.circle")
.padding()
.background(Color.black.opacity(0.75))
.clipShape(Circle())
}
}
Spacer()
}
.foregroundColor(.white)
.font(.largeTitle)
.padding()
if differentiateWithoutColor || accessibilityEnabled {
VStack {
Spacer()
HStack {
Button(action: {
withAnimation {
self.removeCard(at: self.cards.count - 1)
}
}) {
Image(systemName: "xmark.circle")
.padding()
.background(Color.black.opacity(0.7))
.clipShape(Circle())
}
.accessibility(label: Text("Wrong"))
.accessibility(hint: Text("Mark your answer as being incorrect"))
Spacer()
Button(action: {
withAnimation {
self.removeCard(at: self.cards.count - 1)
}
}) {
Image(systemName: "checkmark.circle")
.padding()
.background(Color.black.opacity(0.7))
.clipShape(Circle())
}
.accessibility(label: Text("Correct"))
.accessibility(hint: Text("Mark your answer as being correct"))
}
.foregroundColor(.white)
.font(.largeTitle)
.padding()
}
}
}
.onReceive(timer) { time in
guard self.isActive else { return }
if self.timeRemaining > 0 {
self.timeRemaining -= 1
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
self.isActive = false
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
if self.cards.isEmpty == false {
self.isActive = true
}
}
.sheet(isPresented: $showingEditScreen, onDismiss: resetCards) {
EditCards()
}
.onAppear(perform: resetCards)
}
func removeCard(at index: Int) {
guard index >= 0 else { return }
cards.remove(at: index)
if cards.isEmpty {
isActive = false
}
}
func resetCards() {
//cards = [Card](repeating: Card.example, count: 10)
timeRemaining = 100
isActive = true
loadData()
}
func loadData() {
if let data = UserDefaults.standard.data(forKey: "Cards") {
if let decoded = try? JSONDecoder().decode([Card].self, from: data) {
self.cards = decoded
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}