Refactored practice system again
This commit is contained in:
parent
3b0c87328c
commit
25874a711d
4 changed files with 116 additions and 132 deletions
10
lib/pages/practice/PracticeThing.dart
Normal file
10
lib/pages/practice/PracticeThing.dart
Normal file
|
@ -0,0 +1,10 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
abstract class PracticeThing {
|
||||
bool answer(int answer);
|
||||
void next();
|
||||
List<bool> getBoolAnswers();
|
||||
Text getCurrentQuestionText();
|
||||
Text getLastAnswerText(bool showCorrect);
|
||||
Text getSecondLastAnswerText(bool showCorrect);
|
||||
}
|
76
lib/pages/practice/PracticeThingMonth.dart
Normal file
76
lib/pages/practice/PracticeThingMonth.dart
Normal file
|
@ -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<MonthPracticeAnswer> answers = new List<MonthPracticeAnswer>();
|
||||
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<bool> 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
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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<MonthPracticePage> {
|
||||
int _startCount = 0;
|
||||
bool _showCorrect = true;
|
||||
PracticeThing practiceThing;
|
||||
|
||||
int _count = 0;
|
||||
int _correct = 0;
|
||||
int _incorrect = 0;
|
||||
static final practiceGenerator = MonthPracticeGenerator();
|
||||
List<MonthPracticeAnswer> answers = List<MonthPracticeAnswer>();
|
||||
|
||||
_MonthPracticeState(PracticeSetup practiceSetup){
|
||||
_startCount = practiceSetup.count;
|
||||
_showCorrect = practiceSetup.showCorrect;
|
||||
}
|
||||
Widget getAnswerRow(){
|
||||
List<Widget> answerBoxes = List<Widget>();
|
||||
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<Widget> questions = List<Widget>();
|
||||
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<Widget> answerBoxes = List<Widget>();
|
||||
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<MonthPracticePage> {
|
|||
)
|
||||
],
|
||||
),
|
||||
getQuestions(),
|
||||
Column(
|
||||
children: <Widget>[
|
||||
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<MonthPracticePage> {
|
|||
}
|
||||
|
||||
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(
|
||||
|
|
Loading…
Add table
Reference in a new issue