import 'dart:async'; import 'dart:io'; import 'package:bloc/bloc.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:go_router/go_router.dart'; import 'package:hadi_hoda_flutter/common_ui/resources/my_audios.dart'; import 'package:hadi_hoda_flutter/core/constants/my_constants.dart'; import 'package:hadi_hoda_flutter/core/routers/my_routes.dart'; import 'package:hadi_hoda_flutter/core/services/audio_service.dart'; import 'package:hadi_hoda_flutter/core/utils/local_storage.dart'; import 'package:hadi_hoda_flutter/core/utils/my_context.dart'; import 'package:hadi_hoda_flutter/core/utils/storage_path.dart'; import 'package:hadi_hoda_flutter/core/widgets/dialog/about_us_dialog.dart'; import 'package:hadi_hoda_flutter/features/download/domain/entities/download_entity.dart'; import 'package:hadi_hoda_flutter/features/download/domain/usecases/get_last_downloaded_level.dart'; import 'package:hadi_hoda_flutter/features/home/presentation/bloc/home_event.dart'; import 'package:hadi_hoda_flutter/features/home/presentation/bloc/home_state.dart'; import '../../../../core/params/no_params.dart'; class HomeBloc extends Bloc { /// ------------constructor------------ HomeBloc(this._mainAudioService, this._effectAudioService, this._getLastDownloadedLevel) : super(const HomeState()) { volumeStream = _mainAudioService.volumeStream(); playMusic(); preCacheQuestionGifs(); preCacheAnswerGifs(); on(_getHomeEvent); } /// ------------UseCases------------ final GetLastDownloadedLevel _getLastDownloadedLevel; /// ------------Variables------------ late final Stream volumeStream; /// ------------Controllers------------ final AudioService _mainAudioService; final AudioService _effectAudioService; /// ------------Functions------------ void goToLevelPage(BuildContext context) async { final lastDownloadedLevel = (await _getLastDownloadedLevel(NoParams())).data ?? 0; final hasFirstDownload = MyConstants.firstDownloadBatchCount <= lastDownloadedLevel; if(!context.mounted) return; if (hasFirstDownload) { context.goNamed(Routes.levelPage); } else { context.goNamed( Routes.downloadPage, extra: const DownloadPageConfig( downloadToLevel: MyConstants.firstDownloadBatchCount, redirectTo: Routes.homePage ), ); } } Future getLastDownloadedLevel() async { final result = await _getLastDownloadedLevel(NoParams()); return result.data ?? 0; } void goToLanguagePage(BuildContext context) { context.pushNamed(Routes.languagePage); } void showAboutUs(BuildContext context) { showAboutUsDialog(context: context); } Future changeMute() async { await Future.wait([ _mainAudioService.changeMute(), _effectAudioService.changeMute(), ]); } Future playMusic() async { await Future.wait([ _mainAudioService.setAudio(assetPath: MyAudios.home), _mainAudioService.setLoopMode(isLoop: true), ]); await _mainAudioService.play(); } Future preCacheQuestionGifs() async { try { final Directory directory = Directory( '${StoragePath.documentDir}/question_image', ); final List files = directory.listSync(); await Future.wait( files.map( (file) => precacheImage(FileImage(File(file.path)), MyContext.get), ), ); } catch (e) { if (kDebugMode) { print('$e'); } } } Future preCacheAnswerGifs() async { try { final Directory directory = Directory( '${StoragePath.documentDir}/answer_image', ); final List files = directory.listSync(); await Future.wait( files.map( (file) => precacheImage(FileImage(File(file.path)), MyContext.get), ), ); } catch (e) { if (kDebugMode) { print('$e'); } } } /// ------------Api Calls------------ FutureOr _getHomeEvent(event, emit) async {} }