You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							159 lines
						
					
					
						
							5.2 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							159 lines
						
					
					
						
							5.2 KiB
						
					
					
				| import 'dart:async'; | |
| import 'dart:io'; | |
| 
 | |
| import 'package:flutter_archive/flutter_archive.dart'; | |
| import 'package:hadi_hoda_flutter/core/constants/my_api.dart'; | |
| import 'package:hadi_hoda_flutter/core/constants/my_constants.dart'; | |
| import 'package:hadi_hoda_flutter/core/error_handler/my_exception.dart'; | |
| import 'package:hadi_hoda_flutter/core/network/http_request.dart'; | |
| import 'package:hadi_hoda_flutter/core/response/base_response.dart'; | |
| import 'package:hadi_hoda_flutter/core/utils/local_storage.dart'; | |
| import 'package:hadi_hoda_flutter/core/utils/storage_path.dart'; | |
| import 'package:hadi_hoda_flutter/features/download/domain/entities/download_entity.dart'; | |
| import 'package:hadi_hoda_flutter/features/level/data/model/level_model.dart'; | |
| import 'package:hadi_hoda_flutter/features/level/domain/entity/level_entity.dart'; | |
| import 'package:hadi_hoda_flutter/features/level/domain/entity/total_data_entity.dart'; | |
| import 'package:hive/hive.dart'; | |
| 
 | |
| abstract class IDownloadDatasource { | |
|   Future<void> getImages(); | |
|   Future<void> getAudios(); | |
|   Future<void> saveLevels(); | |
|   Stream<DownloadEntity> loadingStream(); | |
| } | |
| 
 | |
| class DownloadDatasourceImpl implements IDownloadDatasource { | |
|   final IHttpRequest httpRequest; | |
|   final StreamController<DownloadEntity> streamController = StreamController<DownloadEntity>.broadcast(); | |
| 
 | |
|   DownloadDatasourceImpl(this.httpRequest); | |
| 
 | |
|   @override | |
|   Future<void> getImages() async { | |
|     final String filePath = '${StoragePath.documentDir.path}/images.zip'; | |
| 
 | |
|     if (LocalStorage.readData(key: MyConstants.downloadedImage) != 'true') { | |
|       await httpRequest.download( | |
|         urlPath: MyApi.images, | |
|         savePath: filePath, | |
|         onReceive: (count, total) { | |
|           streamController.add(DownloadEntity( | |
|             count: count / 1, | |
|             total: total / 1, | |
|             percent: (count / total) * 100, | |
|           )); | |
|         }, | |
|       ).then((value) async { | |
|         await LocalStorage.saveData( | |
|           key: MyConstants.downloadedImage, | |
|           value: 'true', | |
|         ); | |
|       }); | |
|     } | |
| 
 | |
|     try{ | |
|       if (LocalStorage.readData(key: MyConstants.extractedImage) != 'true') { | |
|         final File file = File(filePath); | |
|         final Directory directory = Directory('${StoragePath.documentDir.path}/'); | |
|         await ZipFile.extractToDirectory( | |
|           zipFile: file, | |
|           destinationDir: directory, | |
|           onExtracting: (zipEntry, progress) { | |
|             return ZipFileOperation.includeItem; | |
|           }, | |
|         ).then((value) async { | |
|           await Future.wait([ | |
|             LocalStorage.saveData( | |
|               key: MyConstants.extractedImage, | |
|               value: 'true', | |
|             ), | |
|             file.delete(recursive: true), | |
|           ]); | |
|         }); | |
|       } | |
|     } catch (e){ | |
|       throw MyException(errorMessage: '$e'); | |
|     } | |
|   } | |
| 
 | |
|   @override | |
|   Future<void> getAudios() async { | |
|     final String filePath = '${StoragePath.documentDir.path}/audios.zip'; | |
|     final String selectedLanguage = | |
|         LocalStorage.readData(key: MyConstants.selectLanguage) ?? 'fa'; | |
| 
 | |
|     if(LocalStorage.readData(key: MyConstants.downloadedAudio) != 'true'){ | |
|       await httpRequest.download( | |
|         urlPath: MyApi.audios, | |
|         savePath: filePath, | |
|         queryParameters: { | |
|           'lang': selectedLanguage, | |
|         }, | |
|         onReceive: (count, total) { | |
|           streamController.add(DownloadEntity( | |
|             count: count / 1, | |
|             total: total / 1, | |
|             percent: (count / total) * 100, | |
|           )); | |
|         }, | |
|       ).then((value) async { | |
|         await LocalStorage.saveData( | |
|           key: MyConstants.downloadedAudio, | |
|           value: 'true', | |
|         ); | |
|       }); | |
|     } | |
| 
 | |
|     try{ | |
|       if (LocalStorage.readData(key: MyConstants.extractedAudio) != 'true') { | |
|         final File file = File(filePath); | |
|         final Directory directory = Directory( | |
|           '${StoragePath.documentDir.path}/$selectedLanguage/', | |
|         ); | |
|         await ZipFile.extractToDirectory( | |
|           zipFile: file, | |
|           destinationDir: directory, | |
|           onExtracting: (zipEntry, progress) { | |
|             return ZipFileOperation.includeItem; | |
|           }, | |
|         ).then((value) async { | |
|           await Future.wait([ | |
|             LocalStorage.saveData( | |
|               key: MyConstants.extractedAudio, | |
|               value: 'true', | |
|             ), | |
|             file.delete(recursive: true), | |
|           ]); | |
|         }); | |
|       } | |
|     } catch (e){ | |
|       throw MyException(errorMessage: '$e'); | |
|     } | |
|   } | |
| 
 | |
|   @override | |
|   Future<void> saveLevels() async { | |
|     final String selectedLanguage = | |
|         LocalStorage.readData(key: MyConstants.selectLanguage) ?? 'fa'; | |
|     final Box<TotalDataEntity> data = Hive.box(MyConstants.levelBox); | |
|     final TotalDataEntity findData = data.values.singleWhere( | |
|       (e) => e.code == selectedLanguage, | |
|       orElse: () => TotalDataEntity(), | |
|     ); | |
| 
 | |
|     if (findData.code != selectedLanguage) { | |
|       final response = await httpRequest.get( | |
|         path: MyApi.levels, | |
|         queryParameters: {'lang': selectedLanguage}, | |
|       ); | |
|       final List<LevelEntity> levels = BaseResponse.getDataList<LevelEntity>( | |
|         response?['result'], | |
|             (json) => LevelModel.fromJson(json), | |
|       ); | |
|       await data.add(TotalDataEntity(code: selectedLanguage, levels: levels)); | |
|     } | |
|   } | |
| 
 | |
|   @override | |
|   Stream<DownloadEntity> loadingStream() => streamController.stream; | |
| }
 |