Skip to content

Commit

Permalink
Changed answers status view
Browse files Browse the repository at this point in the history
  • Loading branch information
lolleyball committed Dec 17, 2023
1 parent 2da9790 commit 6c5bdcb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ extension QuestionViewController: QuestionViewModelDelegate {
guard let theme = viewModel.getTheme() else { return }
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.statusView.setQuestionsCount(count: viewModel.questionsCount())
self.emojisBackground.update(with: theme)
viewModel.setup()
self.view.sendSubviewToBack(self.emojisBackground)
Expand Down
5 changes: 5 additions & 0 deletions quizzz/quizzz/Screens/Questions/QuestionViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protocol QuestionViewModelDelegate: AnyObject {
protocol QuestionViewModelProtocol {
func answerForQuestion(_ index: Int) -> Bool
func nextQuestion()
func questionsCount() -> Int
func setup()
func getTheme() -> ParticleAnimationView.Theme?

Expand Down Expand Up @@ -78,6 +79,10 @@ extension QuestionViewModel: QuestionViewModelProtocol {
questionIndex += 1
}

func questionsCount() -> Int {
questions?.count ?? 0
}

func answerForQuestion(_ index: Int) -> Bool {
guard let questionNow = questions?[safe: questionIndex] else { return false }
let flag = questionNow.correctAnswer[safe: index] ?? false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ enum QuestionStatusCellType {
case error
case selected
case empty
case finish
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ final class QuestionStatusCollectionViewCell: UICollectionViewCell {
case .empty:
backgroundColor = .white.withAlphaComponent(0.6)
numberLabel.isHidden = true
case .finish:
backgroundColor = .yellow
numberLabel.isHidden = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import UIKit
final class QuestionStatusView: UIView {
private var answerTypes: [QuestionStatusCellType] = []
private let emptyTypes: [QuestionStatusCellType] = .init(repeating: .empty, count: 7)
private var questionsCount = 1 {
didSet {
collectionView.reloadData()
}
}

private lazy var collectionView: UICollectionView = makeCollectionView()

Expand All @@ -30,6 +35,10 @@ final class QuestionStatusView: UIView {
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}

func setQuestionsCount(count: Int) {
questionsCount = count
}

private func setupCollectionView() {
collectionView.register(
QuestionStatusCollectionViewCell.self,
Expand Down Expand Up @@ -84,9 +93,9 @@ extension QuestionStatusView: UICollectionViewDataSource {
_ collectionView: UICollectionView,
numberOfItemsInSection section: Int
) -> Int {
return answerTypes.count + 1 + emptyTypes.count
return questionsCount + 1
}

func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
Expand All @@ -97,8 +106,13 @@ extension QuestionStatusView: UICollectionViewDataSource {
) as? QuestionStatusCollectionViewCell else {
return UICollectionViewCell()
}

let allTypes = answerTypes + [.selected] + emptyTypes

var empty: [QuestionStatusCellType] = []
if (questionsCount - answerTypes.count) != 0 {
empty = .init(repeating: .empty, count: questionsCount - answerTypes.count - 1)
}

let allTypes = answerTypes + [.selected] + empty + [.finish]
let row = indexPath.row
cell.configure(with: allTypes[safe: indexPath.row] ?? .empty, row: row)

Expand Down

0 comments on commit 6c5bdcb

Please sign in to comment.