From a71ab4c81ac40ad2a9e98e3f115244fc4f328392 Mon Sep 17 00:00:00 2001 From: BlanxxtyIS Date: Thu, 6 Jul 2023 16:08:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=82=D0=BE=D0=B3=204=20=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MovieQuizViewController.swift | 109 ++++++++---------- 1 file changed, 49 insertions(+), 60 deletions(-) diff --git a/MovieQuiz/Presentation/MovieQuizViewController.swift b/MovieQuiz/Presentation/MovieQuizViewController.swift index 23ec75e320..ebb9d73962 100644 --- a/MovieQuiz/Presentation/MovieQuizViewController.swift +++ b/MovieQuiz/Presentation/MovieQuizViewController.swift @@ -3,10 +3,6 @@ import UIKit final class MovieQuizViewController: UIViewController { // MARK: - Lifecycle - override func viewDidLoad() { - super.viewDidLoad() - } - //Типы на экране struct ViewModel { let image: UIImage @@ -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), @@ -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) { @@ -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() } }