Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ConclusionView): add ConclusionView #62

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 72 additions & 6 deletions SwiftUIQuizz/Views/ConclusionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,80 @@ import SwiftUI

extension Views {
struct ConclusionView: View {
@ObservedObject var viewModel: ViewModel
@State var loadingProgress: Int = 0

var body: some View {
Text("Finished Quiz")
Text("Correct Answers: \(Manager.AnswerTracker.shared.correctAnswers)")
Text("Incorrect Answers: \(Manager.AnswerTracker.shared.wrongAnswers)")
Text("Total Questions Answers: \(Manager.AnswerTracker.shared.questionAmount)")
NavigationLink(destination: InitialView(viewModel: .init()).navigationBarHidden(true)) {
Text("Play Again")
VStack {
ZStack {
Circle()
.trim(from: Views.ConclusionView.CircleConstant.trimBegin,
to: Views.ConclusionView.CircleConstant.trimTarget)
.stroke(Color.red,
style: StrokeStyle(lineWidth: Views.ConclusionView.CircleConstant.defaultLineWidth))
.frame(width: Views.ConclusionView.CircleConstant.frameSize,
height: Views.ConclusionView.CircleConstant.frameSize)
.rotationEffect((Angle(degrees: Views.ConclusionView.CircleConstant.defaultAngle)))
Circle()
.trim(from: Views.ConclusionView.CircleConstant.trimBegin,
to: Views.ConclusionView.CircleConstant.animatedTrimTarget(target: loadingProgress))
.stroke(Color.blue, lineWidth: Views.ConclusionView.CircleConstant.defaultLineWidth)
.frame(width: Views.ConclusionView.CircleConstant.frameSize,
height: Views.ConclusionView.CircleConstant.frameSize)
.rotationEffect(Angle(degrees: Views.ConclusionView.CircleConstant.defaultAngle))
Text("\(viewModel.answerRate())%")
}
VStack(spacing: DesignSystem.Padding.microPadding) {
Text("Finished Quiz")
Text("Correct Answers: \(viewModel.correctAnswers)")
Text("Incorrect Answers: \(viewModel.wrongAnswers)")
Text("Total Questions Answers: \(viewModel.totalQuestions)")
NavigationLink(destination: InitialView(viewModel: .init()).navigationBarHidden(true)) {
Text("Play Again")
}
}
}.task {
animateCircle()
}
}

func animateCircle() {
_ = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { timer in
withAnimation {
if loadingProgress < viewModel.answerRate() {
loadingProgress += 1
print(loadingProgress)
} else {
timer.invalidate()
return
}
}
}
}
}
}

extension Views.ConclusionView {
struct CircleConstant {
static let frameSize: CGFloat = 200
static let trimBegin: CGFloat = 0.0
static let trimTarget: CGFloat = 0.5
static let defaultLineWidth: CGFloat = 12.0
static let defaultAngle: CGFloat = 180
static func animatedTrimTarget(target: Int) -> Double {
return Double(target) / 100 / 2
}
}

class ViewModel: ObservableObject {
@Published var correctAnswers: Int = Manager.AnswerTracker.shared.correctAnswers
@Published var wrongAnswers: Int = Manager.AnswerTracker.shared.wrongAnswers
@Published var totalQuestions: Int = Manager.AnswerTracker.shared.questionAmount

func answerRate() -> Int {
var rate: Double
rate = Double(correctAnswers) / Double(totalQuestions)
return Int(rate*100)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between variables in a operation

}
}
}
2 changes: 1 addition & 1 deletion SwiftUIQuizz/Views/LaunchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension Views {
.foregroundColor(DesignSystem.Color.System.logoFontColor.color.uiColor)
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
withAnimation {
self.isLoading = false
}
Expand Down
2 changes: 1 addition & 1 deletion SwiftUIQuizz/Views/QuestionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extension Views {
Text("Next Question")
}
} else if currentQuestion >= Manager.API.shared.questions.count - 1 && self.isAnimating {
NavigationLink(destination: ConclusionView().navigationBarHidden(true)
NavigationLink(destination: ConclusionView(viewModel: .init()).navigationBarHidden(true)
.onAppear {
Manager.SFX.playSound(sound: .finished)
}
Expand Down