From e77e4135d6b7cc68336a60295a49763c5c5dd019 Mon Sep 17 00:00:00 2001 From: AmirrezaChegini Date: Tue, 16 Dec 2025 11:40:37 +0330 Subject: [PATCH] fix: download and precache --- lib/core/utils/pre_cache_image.dart | 11 +++++++++++ .../data/datasource/download_datasource.dart | 10 +++++----- .../download/presentation/bloc/download_bloc.dart | 2 ++ .../splash/presentation/bloc/splash_bloc.dart | 14 ++------------ 4 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 lib/core/utils/pre_cache_image.dart diff --git a/lib/core/utils/pre_cache_image.dart b/lib/core/utils/pre_cache_image.dart new file mode 100644 index 0000000..3651b51 --- /dev/null +++ b/lib/core/utils/pre_cache_image.dart @@ -0,0 +1,11 @@ +import 'package:flutter/material.dart'; +import 'package:hadi_hoda_flutter/common_ui/resources/my_assets.dart'; +import 'package:hadi_hoda_flutter/core/utils/my_context.dart'; + +Future preCacheImages() async { + await Future.wait( + MyAssets.images.map( + (assetPath) => precacheImage(AssetImage(assetPath), MyContext.get), + ), + ); +} diff --git a/lib/features/download/data/datasource/download_datasource.dart b/lib/features/download/data/datasource/download_datasource.dart index 843d969..1b452fb 100644 --- a/lib/features/download/data/datasource/download_datasource.dart +++ b/lib/features/download/data/datasource/download_datasource.dart @@ -90,11 +90,11 @@ class DownloadDatasourceImpl implements IDownloadDatasource { 'lang': selectedLanguage, }, onReceive: (count, total) { - streamController.add(DownloadEntity( - count: count / 1, - total: total / 1, - percent: (count / total) * 100, - )); + // streamController.add(DownloadEntity( + // count: count / 1, + // total: total / 1, + // percent: (count / total) * 100, + // )); }, ).then((value) async { await LocalStorage.saveData( diff --git a/lib/features/download/presentation/bloc/download_bloc.dart b/lib/features/download/presentation/bloc/download_bloc.dart index 4c94b16..ffe9310 100644 --- a/lib/features/download/presentation/bloc/download_bloc.dart +++ b/lib/features/download/presentation/bloc/download_bloc.dart @@ -8,6 +8,7 @@ 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/core/utils/my_context.dart'; +import 'package:hadi_hoda_flutter/core/utils/pre_cache_image.dart'; import 'package:hadi_hoda_flutter/features/download/domain/entities/download_entity.dart'; import 'package:hadi_hoda_flutter/features/download/domain/usecases/get_audios_usecase.dart'; import 'package:hadi_hoda_flutter/features/download/domain/usecases/get_images_usecase.dart'; @@ -25,6 +26,7 @@ class DownloadBloc extends Bloc { this._saveLevelsUseCase, ) : super(const DownloadState()) { + preCacheImages(); on(_getImagesEvent); on(_getAudiosEvent); on(_saveLevelsEvent); diff --git a/lib/features/splash/presentation/bloc/splash_bloc.dart b/lib/features/splash/presentation/bloc/splash_bloc.dart index 5fc686b..b4270a2 100644 --- a/lib/features/splash/presentation/bloc/splash_bloc.dart +++ b/lib/features/splash/presentation/bloc/splash_bloc.dart @@ -1,8 +1,8 @@ import 'package:bloc/bloc.dart'; import 'package:flutter/cupertino.dart'; import 'package:go_router/go_router.dart'; -import 'package:hadi_hoda_flutter/common_ui/resources/my_assets.dart'; import 'package:hadi_hoda_flutter/core/routers/my_routes.dart'; +import 'package:hadi_hoda_flutter/core/utils/pre_cache_image.dart'; import 'package:hadi_hoda_flutter/features/splash/presentation/bloc/splash_event.dart'; import 'package:hadi_hoda_flutter/features/splash/presentation/bloc/splash_state.dart'; @@ -17,19 +17,9 @@ class SplashBloc extends Bloc { /// ------------Controllers------------ /// ------------Functions------------ - Future _precacheAllImages(BuildContext context) async { - await Future.wait( - MyAssets.images.map( - (assetPath) => precacheImage(AssetImage(assetPath), context), - ), - ); - } Future goToHomePage(BuildContext context) async { - if (context.mounted) { - await _precacheAllImages(context); - } - + await preCacheImages(); await Future.delayed( Duration(seconds: 2), () {