import 'dart:async'; import 'package:bloc/bloc.dart'; import 'package:flutter/cupertino.dart'; import 'package:go_router/go_router.dart'; import 'package:hadi_hoda_flutter/core/params/level_params.dart'; import 'package:hadi_hoda_flutter/core/routers/my_routes.dart'; import 'package:hadi_hoda_flutter/core/status/base_status.dart'; import 'package:hadi_hoda_flutter/features/level/domain/entity/level_entity.dart'; import 'package:hadi_hoda_flutter/features/level/domain/entity/level_location.dart'; import 'package:hadi_hoda_flutter/features/level/domain/usecases/get_levels_usecase.dart'; import 'package:hadi_hoda_flutter/features/level/presentation/bloc/level_event.dart'; import 'package:hadi_hoda_flutter/features/level/presentation/bloc/level_state.dart'; class LevelBloc extends Bloc { /// ------------constructor------------ LevelBloc( this._getLeveslUseCase, ) : super(const LevelState()) { on(_getLevelListEvent); } @override Future close() { scrollController.dispose(); return super.close(); } /// ------------UseCases------------ final GetLevelsUseCase _getLeveslUseCase; /// ------------Variables------------ final List bottomLocationList = [ LevelLocation(bottom: -30, left: 30, index: 1), LevelLocation(bottom: 50, left: 100, index: 2), LevelLocation(bottom: 150, left: 60, index: 3), LevelLocation(bottom: 210, left: 120, index: 4), LevelLocation(bottom: 250, right: 60, index: 5), LevelLocation(top: 150, right: 40, index: 6), LevelLocation(top: 70, right: 70, index: 7), LevelLocation(top: -20, right: 70, index: 8), ]; final List topLocationList = [ LevelLocation(bottom: 30, right: 80, index: 9), LevelLocation(bottom: 70, left: 20, index: 10), LevelLocation(bottom: 150, left: 50, index: 11), LevelLocation(bottom: 180, left: 140, index: 12), LevelLocation(bottom: 260, right: 20, index: 13), LevelLocation(bottom: 370, right: 30, index: 14), LevelLocation(bottom: 420, left: 40, index: 15), LevelLocation(top: 410, left: 0, index: 16), LevelLocation(top: 320, left: 60, index: 17), LevelLocation(top: 220, left: 80, index: 18), LevelLocation(top: 130, left: 20, index: 19), LevelLocation(top: 50, left: 70, index: 20), ]; final List bottom8LevelList = []; final List top12LevelList = []; /// ------------Controllers------------ final ScrollController scrollController = ScrollController(); /// ------------Functions------------ void goToQuestionPage(BuildContext context, LevelEntity level){ context.pushNamed( Routes.questionPage, pathParameters: { 'id': '${level.id}', }, ); } void goToHomePage(BuildContext context){ context.pop(); } /// ------------Api Calls------------ FutureOr _getLevelListEvent(GetLevelListEvent event, Emitter emit) async { await _getLeveslUseCase(LevelParams()).then((value) { value.fold( (data) async { bottom8LevelList.addAll(data.take(8)); if(data.length > 8){ top12LevelList.addAll(data.sublist(8, data.length)); } emit(state.copyWith( getLevelStatus: const BaseComplete(''), chooseLevel: data.first, )); await Future.delayed(Duration(milliseconds: 500), () { scrollController.animateTo( scrollController.position.maxScrollExtent, duration: Duration(seconds: 500), curve: Curves.easeInOut, ); }); }, (error) {}, ); }); } }