From 25874a711d35df9d0a82cd53ccc2706e99f76bf7 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sun, 1 Nov 2020 21:48:51 +0100 Subject: [PATCH] Refactored practice system again --- lib/pages/practice/PracticeThing.dart | 10 ++ lib/pages/practice/PracticeThingMonth.dart | 76 ++++++++++ .../month/MonthPracticeGenerator.dart | 24 --- .../practice/month/MonthPracticePage.dart | 138 ++++-------------- 4 files changed, 116 insertions(+), 132 deletions(-) create mode 100644 lib/pages/practice/PracticeThing.dart create mode 100644 lib/pages/practice/PracticeThingMonth.dart delete mode 100644 lib/pages/practice/month/MonthPracticeGenerator.dart diff --git a/lib/pages/practice/PracticeThing.dart b/lib/pages/practice/PracticeThing.dart new file mode 100644 index 0000000..0b2833d --- /dev/null +++ b/lib/pages/practice/PracticeThing.dart @@ -0,0 +1,10 @@ +import 'package:flutter/material.dart'; + +abstract class PracticeThing { + bool answer(int answer); + void next(); + List getBoolAnswers(); + Text getCurrentQuestionText(); + Text getLastAnswerText(bool showCorrect); + Text getSecondLastAnswerText(bool showCorrect); +} \ No newline at end of file diff --git a/lib/pages/practice/PracticeThingMonth.dart b/lib/pages/practice/PracticeThingMonth.dart new file mode 100644 index 0000000..d8d0d73 --- /dev/null +++ b/lib/pages/practice/PracticeThingMonth.dart @@ -0,0 +1,76 @@ +import 'dart:math'; + +import 'package:flutter/material.dart'; +import 'package:ohthatsa/pages/practice/PracticeThing.dart'; +import 'package:ohthatsa/pages/practice/month/MonthPracticeAnswer.dart'; +import 'package:ohthatsa/util/Months.dart'; + +class PracticeThingMonth extends PracticeThing { + final random = Random(); + final List answers = new List(); + Month current; + + PracticeThingMonth(){ + next(); + } + @override + bool answer(int answer) { + answers.add(MonthPracticeAnswer(current, answer)); + return current.value == answer; + } + + @override + void next() { + current = Months.getFromInt(random.nextInt(Months.length)); + } + + List getBoolAnswers(){ + return answers.map((answer) => answer.month.value == answer.answer).toList(); + } + + Text getCurrentQuestionText(){ + return Text( + current.toString(), + style: TextStyle( + fontSize: 30 + ) + ); + } + + Text getLastAnswerText(bool showCorrect){ + String text = "-"; + Color color = Colors.red; + if(answers.length >= 1){ + final answer = answers[answers.length - 1]; + text = answer.month.toString() + (showCorrect ? ": ${answer.month.value}" : ""); + if(answer.month.value == answer.answer){ + color = Colors.green; + } + } + return Text( + text, + style: TextStyle( + fontSize: 15, + color: color + ) + ); + } + Text getSecondLastAnswerText(bool showCorrect){ + String text = "-"; + Color color = Colors.red; + if(answers.length >= 2){ + final answer = answers[answers.length - 2]; + text = answer.month.toString() + (showCorrect ? ": ${answer.month.value}" : ""); + if(answer.month.value == answer.answer){ + color = Colors.green; + } + } + return Text( + text, + style: TextStyle( + fontSize: 10, + color: color + ) + ); + } +} \ No newline at end of file diff --git a/lib/pages/practice/month/MonthPracticeGenerator.dart b/lib/pages/practice/month/MonthPracticeGenerator.dart deleted file mode 100644 index 22298c2..0000000 --- a/lib/pages/practice/month/MonthPracticeGenerator.dart +++ /dev/null @@ -1,24 +0,0 @@ -import 'dart:math'; - -import 'package:ohthatsa/pages/practice/PracticeGenerator.dart'; -import 'package:ohthatsa/util/Months.dart'; -import 'package:ohthatsa/util/Extensions.dart'; - -class MonthPracticeGenerator extends PracticeGenerator { - final random = Random(); - Month current; - MonthPracticeGenerator(){ - next(); - } - bool check(int value){ - return current.value == value; - } - - void next(){ - current = Months.getFromInt(random.nextInt(Months.length)); - } - - String toString(){ - return current.toString().capitalize(); - } -} \ No newline at end of file diff --git a/lib/pages/practice/month/MonthPracticePage.dart b/lib/pages/practice/month/MonthPracticePage.dart index 343e735..feb4e3b 100644 --- a/lib/pages/practice/month/MonthPracticePage.dart +++ b/lib/pages/practice/month/MonthPracticePage.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:ohthatsa/pages/practice/PracticeSetup.dart'; -import 'package:ohthatsa/pages/practice/month/MonthPracticeGenerator.dart'; +import 'package:ohthatsa/pages/practice/PracticeThing.dart'; +import 'package:ohthatsa/pages/practice/PracticeThingMonth.dart'; import 'package:ohthatsa/AppDrawer.dart'; import 'package:ohthatsa/util/Extensions.dart'; @@ -18,117 +19,27 @@ class MonthPracticePage extends StatefulWidget { class _MonthPracticeState extends State { int _startCount = 0; bool _showCorrect = true; + PracticeThing practiceThing; int _count = 0; int _correct = 0; int _incorrect = 0; - static final practiceGenerator = MonthPracticeGenerator(); - List answers = List(); _MonthPracticeState(PracticeSetup practiceSetup){ - _startCount = practiceSetup.count; - _showCorrect = practiceSetup.showCorrect; - } - Widget getAnswerRow(){ - List answerBoxes = List(); - for(MonthPracticeAnswer answer in answers){ - Color c = Colors.green; - if(answer.month.value != answer.answer){ - c = Colors.red; - } - answerBoxes.add( - Expanded( - child: Container( - height: 50, - color: c - ) - ) - ); - } - for(int i in Iterable.generate(_startCount - _count)) { - answerBoxes.add( - Expanded( - child: Container( - height: 50, - color: Colors.blue, - ) - ) - ); - } - return Row(children: answerBoxes); + this._startCount = practiceSetup.count; + this._showCorrect = practiceSetup.showCorrect; + this.practiceThing = PracticeThingMonth(); } - Widget getQuestions(){ - List questions = List(); - if(answers.length >= 2){ - MonthPracticeAnswer answer = answers[answers.length - 2]; - StringBuffer tmp = StringBuffer("${answer.month.string.capitalize()}"); - if (_showCorrect){ - tmp.write(": ${answer.month.value}"); - } - questions.add( - Opacity( - opacity: 0.3, - child: Text( - tmp.toString(), - style: TextStyle( - fontSize: 10, - color: answer.answer == answer.month.value ? Colors.green : Colors.red - ) - ) - ) - ); - } else { - questions.add( - Opacity( - opacity: 0.6, - child: Text( - "-", - style: TextStyle(fontSize: 10) - ) - ) - ); - } - if(answers.length >= 1){ - MonthPracticeAnswer answer = answers[answers.length - 1]; - StringBuffer tmp = StringBuffer("${answer.month.string.capitalize()}"); - if (_showCorrect){ - tmp.write(": ${answer.month.value}"); - } - questions.add( - Opacity( - opacity: 0.6, - child: Text( - tmp.toString(), - style: TextStyle( - fontSize: 15, - color: answer.answer == answer.month.value ? Colors.green : Colors.red - ) - ) - ) - ); - } else { - questions.add( - Opacity( - opacity: 0.6, - child: Text( - "-", - style: TextStyle(fontSize: 15) - ) - ) - ); - } - questions.add( - Text( - practiceGenerator.toString(), - style: TextStyle( - fontSize: 30, - ) - ) - ); - return Column( - children: questions - ); + Widget getAnswerRow(){ + List answerBoxes = List(); + answerBoxes.addAll(practiceThing.getBoolAnswers().map( + (isCorrect) => Expanded(child: Container(height: 50, color: isCorrect ? Colors.green : Colors.red)) + )); + answerBoxes.addAll(Iterable.generate(_startCount - _count).map( + (_) => Expanded(child: Container(height: 50, color: Colors.blue)) + )); + return Row(children: answerBoxes); } Widget getButtons(){ @@ -240,7 +151,19 @@ class _MonthPracticeState extends State { ) ], ), - getQuestions(), + Column( + children: [ + Opacity( + opacity: 0.3, + child: practiceThing.getSecondLastAnswerText(_showCorrect) + ), + Opacity( + opacity: 0.6, + child: practiceThing.getLastAnswerText(_showCorrect) + ), + practiceThing.getCurrentQuestionText() + ] + ), getButtons(), Align( alignment: FractionalOffset.bottomCenter, @@ -254,15 +177,14 @@ class _MonthPracticeState extends State { } void checkMonth(int answer){ - if(practiceGenerator.check(answer)){ + if(practiceThing.answer(answer)){ _correct += 1; } else { _incorrect += 1; } _count += 1; - answers.add(MonthPracticeAnswer(practiceGenerator.current, answer)); setState(() => { - practiceGenerator.next() + practiceThing.next() }); if((_startCount - _count) == 0) { showDialog(