mnemo_cards/lib/features/tests/test_state_holder.dart

106 lines
2.9 KiB
Dart
Raw Normal View History

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;
2024-06-12 23:00:13 +00:00
TestHolder() : __state = _TestState({}, null);
2024-05-22 20:48:44 +00:00
2024-06-08 23:34:38 +00:00
void skip(int id) {
final state = testQuestionState(id);
2024-06-12 23:00:13 +00:00
print('Skipping $id ${state} ${state?.testType}');
2024-06-08 23:34:38 +00:00
switch (state?.testType) {
case TestQuestionType.simple:
state as SimpleTestState;
2024-06-12 23:00:13 +00:00
print('Skipped $id ${state.word}');
2024-06-08 23:34:38 +00:00
setQuestionState(id, state.copyWith(isSkipped: true));
break;
case TestQuestionType.input_buttons:
state as InputButtonsTestState;
2024-06-12 23:00:13 +00:00
print('Skipped $id ${state.word}');
2024-06-08 23:34:38 +00:00
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-06-12 23:00:13 +00:00
void setQuestionState(int id, TestQuestionState state) {
_state = _TestState(
Map.from(_state.questions..[id] = state),
_state.sessionToken,
);
}
2024-05-22 20:48:44 +00:00
void clear() {
2024-06-12 23:00:13 +00:00
_state = _TestState({}, null);
2024-05-22 20:48:44 +00:00
_streamController.add(_state);
}
2024-06-12 23:00:13 +00:00
void setQuestions(
List<AbstractTestQuestion> questions, String? sessionToken) {
2024-05-22 20:48:44 +00:00
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.
}
}
2024-06-12 23:00:13 +00:00
_state = _TestState(stateQuestions, sessionToken);
2024-05-22 20:48:44 +00:00
}
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 {
2024-06-12 23:00:13 +00:00
final String? sessionToken;
2024-05-22 20:48:44 +00:00
final Map<int, TestQuestionState> questions;
2024-06-12 23:00:13 +00:00
_TestState(this.questions, this.sessionToken);
2024-05-22 20:48:44 +00:00
}