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/constants/my_constants.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/core/utils/local_storage.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'; import 'package:hadi_hoda_flutter/features/level/presentation/ui/widgets/level_widget.dart'; class LevelBloc extends Bloc { /// ------------constructor------------ LevelBloc( this._getLeveslUseCase, ) : super(const LevelState()) { on(_getLevelListEvent); on(_setCurrentLevelEvent); on(_startScrollEvent); } @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(); } LevelType getLevelType(int index) { final int currentLevel = int.parse( LocalStorage.readData(key: MyConstants.currentLevel) ?? '1', ); if (index < currentLevel) { return LevelType.finished; } else if (index == currentLevel) { return LevelType.current; } else { return LevelType.unFinished; } } /// ------------Api Calls------------ FutureOr _getLevelListEvent(GetLevelListEvent event, Emitter emit) async { final int currentLevel = int.parse( LocalStorage.readData(key: MyConstants.currentLevel) ?? '1', ); 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.singleWhere((e) => e.order == currentLevel), )); add(StartScrollEvent()); }, (error) {}, ); }); } FutureOr _startScrollEvent( StartScrollEvent event, Emitter emit, ) async { final int currentLevel = int.parse( LocalStorage.readData(key: MyConstants.currentLevel) ?? '1', ); await Future.delayed(const Duration(seconds: 1)); if (scrollController.hasClients) { if(currentLevel < 9){ scrollController.animateTo( scrollController.position.maxScrollExtent, duration: const Duration(milliseconds: 500), // Note: 500 seconds is very long. curve: Curves.easeInOut, ); } else if( currentLevel > 8 && currentLevel < 14){ scrollController.animateTo( scrollController.position.maxScrollExtent / 2, duration: const Duration(milliseconds: 500), // Note: 500 seconds is very long. curve: Curves.easeInOut, ); } } } FutureOr _setCurrentLevelEvent(SetCurrentLevelEvent event, Emitter emit) async { final String? currentLevel = LocalStorage.readData( key: MyConstants.currentLevel); if (currentLevel == null || currentLevel.isEmpty) { await LocalStorage.saveData(key: MyConstants.currentLevel, value: '1'); } add(GetLevelListEvent()); } }