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.
74 lines
2.3 KiB
74 lines
2.3 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';
|
|
|
|
abstract class IIntroDatasource {
|
|
Future<void> getFiles();
|
|
Stream<double> loadingStream();
|
|
}
|
|
|
|
class IntroDatasourceImpl implements IIntroDatasource {
|
|
final IHttpRequest httpRequest;
|
|
final StreamController<double> streamController = StreamController<double>.broadcast();
|
|
|
|
IntroDatasourceImpl(this.httpRequest);
|
|
|
|
@override
|
|
Future<void> getFiles() async {
|
|
final String filePath = '${StoragePath.documentDir.path}/files.zip';
|
|
|
|
if (LocalStorage.readData(key: MyConstants.downloadCompleted) != 'true') {
|
|
await httpRequest.download(
|
|
urlPath: MyApi.files,
|
|
savePath: filePath,
|
|
onReceive: (count, total) {
|
|
double percent = ((count / total) * 100);
|
|
streamController.add(percent);
|
|
},
|
|
).then((value) async {
|
|
await LocalStorage.saveData(
|
|
key: MyConstants.downloadCompleted,
|
|
value: 'true',
|
|
);
|
|
});
|
|
}
|
|
|
|
try{
|
|
if (LocalStorage.readData(key: MyConstants.extractCompleted) != 'true') {
|
|
final File file = File(filePath);
|
|
await ZipFile.extractToDirectory(
|
|
zipFile: file,
|
|
destinationDir: StoragePath.documentDir,
|
|
onExtracting: (zipEntry, progress) {
|
|
streamController.add(progress);
|
|
return ZipFileOperation.includeItem;
|
|
},
|
|
).then((value) async {
|
|
await Future.wait([
|
|
LocalStorage.saveData(
|
|
key: MyConstants.extractCompleted,
|
|
value: 'true',
|
|
),
|
|
file.delete(recursive: true),
|
|
]);
|
|
});
|
|
} else {
|
|
streamController.add(50);
|
|
await Future.delayed(Duration(milliseconds: 150));
|
|
streamController.add(100);
|
|
}
|
|
} catch (e){
|
|
throw MyException(errorMessage: '$e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Stream<double> loadingStream() => streamController.stream;
|
|
}
|