Skip to content

Commit

Permalink
fix installed apps
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Jan 20, 2025
1 parent 7f01ab2 commit 3f577fa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
41 changes: 32 additions & 9 deletions lib/data/repositories/app/android_app_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class AndroidAppRepository implements AppRepository {

@override
AsyncResult<AppEntity> addInfo(AppEntity app) async {
if (app.appNotInstall) {
return Failure(AndroidPluginException('App not installed: ${app.repository.projectName}'));
}

final package = await _androidPackage.getInfoById(app.packageInfo.id);
if (package == null) {
return Failure(AndroidPluginException('Package not found: ${app.packageInfo.id}'));
Expand Down Expand Up @@ -101,22 +105,30 @@ class AndroidAppRepository implements AppRepository {
.map(_jsonDecode)
.map(_listToApps)
.recover(_recoverEmptyList)
.map(
(apps) => apps.map((a) => a.toNotInstalled()).toList(),
);
.flatMap(_addInfos);
}

@override
AsyncResult<AppEntity> putApp(AppEntity app) {
return fetchApps() //
.flatMap((apps) {
if (apps.indexWhere((element) => element.repository == app.repository) != -1) {
return const Failure<String, Exception>(RemoteRepositoryException('App already exists'));
.map((apps) {
final index = apps.indexWhere((a) => a.repository == app.repository);
if (index != -1) {
final newApps = [
...apps.sublist(0, index),
app.copyWith.packageInfo(imageBytes: []),
...apps.sublist(index + 1),
].map((a) => a.toJson()).toList();
return newApps;
}

final newApps = [...apps, app.toNotInstalled()].map((a) => a.toJson()).toList();
return Success(jsonEncode(newApps));
final newApps = [
...apps,
app.copyWith.packageInfo(imageBytes: []),
].map((a) => a.toJson()).toList();
return newApps;
})
.map(jsonEncode)
.flatMap((json) => _localStorage.saveData(_localAndroidAppKey, json))
.pure(app);
}
Expand All @@ -143,7 +155,7 @@ class AndroidAppRepository implements AppRepository {
}

List<AppEntity> _listToApps(List<Map<String, Object?>> list) {
return list.map(NotInstalledAppEntity.fromJson).toList();
return list.map(AppEntity.fromJson).toList();
}

List<Map<String, Object?>> _jsonDecode(String json) {
Expand All @@ -156,4 +168,15 @@ class AndroidAppRepository implements AppRepository {
return [];
}
}

AsyncResult<List<AppEntity>> _addInfos(List<AppEntity> apps) async {
final List<AppEntity> newApps = [];

for (final app in apps) {
final result = await addInfo(app);
newApps.add(result.getOrDefault(app));
}

return Success(newApps);
}
}
15 changes: 13 additions & 2 deletions lib/domain/usecases/add_this_app_information.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ class AddThisAppInformation {
AsyncResult<Unit> call() async {
final getapps = AppEntity.thisAppEntity();

return _appRepository //
.putApp(getapps)
return _checkExistApp(getapps) //
.flatMap(_appRepository.addInfo)
.flatMap(_codeHostingRepository.getLastRelease)
.flatMap(_appRepository.putApp)
.pure(unit);
}

AsyncResult<AppEntity> _checkExistApp(AppEntity app) async {
return _appRepository //
.fetchApps()
.flatMap((apps) {
if (apps.contains(app)) {
return Success(app);
} else {
return Failure(Exception('App not found'));
}
});
}
}
2 changes: 1 addition & 1 deletion lib/domain/usecases/install_app_usecase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Future<void> _installAppIsolateAction(
.map((app) => app.toLoading())
.onSuccess(installReceivePort.send)
.flatMap(appRepository.installApp)
.map<AppEntity>((app) => app.toInstalled())
.flatMap(appRepository.putApp)
.pureError<AppEntity>(currentState)
.merge();

Expand Down

0 comments on commit 3f577fa

Please sign in to comment.