Skip to content

Commit

Permalink
add time quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
xqsadness committed Sep 19, 2023
1 parent 143e566 commit ddc8793
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 31 deletions.
4 changes: 4 additions & 0 deletions DefaultProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
DD67413E2A85D9AE00CB9B0B /* MathView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD67413D2A85D9AE00CB9B0B /* MathView.swift */; };
DD6741422A8616D200CB9B0B /* correct.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = DD6741402A86162600CB9B0B /* correct.mp3 */; };
DD6741442A8624F500CB9B0B /* wrong.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = DD6741432A8624F500CB9B0B /* wrong.mp3 */; };
DD69C5D62AB9916100ADED44 /* QuizTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD69C5D52AB9916100ADED44 /* QuizTimer.swift */; };
DD72E2322A8A1233006AB647 /* SpeechRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD72E2312A8A1233006AB647 /* SpeechRecognizer.swift */; };
DD72E2362A8A1D39006AB647 /* PoinRealm.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD72E2352A8A1D38006AB647 /* PoinRealm.swift */; };
DD72E2382A8A1D4D006AB647 /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD72E2372A8A1D4D006AB647 /* HistoryView.swift */; };
Expand Down Expand Up @@ -247,6 +248,7 @@
DD67413D2A85D9AE00CB9B0B /* MathView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MathView.swift; sourceTree = "<group>"; };
DD6741402A86162600CB9B0B /* correct.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = correct.mp3; sourceTree = "<group>"; };
DD6741432A8624F500CB9B0B /* wrong.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = wrong.mp3; sourceTree = "<group>"; };
DD69C5D52AB9916100ADED44 /* QuizTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuizTimer.swift; sourceTree = "<group>"; };
DD72E2312A8A1233006AB647 /* SpeechRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpeechRecognizer.swift; sourceTree = "<group>"; };
DD72E2352A8A1D38006AB647 /* PoinRealm.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PoinRealm.swift; sourceTree = "<group>"; };
DD72E2372A8A1D4D006AB647 /* HistoryView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -567,6 +569,7 @@
children = (
DD72E2312A8A1233006AB647 /* SpeechRecognizer.swift */,
DD0EA48A2A8B30FD007D8C55 /* Language.swift */,
DD69C5D52AB9916100ADED44 /* QuizTimer.swift */,
);
path = ViewModel;
sourceTree = "<group>";
Expand Down Expand Up @@ -880,6 +883,7 @@
CA2223492A2BB329005164BB /* ContentView.swift in Sources */,
CA2223A62A2BB34A005164BB /* Giftcode.swift in Sources */,
DDA494532A871421003814F1 /* PopupScoreView.swift in Sources */,
DD69C5D62AB9916100ADED44 /* QuizTimer.swift in Sources */,
DD7F60FB2A8F040C0030E044 /* ListeningInfoView.swift in Sources */,
CA2223B72A2BB34A005164BB /* Array.swift in Sources */,
CA2223972A2BB34A005164BB /* SkeletonView.swift in Sources */,
Expand Down
Binary file not shown.
7 changes: 3 additions & 4 deletions DefaultProject/View/Color/ColorSubmitNextButtonsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ struct ColorSubmitNextButtonsView: View {
loadAudio(nameSound: "correct")
isCorrect = true
countCorrect += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
if selectedTab < CONSTANT.SHARED.DATA_COLOR.count - 1{
submitCorrect()
}else{
isSubmit = true
completeAllQuestion()
}
})
// })
}else{
isSubmit = true
loadAudio(nameSound: "wrong")
Expand All @@ -65,9 +65,7 @@ struct ColorSubmitNextButtonsView: View {
completeAllQuestion()
}

// if selectedTab >= QUIZDEFAULT.SHARED.listQuestionsHistory.count - 1{
Point.updatePointColor(point: countCorrect)
// }
}label: {
Text(selectedTab < CONSTANT.SHARED.DATA_COLOR.count - 1 ? "Next".cw_localized : "Done".cw_localized)
.foregroundColor(.text)
Expand Down Expand Up @@ -104,6 +102,7 @@ struct ColorSubmitNextButtonsView: View {
}else{
loadAudio(nameSound: "congralutions")
}
QuizTimer.shared.stop()
}

func loadAudio(nameSound: String) {
Expand Down
8 changes: 7 additions & 1 deletion DefaultProject/View/Color/ColorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ struct ColorView: View {
.background(Color.text)
.navigationBarBackButtonHidden(true)
.popup(isPresented: $isShowPopup) {
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, totalQuestion: CONSTANT.SHARED.DATA_COLOR.count)
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, title: "Color", totalQuestion: CONSTANT.SHARED.DATA_COLOR.count)
}
.onAppear{
QuizTimer.shared.start()
}
.onDisappear{
QuizTimer.shared.reset()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ struct HistorySubmitNextButtonsView: View {
loadAudio(nameSound: "correct")
isCorrect = true
countCorrect += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
if selectedTab < CONSTANT.SHARED.DATA_HISTORY.count - 1{
submitCorrect()
}else{
isSubmit = true
completeAllQuestion()
}
})
// })
}else{
isSubmit = true
loadAudio(nameSound: "wrong")
Expand Down Expand Up @@ -106,6 +106,7 @@ struct HistorySubmitNextButtonsView: View {
}else{
loadAudio(nameSound: "congralutions")
}
QuizTimer.shared.stop()
}

func loadAudio(nameSound: String) {
Expand Down
12 changes: 9 additions & 3 deletions DefaultProject/View/History/HistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct HistoryView: View {
.contentShape(Rectangle()).gesture(DragGesture())
.onAppear{
answerCorrect = CONSTANT.SHARED.DATA_HISTORY[index].answer
DispatchQueue.main.asyncAfter(deadline: .now() + 0.75 ,execute: {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.8 ,execute: {
if !synthesizer.isSpeaking{
speakText(textToSpeak: CONSTANT.SHARED.DATA_HISTORY[index].question.cw_localized)
}else{
Expand All @@ -75,7 +75,13 @@ struct HistoryView: View {
.background(Color.text)
.navigationBarBackButtonHidden(true)
.popup(isPresented: $isShowPopup) {
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, totalQuestion: CONSTANT.SHARED.DATA_HISTORY.count)
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, title: "History ", totalQuestion: CONSTANT.SHARED.DATA_HISTORY.count)
}
.onAppear{
QuizTimer.shared.start()
}
.onDisappear{
QuizTimer.shared.reset()
}
}

Expand All @@ -89,7 +95,7 @@ struct HistoryView: View {

let utterance = AVSpeechUtterance(string: textToSpeak)
utterance.voice = AVSpeechSynthesisVoice(language: language)
utterance.rate = 0.3
utterance.rate = 0.35
synthesizer.speak(utterance)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ struct ListeningSubmitNextButtonsView: View {
loadAudio(nameSound: "correct")
isCorrect = true
countCorrect += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
if selectedTab < CONSTANT.SHARED.DATA_LISTEN.count - 1{
submitCorrect()
}else{
isSubmit = true
completeAllQuestion()
}
})
// })
}else{
isSubmit = true
loadAudio(nameSound: "wrong")
Expand Down Expand Up @@ -103,6 +103,7 @@ struct ListeningSubmitNextButtonsView: View {
}else{
loadAudio(nameSound: "congralutions")
}
QuizTimer.shared.stop()
}

func loadAudio(nameSound: String) {
Expand Down
10 changes: 8 additions & 2 deletions DefaultProject/View/Listen/ListeningView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct ListeningView: View {
.contentShape(Rectangle()).gesture(DragGesture())
.onAppear{
answerCorrect = CONSTANT.SHARED.DATA_LISTEN[index].answer
DispatchQueue.main.asyncAfter(deadline: .now() + 0.75 ,execute: {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.8 ,execute: {
if !synthesizer.isSpeaking{
speakText(textToSpeak: CONSTANT.SHARED.DATA_LISTEN[index].answer.cw_localized)
}else{
Expand All @@ -74,7 +74,13 @@ struct ListeningView: View {
.background(Color.text)
.navigationBarBackButtonHidden(true)
.popup(isPresented: $isShowPopup) {
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, totalQuestion: CONSTANT.SHARED.DATA_LISTEN.count)
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, title: "Listening", totalQuestion: CONSTANT.SHARED.DATA_LISTEN.count)
}
.onAppear{
QuizTimer.shared.start()
}
.onDisappear{
QuizTimer.shared.reset()
}
}

Expand Down
10 changes: 9 additions & 1 deletion DefaultProject/View/ListenEndRepeat/ListenAndRepeatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct ListenAndRepeatView: View {
.background(Color.text)
.navigationBarBackButtonHidden(true)
.popup(isPresented: $isShowPopup) {
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, totalQuestion: CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count)
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, title: "Listen and repeat", totalQuestion: CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count)
}
.onTapGesture {
withAnimation {
Expand Down Expand Up @@ -113,6 +113,7 @@ struct ListenAndRepeatView: View {
}
withAnimation {
isShowPopup = true
QuizTimer.shared.reset()
}
}
}
Expand Down Expand Up @@ -145,6 +146,7 @@ struct ListenAndRepeatView: View {
loadAudio(nameSound: "congralutions")
withAnimation {
isShowPopup = true
QuizTimer.shared.reset()
}
}
}
Expand All @@ -169,6 +171,12 @@ struct ListenAndRepeatView: View {
}
}
}
.onAppear{
QuizTimer.shared.start()
}
.onDisappear{
QuizTimer.shared.reset()
}
}

func handleTapToSpeak(answer: String){
Expand Down
5 changes: 3 additions & 2 deletions DefaultProject/View/Math/MathSubmitNextButtonsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ struct MathSubmitNextButtonsView: View {
loadAudio(nameSound: "correct")
isCorrect = true
countCorrect += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
if selectedTab < CONSTANT.SHARED.DATA_MATH.count - 1{
submitCorrect()
}else{
isSubmit = true
completeAllQuestion()
}
})
// })
}else{
isSubmit = true
loadAudio(nameSound: "wrong")
Expand Down Expand Up @@ -106,6 +106,7 @@ struct MathSubmitNextButtonsView: View {
}else{
loadAudio(nameSound: "congralutions")
}
QuizTimer.shared.stop()
}

func loadAudio(nameSound: String) {
Expand Down
11 changes: 10 additions & 1 deletion DefaultProject/View/Math/MathView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct MathView: View {
}

MathInfoView(countCorrect: $countCorrect, countWrong: $countWrong, selectedTab: $selectedTab, progress: $progress)
.onTapGesture {
isShowPopup.toggle()
}

VStack{
TabView(selection: $selectedTab) {
Expand All @@ -67,7 +70,13 @@ struct MathView: View {
.background(Color.text)
.navigationBarBackButtonHidden(true)
.popup(isPresented: $isShowPopup) {
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, totalQuestion: CONSTANT.SHARED.DATA_MATH.count)
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, title: "Math", totalQuestion: CONSTANT.SHARED.DATA_MATH.count)
}
.onAppear{
QuizTimer.shared.start()
}
.onDisappear{
QuizTimer.shared.reset()
}
}
}
Expand Down
57 changes: 46 additions & 11 deletions DefaultProject/View/PopupScoreView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,12 @@ struct PopupScoreView: View {
@Binding var isShowPopup: Bool
@Binding var countCorrect: Int
@Binding var countWrong: Int
@State var title: String
@State var totalQuestion: Int
@EnvironmentObject var coordinator: Coordinator

var body: some View {
VStack{
Text(countCorrect == 0 ? "You didn't get any question right !".cw_localized : "Congralutions! You have scored".cw_localized)
.font(.bold(size: 20))
.foregroundColor(.background)
.vAlign(.top)
.hAlign(.center)
.fixedSize(horizontal: false, vertical: true)
.padding(.bottom,15)

Circle()
.foregroundColor(Color(hex: "796dee"))
.frame(width: 130, height: 130)
Expand All @@ -41,6 +34,49 @@ struct PopupScoreView: View {
)
.vAlign(.top)

Text(countCorrect == 0 ? "You didn't get any question right !".cw_localized : "Congralutions! You have scored".cw_localized)
.font(.bold(size: 20))
.foregroundColor(.background)
.vAlign(.top)
.hAlign(.center)
.fixedSize(horizontal: false, vertical: true)
.padding(.bottom,15)

styledText(text: "\(title) Quiz", foregroundColor: .background, font: .bold(size: 16))
.hAlign(.leading)
.padding(.bottom)

VStack(spacing: 8){
HStack{
styledText(text: "Correct Answer", foregroundColor: .text2, font: .regular(size: 15))
Spacer()
styledText(text: "\(countCorrect)", foregroundColor: .background, font: .regular(size: 15))
}
.frame(width: 160, alignment: .center)

HStack{
styledText(text: "Wrong Answer", foregroundColor: .text2, font: .regular(size: 15))
Spacer()
styledText(text: "\(countWrong)", foregroundColor: .background, font: .regular(size: 15))
}
.frame(width: 160, alignment: .center)

HStack{
styledText(text: "Total Answered", foregroundColor: .text2, font: .regular(size: 15))
Spacer()
styledText(text: "\(totalQuestion)", foregroundColor: .background, font: .regular(size: 15))
}
.frame(width: 160, alignment: .center)

HStack{
styledText(text: "Time spent", foregroundColor: .text2, font: .regular(size: 15))
Spacer()
styledText(text: "\(QuizTimer.shared.formatTimeInterval(QuizTimer.shared.elapsedTime))", foregroundColor: .background, font: .regular(size: 15))
}
.frame(width: 160, alignment: .center)
}
.hAlign(.center)

Button{
withAnimation {
isShowPopup = false
Expand All @@ -62,17 +98,16 @@ struct PopupScoreView: View {
}
.padding()
.frame(maxWidth: .infinity)
.frame(height: 300)
.frame(height: 390)
.background(Color.text)
.cornerRadius(12)
.padding(.horizontal)
.overlay {
LottieView(name: "congralutions", loopMode: .loop)
.frame(maxWidth: .infinity)
.frame(height: 300)
.frame(height: 400)
.padding(.horizontal)
.allowsHitTesting(false)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct SurroundingObjectView: View {
if isShowPopup {
ZStack{
Color.background.opacity(0.7).ignoresSafeArea()
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, totalQuestion: CONSTANT.SHARED.DATA_SURROUNDING.count)
PopupScoreView(isShowPopup: $isShowPopup, countCorrect: $countCorrect, countWrong: $countWrong, title: "Surrounding Object", totalQuestion: CONSTANT.SHARED.DATA_SURROUNDING.count)
}
}
if isShowPopupFail {
Expand Down Expand Up @@ -182,6 +182,12 @@ struct SurroundingObjectView: View {
listImg = listText.shuffled()
}
}
.onAppear{
QuizTimer.shared.start()
}
.onDisappear{
QuizTimer.shared.reset()
}
.onChange(of: heartPoint) { newValue in
if newValue <= 0 {
withAnimation {
Expand All @@ -194,6 +200,7 @@ struct SurroundingObjectView: View {
loadAudio(nameSound: "congralutions")
withAnimation {
isShowPopup = true
QuizTimer.shared.stop()
}
}
}
Expand Down
Loading

0 comments on commit ddc8793

Please sign in to comment.