-
Notifications
You must be signed in to change notification settings - Fork 0
/
CipherGraphView.swift
277 lines (259 loc) · 11.4 KB
/
CipherGraphView.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
import SwiftUI
struct CipherGraphView: View {
let lowercaseLetter = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
let uppercaseLetter = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
@ObservedObject var viewModel: CipherViewModel
@State var encryptPulseStart = false
@State var decryptPulseStart = false
@State var keyShake = 0
@State var keyShakeAgain = 0
@State var wrongKeyShake = 0
var body: some View {
VStack(spacing: 25) {
Spacer()
ZStack {
Text(" \(viewModel.message) ")
.font(.custom("menlo", size: 48, relativeTo: .largeTitle))
.id("message" + viewModel.message)
.transition(.opacity)
}
.padding()
.overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.gray, style: StrokeStyle(lineWidth: 1.0)))
HStack {
Spacer()
VStack {
Image("arrow_right")
.resizable()
.frame(width: 18, height: 18)
.scaleEffect(x: 1, y: -1, anchor: .center)
.offset(x: 3)
.invertOnDarkTheme()
if #available(iOS 16.0, *) {
if viewModel.pageThree {
Image("encryption")
.scaleEffect(encryptPulseStart ? 1.05 : 1)
.animation(encryptPulseStart ? .easeInOut.repeatForever(autoreverses: true) : .default, value: encryptPulseStart)
.dropDestination(for: String.self) { _, _ in
// save user choices
viewModel.originalMessage = viewModel.message
viewModel.encryptedMessage = encrypt(viewModel.message, key: viewModel.key.wrappedValue)
viewModel.wrongMessage = encrypt(viewModel.message, key: viewModel.key.wrappedValue+1)
Task {
// show the encrypted message
await animateToEncrypted()
}
return true
}
} else {
Image("encryption")
}
}
}
Spacer()
VStack {
Image("arrow_left")
.resizable()
.frame(width: 18, height: 18)
.scaleEffect(x: 1, y: -1, anchor: .center)
.invertOnDarkTheme()
if #available(iOS 16.0, *) {
if viewModel.pageFour {
Image("decryption")
.scaleEffect(decryptPulseStart ? 1.05 : 1)
.animation(decryptPulseStart ? .easeInOut.repeatForever(autoreverses: true) : .default, value: decryptPulseStart)
.dropDestination(for: String.self) { _, _ in
// stop animating
decryptPulseStart = false
Task {
// show the decrypted message
await animateToDecrypted()
}
return true
}
} else if viewModel.pageFive {
Image("decryption")
.scaleEffect(decryptPulseStart ? 1.05 : 1)
.animation(decryptPulseStart ? .easeInOut.repeatForever(autoreverses: true) : .default, value: decryptPulseStart)
.dropDestination(for: String.self) { _, _ in
// stop animating
decryptPulseStart = false
Task {
// show the wrong dencrypted message
await animateToWrongDecrypted()
}
return true
}
} else {
Image("decryption")
}
}
}
Spacer()
}
HStack {
Spacer()
// correct key
if #available(iOS 16.0, *) {
if viewModel.pageThree {
ZStack {
Image("key")
Style.monospaceBig("\(viewModel.key.wrappedValue)").foregroundColor(.black).offset(x: -30)
}
.shake(with: keyShake)
.onAppear {
withAnimation(.shakeSpring()) {
keyShake = 3
}
}
.onDrag {
encryptPulseStart = true
return NSItemProvider(object: "enc" as NSItemProviderWriting)
}
} else if viewModel.pageFour && !viewModel.pageFourSub {
ZStack {
Image("key")
Style.monospaceBig("\(viewModel.key.wrappedValue)").foregroundColor(.black).offset(x: -30)
}
.shake(with: keyShakeAgain)
.onAppear {
withAnimation(.shakeSpring()) {
keyShakeAgain = 3
}
}
.onDrag {
decryptPulseStart = true
return NSItemProvider(object: "enc" as NSItemProviderWriting)
}
} else {
ZStack {
Image("key")
Style.monospaceBig("\(viewModel.key.wrappedValue)").foregroundColor(.black).offset(x: -30)
}
}
} else {
// Fallback on earlier versions
}
Spacer()
// wrong key
if #available(iOS 16.0, *) {
if viewModel.pageFive && !viewModel.pageFiveSub {
ZStack {
Image("key_wrong")
Style.monospaceBig("\(viewModel.key.wrappedValue+1)").foregroundColor(.black).offset(x: -30)
}
.shake(with: wrongKeyShake)
.onAppear {
withAnimation(.shakeSpring()) {
wrongKeyShake = 3
}
}
.onDrag {
decryptPulseStart = true
return NSItemProvider(object: "enc" as NSItemProviderWriting)
}
.transition(.enterFromRight)
Spacer()
} else if viewModel.pageFiveSub || viewModel.pageSix {
ZStack {
Image("key_wrong")
Style.monospaceBig("\(viewModel.key.wrappedValue+1)").foregroundColor(.black).offset(x: -30)
}
Spacer()
}
} else {
// Fallback on earlier versions
}
}
Spacer()
}.animation(.default, value: viewModel.pageFive)
}
func encrypt(_ t: String, key: Int) -> String {
var encrypted = ""
for letter in t {
if String(letter) == " " {
encrypted.append(" ")
continue
}
if letter.isLowercase { // lowercase
if let oldIndex = lowercaseLetter.firstIndex(of: String(letter)) {
let newIndex = (oldIndex + key) % 26
encrypted.append(lowercaseLetter[newIndex])
} else {
print("SOME ERROR WITH OPTIONALS")
}
} else { // uppercase
if let oldIndex = uppercaseLetter.firstIndex(of: String(letter)) {
let newIndex = (oldIndex + key) % 26
encrypted.append(uppercaseLetter[newIndex])
}
}
}
return encrypted
}
func decrypt(_ t: String, key: Int) -> String {
var decrypted = ""
for letter in t {
if String(letter) == " " {
decrypted.append(" ")
continue
}
if letter.isLowercase { // lowercase
if let oldIndex = lowercaseLetter.firstIndex(of: String(letter)) {
let newIndex = (oldIndex - key) %% 26
decrypted.append(lowercaseLetter[newIndex])
} else {
print("SOME ERROR WITH OPTIONALS")
}
} else { // uppercase
if let oldIndex = uppercaseLetter.firstIndex(of: String(letter)) {
let newIndex = (oldIndex - key) %% 26
decrypted.append(uppercaseLetter[newIndex])
}
}
}
return decrypted
}
func animateToEncrypted() async {
try? await Task.sleep(nanoseconds: 50_000_000) // 0.1 seconds
withAnimation(.default) {
viewModel.message = encrypt(viewModel.message, key: 1)
}
if viewModel.message != viewModel.encryptedMessage {
Task {
await animateToEncrypted()
}
} else {
// next page
viewModel.pageThree = false
viewModel.pageFour = true
}
}
func animateToDecrypted() async {
try? await Task.sleep(nanoseconds: 50_000_000) // 0.1 seconds
withAnimation(.default) {
viewModel.message = decrypt(viewModel.message, key: 1)
}
if viewModel.message != viewModel.originalMessage {
Task {
await animateToDecrypted()
}
} else {
// display success
viewModel.pageFourSub = true
}
}
func animateToWrongDecrypted() async {
try? await Task.sleep(nanoseconds: 50_000_000) // 0.1 seconds
withAnimation(.default) {
viewModel.message = decrypt(viewModel.message, key: 1)
}
if viewModel.message != viewModel.wrongMessage {
Task {
await animateToWrongDecrypted()
}
} else {
// display success
viewModel.pageFiveSub = true
}
}
}