Skip to content

Commit

Permalink
feat: scan three cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Christopher Tanhar committed Dec 3, 2023
1 parent 0a203c2 commit 229b2a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Moco/View/Components/Prompts/ARStory/MakeSentence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct MakeSentence: View {

var prompts: [PromptModel] = [
.init(
correctAnswer: "bebe",
correctAnswer: "bebe makan madu",
startTime: 0,
promptType: PromptType.card,
hints: nil,
Expand Down Expand Up @@ -85,19 +85,24 @@ struct MakeSentence: View {
// }
} else {
if showScanner {
CardScan(scanResult: $scanResult) {
CardScan(scanResult: $scanResult, resultCount: 3) {
showScanner = false

scanResult = scanResult.map {
$0.fromBase64() ?? ""
}

print(scanResult.joined(separator: " ")
.trimmingCharacters(in: .whitespacesAndNewlines))

if scanResult.joined(separator: " ")
.trimmingCharacters(in: .whitespacesAndNewlines) != prompts[currentPromptIndex].correctAnswer {
showWrongAnswerPopup = true
return
}



currentPromptIndex += 1
print("currentPromptIndex")
print(currentPromptIndex)
Expand Down
33 changes: 32 additions & 1 deletion Moco/View/Components/Prompts/Card/CardScan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ struct CardScan: View {
@Environment(\.audioViewModel) private var audioViewModel

@State private var cameraMode = CameraMode.back
@State private var internalResultCount = 0
@Binding var scanResult: [String]

var resultCount = 1

var onComplete: (() -> Void)?

var captureDevice: AVCaptureDevice? {
Expand All @@ -52,6 +55,10 @@ struct CardScan: View {
}
}

var capturedAnswer: String {
scanResult.map{ $0.fromBase64() ?? "" }.joined(separator: " ").capitalized
}

var body: some View {
ZStack {
Color.bg.blue
Expand All @@ -68,6 +75,24 @@ struct CardScan: View {
)
.frame(height: Screen.height * 0.2)
}
if resultCount > 1 && scanResult.count > 0 {
Text("Jawaban Kamu: \(capturedAnswer)")
HStack(spacing: 40) {
Button("Ulangi") {
scanResult = []
internalResultCount = 0
}
.buttonStyle(MainButton(width: 180, type: .danger))
.font(.footnote)
if internalResultCount >= resultCount {
Button("Lanjut") {
onComplete?()
}
.buttonStyle(MainButton(width: 180, type: .success))
.font(.footnote)
}
}
}
}
VStack {
ZStack {
Expand All @@ -77,8 +102,13 @@ struct CardScan: View {
videoCaptureDevice: captureDevice,
completion: { result in
if case let .success(code) = result {
internalResultCount += 1
print(code.string)
print(scanResult)
scanResult.append(code.string)
onComplete?()
if resultCount == 1 {
onComplete?()
}
}
}
).id(cameraMode)
Expand All @@ -105,6 +135,7 @@ struct CardScan: View {
}
}.task {
scanResult = []
internalResultCount = 0
audioViewModel.playSound(
soundFileName: "arahkan_kamera",
type: .m4a,
Expand Down

0 comments on commit 229b2a3

Please sign in to comment.