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.
 
 
 
 

78 lines
2.6 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/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';
abstract class IIntroDatasource {
Future<void> getImages();
Stream<DownloadEntity> loadingStream();
}
class IntroDatasourceImpl implements IIntroDatasource {
final IHttpRequest httpRequest;
final StreamController<DownloadEntity> streamController = StreamController<DownloadEntity>.broadcast();
IntroDatasourceImpl(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),
]);
});
} else {
streamController.add(DownloadEntity(percent: 50));
await Future.delayed(Duration(milliseconds: 150));
streamController.add(DownloadEntity(percent: 100));
}
} catch (e){
throw MyException(errorMessage: '$e');
}
}
@override
Stream<DownloadEntity> loadingStream() => streamController.stream;
}