Browse Source

fix: download and precache

pull/54/head
AmirrezaChegini 2 weeks ago
parent
commit
e77e4135d6
  1. 11
      lib/core/utils/pre_cache_image.dart
  2. 10
      lib/features/download/data/datasource/download_datasource.dart
  3. 2
      lib/features/download/presentation/bloc/download_bloc.dart
  4. 14
      lib/features/splash/presentation/bloc/splash_bloc.dart

11
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<void> preCacheImages() async {
await Future.wait(
MyAssets.images.map(
(assetPath) => precacheImage(AssetImage(assetPath), MyContext.get),
),
);
}

10
lib/features/download/data/datasource/download_datasource.dart

@ -90,11 +90,11 @@ class DownloadDatasourceImpl implements IDownloadDatasource {
'lang': selectedLanguage, 'lang': selectedLanguage,
}, },
onReceive: (count, total) { 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 { ).then((value) async {
await LocalStorage.saveData( await LocalStorage.saveData(

2
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/status/base_status.dart';
import 'package:hadi_hoda_flutter/core/utils/local_storage.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/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/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_audios_usecase.dart';
import 'package:hadi_hoda_flutter/features/download/domain/usecases/get_images_usecase.dart'; import 'package:hadi_hoda_flutter/features/download/domain/usecases/get_images_usecase.dart';
@ -25,6 +26,7 @@ class DownloadBloc extends Bloc<DownloadEvent, DownloadState> {
this._saveLevelsUseCase, this._saveLevelsUseCase,
) )
: super(const DownloadState()) { : super(const DownloadState()) {
preCacheImages();
on<GetImagesEvent>(_getImagesEvent); on<GetImagesEvent>(_getImagesEvent);
on<GetAudiosEvent>(_getAudiosEvent); on<GetAudiosEvent>(_getAudiosEvent);
on<SaveLevelsEvent>(_saveLevelsEvent); on<SaveLevelsEvent>(_saveLevelsEvent);

14
lib/features/splash/presentation/bloc/splash_bloc.dart

@ -1,8 +1,8 @@
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.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/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_event.dart';
import 'package:hadi_hoda_flutter/features/splash/presentation/bloc/splash_state.dart'; import 'package:hadi_hoda_flutter/features/splash/presentation/bloc/splash_state.dart';
@ -17,19 +17,9 @@ class SplashBloc extends Bloc<SplashEvent, SplashState> {
/// ------------Controllers------------ /// ------------Controllers------------
/// ------------Functions------------ /// ------------Functions------------
Future<void> _precacheAllImages(BuildContext context) async {
await Future.wait(
MyAssets.images.map(
(assetPath) => precacheImage(AssetImage(assetPath), context),
),
);
}
Future<void> goToHomePage(BuildContext context) async { Future<void> goToHomePage(BuildContext context) async {
if (context.mounted) {
await _precacheAllImages(context);
}
await preCacheImages();
await Future.delayed( await Future.delayed(
Duration(seconds: 2), Duration(seconds: 2),
() { () {

Loading…
Cancel
Save