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