Line data Source code
1 : import 'package:amadeus_proto/api/routes/exercises.dart';
2 : import 'package:amadeus_proto/exercises/exercises_provider.dart';
3 :
4 : class NotePlacementProvider extends ExerciseProvider {
5 : int _index = 0;
6 : int _goodAnswer = 0;
7 : int _nbAnswer = 0;
8 :
9 : List<Map<String, dynamic>> _answerData = [];
10 0 : List<Map<String, dynamic>> get answerData => _answerData;
11 0 : set answerData(List<Map<String, dynamic>> value) {
12 0 : _answerData = 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 : List<String> _choicesPath = [];
24 0 : List<String> get choicesPath => _choicesPath;
25 0 : set choicesPath(List<String> value) {
26 0 : _choicesPath = value;
27 0 : notifyListeners();
28 : }
29 :
30 0 : int get index => _index;
31 0 : int get nbAnswer => _nbAnswer;
32 0 : int get goodAnswer => _goodAnswer;
33 :
34 0 : void selectGoodAnswer() {
35 0 : _nbAnswer++;
36 0 : if (_nbAnswer == 2) {
37 0 : setNextQuestion();
38 : }
39 : }
40 :
41 : /// Initialize the [data] from the JSON file in the provider
42 : ///
43 : /// We load the `choices` and `assetPath` from JSON file to shuffle it.
44 : /// Then we load :
45 : /// - [choices], [choicesPath] that represents the data and its image
46 : /// - [questions] corresponds to the question image
47 : /// - [answerData] that correspond the data and image answer for each question
48 : /// to the provider
49 : ///
50 : /// Then, we load the [lives] and [question] from the JSON file.
51 : ///
52 : /// Finally, we set the [isInitialized] to true, to prevent multiple
53 : /// data initialization
54 0 : void initializeData(dynamic data) {
55 0 : answerData = (data["answer"] as List)
56 0 : .map((dynamic e) => e as Map<String, dynamic>)
57 0 : .toList();
58 :
59 0 : isInitialized = true;
60 0 : lives = data["lives"];
61 0 : question = data["question"];
62 0 : questions = data["questionAssetPath"].cast<String>();
63 0 : choicesPath = data["choicePath"].cast<String>();
64 0 : choices = data["choices"].cast<String>();
65 : }
66 :
67 : /// Returns `true` if :
68 : /// - The number of [_goodAnswer] is equal to the length of [_questions]
69 : /// - The number [lives] fall to 0
70 0 : @override
71 : bool checkEndGame() {
72 0 : if (exerciseInfo != null && _goodAnswer == _questions.length) {
73 0 : apiBase.completeExercise(exerciseInfo!.id);
74 : }
75 0 : return (_goodAnswer == _questions.length || lives == 0);
76 : }
77 :
78 : /// Verify if the selected [selectedAnswer] is the correct one.
79 : ///
80 : /// If the choosen [selectedAnswer] is not the [answer], decrease the
81 : /// number of [lives] by one.
82 : /// Otherwise, add the actual answer to the [_correctAnswers] list
83 : /// and choose the next question.
84 0 : void setNextQuestion() {
85 0 : if (index < _questions.length - 1) {
86 0 : _index++;
87 : }
88 0 : _goodAnswer++;
89 0 : _nbAnswer = 0;
90 0 : notifyListeners();
91 : }
92 :
93 0 : int progressRatio() {
94 0 : return goodAnswer;
95 : }
96 :
97 0 : @override
98 : void reset() {
99 0 : lives = 3;
100 0 : isInitialized = false;
101 0 : _nbAnswer = 0;
102 0 : _goodAnswer = 0;
103 0 : _index = 0;
104 0 : correctAnswer = null;
105 0 : displayDialog = false;
106 : }
107 : }
|