2024-05-22 20:48:44 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
|
|
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
|
|
|
|
|
import 'package:rxdart/rxdart.dart';
|
|
|
|
|
|
|
|
|
|
import 'question_states/input_buttons_test_state.dart';
|
|
|
|
|
import 'question_states/simple_test_state.dart';
|
|
|
|
|
import 'question_states/test_question_state.dart';
|
|
|
|
|
|
|
|
|
|
class TestHolder {
|
|
|
|
|
final StreamController<_TestState?> _streamController =
|
|
|
|
|
StreamController.broadcast();
|
|
|
|
|
_TestState __state;
|
|
|
|
|
|
|
|
|
|
TestHolder() : __state = _TestState({});
|
|
|
|
|
|
2024-06-08 23:34:38 +00:00
|
|
|
void skip(int id) {
|
|
|
|
|
final state = testQuestionState(id);
|
|
|
|
|
switch (state?.testType) {
|
|
|
|
|
case TestQuestionType.simple:
|
|
|
|
|
state as SimpleTestState;
|
|
|
|
|
setQuestionState(id, state.copyWith(isSkipped: true));
|
|
|
|
|
break;
|
|
|
|
|
case TestQuestionType.input_buttons:
|
|
|
|
|
state as InputButtonsTestState;
|
|
|
|
|
setQuestionState(id, state.copyWith(isSkipped: true));
|
|
|
|
|
break;
|
|
|
|
|
case TestQuestionType.matrix:
|
|
|
|
|
// TODO: Handle this case.
|
|
|
|
|
break;
|
|
|
|
|
case TestQuestionType.match:
|
|
|
|
|
// TODO: Handle this case.
|
|
|
|
|
break;
|
|
|
|
|
case TestQuestionType.undefined:
|
|
|
|
|
// TODO: Handle this case.
|
|
|
|
|
case null:
|
|
|
|
|
// TODO: Handle this case.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 20:48:44 +00:00
|
|
|
void setQuestionState(int id, TestQuestionState state) => _state = _TestState(
|
|
|
|
|
Map.from(_state.questions..[id] = state),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
|
_state = _TestState({});
|
|
|
|
|
_streamController.add(_state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setQuestions(List<AbstractTestQuestion> questions) {
|
|
|
|
|
final stateQuestions = <int, TestQuestionState>{};
|
|
|
|
|
final seed = Random().nextInt(999);
|
|
|
|
|
for (final question in questions) {
|
|
|
|
|
switch (question.questionType) {
|
|
|
|
|
case TestQuestionType.simple:
|
2024-06-11 00:28:57 +00:00
|
|
|
stateQuestions[question.id!] = SimpleTestState(
|
|
|
|
|
seed: seed,
|
|
|
|
|
word: question.word,
|
|
|
|
|
);
|
2024-05-22 20:48:44 +00:00
|
|
|
case TestQuestionType.input_buttons:
|
2024-06-11 00:28:57 +00:00
|
|
|
stateQuestions[question.id!] = InputButtonsTestState(
|
|
|
|
|
seed: seed,
|
|
|
|
|
word: question.word,
|
|
|
|
|
);
|
2024-05-22 20:48:44 +00:00
|
|
|
case TestQuestionType.matrix:
|
|
|
|
|
// TODO: Handle this case.
|
|
|
|
|
case TestQuestionType.match:
|
|
|
|
|
// TODO: Handle this case.
|
|
|
|
|
case TestQuestionType.undefined:
|
|
|
|
|
// TODO: Handle this case.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_state = _TestState(stateQuestions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TestQuestionState? testQuestionState(int id) => _state.questions[id];
|
|
|
|
|
|
|
|
|
|
_TestState get state => _state;
|
|
|
|
|
|
|
|
|
|
_TestState get _state => __state;
|
|
|
|
|
|
|
|
|
|
void set _state(_TestState state) {
|
|
|
|
|
__state = state;
|
|
|
|
|
_streamController.add(__state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<_TestState?> get asStream =>
|
|
|
|
|
_streamController.stream.startWith(_state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _TestState {
|
|
|
|
|
final Map<int, TestQuestionState> questions;
|
|
|
|
|
|
|
|
|
|
_TestState(this.questions);
|
|
|
|
|
}
|