-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombineLettersView.swift
217 lines (199 loc) · 8.99 KB
/
CombineLettersView.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
//
// CombineLettersView.swift
// FeelTheHangeul
//
// Created by Greed on 2/25/24.
//
import SwiftUI
struct CombineLettersView: View {
let ttsManager = TTSManager.shared
let cho:[String] = ["ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ","ㅁ","ㅂ","ㅃ","ㅅ","ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"]
let jung:[String] = ["ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅗ", "ㅘ", "ㅙ", "ㅚ", "ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ", "ㅠ", "ㅡ", "ㅢ", "ㅣ"]
let jong:[String] = ["empty", "ㄱ", "ㄲ", "no", "ㄴ", "no", "no", "ㄷ", "ㄹ", "no", "no", "no", "no", "no", "no", "no", "ㅁ", "ㅂ", "no", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"]
let columns = [GridItem(.flexible(), spacing: 20), GridItem(.flexible(), spacing: 20), GridItem(.flexible(), spacing: 20), GridItem(.flexible(), spacing: 20)]
@State var answer = ""
@State var pickedType = 0
@State var resultLetter = ""
@State var showing = false
@State var isCorrect = false
@State var inputFirst = "ㅎ"
@State var inputSecond = "ㅏ"
@State var inputThird = "ㄴ"
@State var showModal = false
var body: some View {
NavigationStack {
VStack {
HStack {
Spacer()
VStack {
Text(self.resultLetter)
.font(.system(size: 150, weight: .bold))
.frame(height: 150)
.onAppear {
resultLetter = hangle(c1: inputFirst, c2: inputSecond, c3: inputThird)
}
HStack {
Text("[ \(resultLetter.applyingTransform(.toLatin, reverse: false) ?? "") ]")
.font(.system(size: 40))
Button {
ttsManager.play(resultLetter)
} label: {
Image(systemName: "speaker.wave.3")
.font(.system(size: 35))
}
}
.frame(height: 50)
.frame(alignment: .bottom)
}
Spacer()
VStack(spacing: -40) {
HStack(spacing: -40) {
Image(inputFirst)
Image(inputSecond)
}
Image(inputThird)
}
Spacer()
VStack {
Text("Writing Box")
.font(.headline)
TextField("", text: $answer)
.font(.system(size: 250))
.frame(width: 250, height: 250)
.border(.blue, width: 5)
.onChange(of: answer) { _ in
if resultLetter == answer {
print("정답")
isCorrect = true
}
showing = true
DispatchQueue.global().async {
sleep(1)
self.answer = ""
}
}
}
Spacer()
}
.frame(height: 300)
.alert(isPresented: $showing) {
let defaultButton = Alert.Button.default(Text("OK")) {
showing = false
isCorrect = false
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
return Alert(title: Text(isCorrect ? "Good Job!" : "Try Again!") , message: Text(isCorrect ? "Let's practice other letter, too" : "You can do it! Write it again "), dismissButton: defaultButton)
}
Picker("select", selection: $pickedType) {
Text("first consonant").tag(0)
Text("middle vowel").tag(1)
Text("last consonant").tag(2)
}
.pickerStyle(.segmented)
ScrollView {
if pickedType == 0 {
LazyVGrid(columns: columns) {
ForEach(cho, id: \.self) { value in
LetterCardView(letter: value)
.frame(width: 200, height: 200)
.background(value == inputFirst ? .cyan : .white)
.cornerRadius(20)
.modifier(CardModifier())
.onTapGesture {
print(1)
inputFirst = value
resultLetter = hangle(c1: value, c2: inputSecond, c3: inputThird)
}
}
}
} else if pickedType == 1 {
LazyVGrid(columns: columns) {
ForEach(jung, id: \.self) { value in
LetterCardView(letter: value)
.frame(width: 200, height: 200)
.background(value == inputSecond ? .cyan : .white)
.cornerRadius(20)
.modifier(CardModifier())
.onTapGesture {
inputSecond = value
resultLetter = hangle(c1: inputFirst, c2: value, c3: inputThird)
}
}
}
} else {
let list = jong.filter { $0 != "no"}
LazyVGrid(columns: columns) {
ForEach(list, id: \.self) { value in
LetterCardView(letter: value)
.frame(width: 200, height: 200)
.background(inputThird == value ? .cyan : .white)
.cornerRadius(20)
.modifier(CardModifier())
.onTapGesture {
print(2)
inputThird = value
resultLetter = hangle(c1: inputFirst, c2: inputSecond, c3: value)
}
}
}
}
}
}
.sheet(isPresented: self.$showModal) {
CombineInfoModalView()
}
.toolbar {
Button("Help") {
showModal = true
}
}
}
}
}
extension CombineLettersView {
func hangle(c1:String,c2:String,c3:String) -> String {
var cho_i = 0
var jung_i = 0
var jong_i = 0
for i in 0 ..< cho.count {
if cho[i] == c1 { cho_i = i }
}
for i in 0 ..< jung.count {
if jung[i] == c2 { jung_i = i }
}
for i in 0 ..< jong.count {
if jong[i] == c3 { jong_i = i }
}
let uniValue:Int = (cho_i * 21 * 28) + (jung_i * 28) + (jong_i) + 0xAC00;
if let uni = Unicode.Scalar(uniValue) {
return String(uni)
}
return "none"
}
}
struct CombineInfoModalView: View {
@Environment(\.presentationMode) var presentation
var body: some View {
VStack {
HStack {
Spacer()
Button {
presentation.wrappedValue.dismiss()
} label: {
Image(systemName: "x.circle")
}.frame(width: 50, height: 50)
}
Spacer()
Image("collection")
.resizable()
.scaledToFit()
Spacer().frame(height: 20)
Text("Korean words consist of an initial consonant and a vowel or sometimes a consonant placed under a vowel. You can click on the consonants and vowels to study how they sound and what the stroke order is like. You can also write the letters in the writing box using Apple pencil if you wanna check that you wrote them correctly. If you can't see the Korean characters as you write, try setting it up!")
.frame(width: UIScreen.main.bounds.width * 2 / 3 )
Spacer()
}
}
}
#Preview {
CombineLettersView()
}