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.
118 lines
4.1 KiB
118 lines
4.1 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/intro/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 IIntroDatasource {
|
|
Future<void> getFiles();
|
|
Future<void> saveLevels();
|
|
Stream<DownloadEntity> loadingStream();
|
|
}
|
|
|
|
class IntroDatasourceImpl implements IIntroDatasource {
|
|
final IHttpRequest httpRequest;
|
|
final StreamController<DownloadEntity> streamController = StreamController<DownloadEntity>.broadcast();
|
|
|
|
IntroDatasourceImpl(this.httpRequest);
|
|
|
|
@override
|
|
Future<void> getFiles() async {
|
|
final String filePath = '${StoragePath.documentDir.path}/files.zip';
|
|
final String selectedLanguage =
|
|
LocalStorage.readData(key: MyConstants.selectLanguage).isEmpty
|
|
? 'fa'
|
|
: LocalStorage.readData(key: MyConstants.selectLanguage);
|
|
|
|
if (LocalStorage.readData(key: MyConstants.downloadCompleted) != 'true') {
|
|
await httpRequest.download(
|
|
urlPath: MyApi.files,
|
|
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.downloadCompleted,
|
|
value: 'true',
|
|
);
|
|
});
|
|
}
|
|
|
|
try{
|
|
if (LocalStorage.readData(key: MyConstants.extractCompleted) != 'true') {
|
|
final File file = File(filePath);
|
|
final Directory directory = Directory('${StoragePath.documentDir.path}/$selectedLanguage/files/');
|
|
await ZipFile.extractToDirectory(
|
|
zipFile: file,
|
|
destinationDir: directory,
|
|
onExtracting: (zipEntry, progress) {
|
|
return ZipFileOperation.includeItem;
|
|
},
|
|
).then((value) async {
|
|
await Future.wait([
|
|
LocalStorage.saveData(
|
|
key: MyConstants.extractCompleted,
|
|
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
|
|
Future<void> saveLevels() async {
|
|
final String selectedLanguage =
|
|
LocalStorage.readData(key: MyConstants.selectLanguage).isEmpty
|
|
? 'fa'
|
|
: LocalStorage.readData(key: MyConstants.selectLanguage);
|
|
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 Future.wait([
|
|
data.add(TotalDataEntity(code: selectedLanguage, levels: levels)),
|
|
]);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Stream<DownloadEntity> loadingStream() => streamController.stream;
|
|
}
|