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 { /// ------------constructor------------ QuestionBloc( this._getLevelUseCase, ) : super(const QuestionState()) { on(_getLevelEvent); on(_changeQuestionEvent); } /// ------------UseCases------------ final GetLevelUseCase _getLevelUseCase; /// ------------Variables------------ final List 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 _getLevelEvent(GetLevelEvent event, Emitter 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 _changeQuestionEvent( ChangeQuestionEvent event, Emitter 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]), ); }); } }