Line data Source code
1 : import 'dart:collection';
2 : import 'package:amadeus_proto/api/routes/exercises.dart';
3 : import 'package:amadeus_proto/exercises/exercises_provider.dart';
4 :
5 : /// A provider that is extends from the [ExerciseProvider]
6 : ///
7 : /// It provides the initialization of the data ([initializeData]) loaded from JSON file
8 : class SingleChoiceProvider extends ExerciseProvider {
9 : ListQueue<int> _answerQueue = ListQueue<int>();
10 0 : ListQueue<int> get answerQueue => _answerQueue;
11 0 : set answerQueue(ListQueue<int> value) {
12 0 : _answerQueue = value;
13 0 : notifyListeners();
14 : }
15 :
16 : List<String> _questions = [];
17 0 : List<String> get questions => _questions;
18 0 : set questions(List<String> value) {
19 0 : _questions = value;
20 0 : notifyListeners();
21 : }
22 :
23 : /// Returns `true` if the all the answers is correct or when [lives] fall to 0
24 0 : @override
25 : bool checkEndGame() {
26 0 : if (exerciseInfo != null && _answerQueue.isEmpty) {
27 0 : apiBase.completeExercise(exerciseInfo!.id);
28 : }
29 0 : return (lives == 0 || _answerQueue.isEmpty);
30 : }
31 :
32 : /// Initialize the [data] from the JSON file in the provider
33 : ///
34 : /// We load the `choices` and `assetPath` from JSON file to shuffle it.
35 : /// Then we load [choices] and [assetPath] to the provider after the
36 : /// shuffle these two lists in the same order.
37 : ///
38 : /// Then, we load the [lives] and [question] from the JSON file.
39 : ///
40 : /// Finally, we set the [isInitialized] to true, to prevent multiple
41 : /// data initialization
42 0 : void initializeData(dynamic data) {
43 0 : final List<String> choicesData = data["choices"].cast<String>();
44 0 : final List<int> indices = List.generate(choicesData.length, (i) => i);
45 :
46 0 : indices.shuffle();
47 0 : _answerQueue.addAll(indices);
48 :
49 0 : questions = data["questionAssetPath"].cast<String>();
50 0 : choices = indices.map((i) => choicesData[i]).toSet().toList();
51 0 : questions = indices.map((i) => questions[i]).toSet().toList();
52 :
53 0 : lives = data["lives"];
54 0 : question = data["question"];
55 0 : answer = _answerQueue.first;
56 0 : isInitialized = true;
57 : }
58 :
59 : /// Verify if the selected [selectedAnswer] is the correct one.
60 : ///
61 : /// If the choosen [selectedAnswer] is not the [answer], decrease the
62 : /// number of [lives] by one.
63 : /// Otherwise, add the actual answer to the [_correctAnswers] list
64 : /// and choose the next question.
65 0 : @override
66 : void verify(dynamic data) {
67 0 : selectedAnswer = data;
68 :
69 0 : if (selectedAnswer == answer) {
70 0 : progress++;
71 0 : _answerQueue.removeFirst();
72 0 : correctAnswer = true;
73 : } else {
74 0 : lives -= (lives > 0) ? 1 : 0;
75 0 : _answerQueue.addLast(_answerQueue.removeFirst());
76 0 : correctAnswer = false;
77 : }
78 :
79 0 : if (nextQuestion && _answerQueue.isNotEmpty) {
80 0 : answer = _answerQueue.first;
81 0 : nextQuestion = false;
82 : }
83 0 : selectedAnswer = null;
84 : }
85 :
86 : /// Reset the every data from the provider
87 0 : @override
88 : void reset() {
89 0 : isReset = false;
90 0 : isInitialized = false;
91 0 : lives = 3;
92 0 : answer = 0;
93 0 : displayDialog = false;
94 0 : selectedAnswer = null;
95 0 : nextQuestion = false;
96 0 : progress = 0;
97 0 : correctAnswer = null;
98 0 : _answerQueue.clear();
99 : }
100 : }
|