Skip to content

Commit

Permalink
итог 4 спринта
Browse files Browse the repository at this point in the history
  • Loading branch information
BlanxxtyIS committed Jul 6, 2023
1 parent faf59e2 commit a71ab4c
Showing 1 changed file with 49 additions and 60 deletions.
109 changes: 49 additions & 60 deletions MovieQuiz/Presentation/MovieQuizViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import UIKit
final class MovieQuizViewController: UIViewController {
// MARK: - Lifecycle

override func viewDidLoad() {
super.viewDidLoad()
}

//Типы на экране
struct ViewModel {
let image: UIImage
Expand Down Expand Up @@ -35,6 +31,12 @@ final class MovieQuizViewController: UIViewController {
let correctAnswer: Bool
}

//номер текущего вопроса
private var currentQuestionIndex = 0

//счетчик правильных ответов
private var correctAnswers = 0

//Mock-Данные
private let questions: [QuizQuestion] = [
QuizQuestion(image: "The Godfather", text: "Рейтинг этого фильма больше чем 6?", correctAnswer: true),
Expand All @@ -48,52 +50,9 @@ final class MovieQuizViewController: UIViewController {
QuizQuestion(image: "Tesla", text: "Рейтинг этого фильма больше чем 6?", correctAnswer: false),
QuizQuestion(image: "Vivarium", text: "Рейтинг этого фильма больше чем 6?", correctAnswer: false)]

//номер текущего вопроса
private var currentQuestionIndex = 0

//счетчик правильных ответов
private var correctAnswers = 0

//Конвертация из QuizQuestions -> QuizStepViewModel
private func convert(model: QuizQuestion) -> QuizStepViewModel {
let questionStep = QuizStepViewModel(
image: UIImage(named: model.image) ?? UIImage(),
question: model.text,
questionNumber: "\(currentQuestionIndex + 1) / \(questions.count)")
return questionStep
}

//Приватный метод вывода на экран вопроса, который принимает на вход вью модель вопроса
private func show(quiz result: QuizResultsViewModel) {
let alert = UIAlertController(
title: result.title,
message: result.text,
preferredStyle: .alert)

let action = UIAlertAction(title: result.buttonText, style: .default) { _ in
self.currentQuestionIndex = 0
self.correctAnswers = 0

let firstQuestion = self.questions[self.currentQuestionIndex]
let viewModel = self.convert(model: firstQuestion)
self.show(quiz: viewModel)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}

//Меняет цвет рамки
private func showAnswerResults(isCorrect: Bool) {
if isCorrect {
correctAnswers += 1
}
imageViev.layer.masksToBounds = true
imageViev.layer.borderWidth = 8
imageViev.layer.borderColor = isCorrect ? UIColor.ypGreen.cgColor : UIColor.ypRed.cgColor

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.showNextQuestionOrResults()
}
override func viewDidLoad() {
super.viewDidLoad()
show(quiz: convert(model: questions[currentQuestionIndex]))
}

@IBAction func noButtonClicked(_ sender: UIButton) {
Expand All @@ -118,18 +77,48 @@ final class MovieQuizViewController: UIViewController {
// логика перехода в один из сценариев
private func showNextQuestionOrResults() {
if currentQuestionIndex == questions.count - 1 {
let text = "Ваш результат: \(correctAnswers)/10" // 1
let viewModel = QuizResultsViewModel( // 2
title: "Этот раунд окончен!",
text: text,
buttonText: "Сыграть ещё раз")
show(quiz: viewModel) // 3
let result = QuizResultsViewModel(title: "Этот раунд окончен!", text: "Ваш реузальтат \(correctAnswers)/\(questions.count)", buttonText: "Сыграть еще раз?")
show(quiz: result)
} else {
currentQuestionIndex += 1
let nextQuestion = questions[currentQuestionIndex]
let viewModel = convert(model: nextQuestion)

show(quiz: viewModel)
show(quiz: convert(model: questions[currentQuestionIndex]))
}
}

//Приватный метод вывода на экран вопроса, который принимает на вход вью модель вопроса
func show(quiz result: QuizResultsViewModel) {
let alert = UIAlertController(title: result.title, message: result.text, preferredStyle: .alert)
let action = UIAlertAction(title: result.buttonText, style: .default){_ in
self.currentQuestionIndex = 0
self.correctAnswers = 0
self.show(quiz: self.convert(model: self.questions[self.currentQuestionIndex]))
}
alert.addAction(action)
self.present(alert, animated: true)
}

func show(quiz step: QuizStepViewModel) {
imageViev.image = step.image
textLabel.text = step.question
indexLabel.text = step.questionNumber
}

//Конвертация из QuizQuestions -> QuizStepViewModel
func convert(model: QuizQuestion) -> QuizStepViewModel {
QuizStepViewModel(image: UIImage(named: model.image) ?? UIImage(), question: model.text, questionNumber: "\(currentQuestionIndex + 1)/\(questions.count)")
}

//Меняет цвет рамки
private func showAnswerResults(isCorrect: Bool) {
if isCorrect {
correctAnswers += 1
}
imageViev.layer.masksToBounds = true
imageViev.layer.borderWidth = 8
imageViev.layer.borderColor = isCorrect ? UIColor.ypGreen.cgColor : UIColor.ypRed.cgColor

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.showNextQuestionOrResults()
}
}

Expand Down

1 comment on commit a71ab4c

@BlanxxtyIS
Copy link
Author

Choose a reason for hiding this comment

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

итог

Please sign in to comment.