You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.6 KiB
78 lines
2.6 KiB
import 'dart:async';
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:hadi_hoda_flutter/core/params/question_params.dart';
|
|
import 'package:hadi_hoda_flutter/core/status/base_status.dart';
|
|
import 'package:hadi_hoda_flutter/core/widgets/hadith_dialog/hadith_dialog.dart';
|
|
import 'package:hadi_hoda_flutter/features/question/domain/entity/question_entity.dart';
|
|
import 'package:hadi_hoda_flutter/features/question/domain/usecases/get_level_usecase.dart';
|
|
import 'package:hadi_hoda_flutter/features/question/presentation/bloc/question_event.dart';
|
|
import 'package:hadi_hoda_flutter/features/question/presentation/bloc/question_state.dart';
|
|
import 'package:showcaseview/showcaseview.dart';
|
|
|
|
class QuestionBloc extends Bloc<QuestionEvent, QuestionState> {
|
|
/// ------------constructor------------
|
|
QuestionBloc(
|
|
this._getLevelUseCase,
|
|
) : super(const QuestionState()) {
|
|
on<GetLevelEvent>(_getLevelEvent);
|
|
on<ChangeQuestionEvent>(_changeQuestionEvent);
|
|
}
|
|
|
|
/// ------------UseCases------------
|
|
final GetLevelUseCase _getLevelUseCase;
|
|
|
|
/// ------------Variables------------
|
|
final List<GlobalKey> keys = [
|
|
GlobalKey(),
|
|
GlobalKey(),
|
|
GlobalKey(),
|
|
GlobalKey(),
|
|
];
|
|
|
|
|
|
/// ------------Controllers------------
|
|
|
|
/// ------------Functions------------
|
|
void startShowCase({required BuildContext context}) {
|
|
ShowCaseWidget.of(context).startShowCase([keys[1]]);
|
|
}
|
|
|
|
void showHadith({required BuildContext context}) {
|
|
showHadithDialog(context: context);
|
|
}
|
|
|
|
/// ------------Event Calls------------
|
|
FutureOr<void> _getLevelEvent(GetLevelEvent event, Emitter<QuestionState> emit) async {
|
|
await _getLevelUseCase(QuestionParams(id: int.parse(event.id ?? '0'))).then(
|
|
(value) {
|
|
value.fold(
|
|
(data) {
|
|
emit(state.copyWith(
|
|
getQuestionStatus: BaseComplete(''),
|
|
levelEntity: data,
|
|
currentQuestion: data.questions?.first,
|
|
));
|
|
},
|
|
(error) {
|
|
emit(state.copyWith(getQuestionStatus: BaseError(error.errorMessage)));
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
FutureOr<void> _changeQuestionEvent(
|
|
ChangeQuestionEvent event,
|
|
Emitter<QuestionState> emit,
|
|
) async {
|
|
await Future.delayed(Duration(seconds: 1), () {
|
|
final QuestionEntity? findPreQuestion = state.currentQuestion;
|
|
final int findPreIndex = (findPreQuestion?.order ?? 1) - 1;
|
|
final int newIndex = findPreIndex + 1;
|
|
emit(
|
|
state.copyWith(currentQuestion: state.levelEntity?.questions?[newIndex]),
|
|
);
|
|
});
|
|
}
|
|
}
|