Skip to content

Commit 3f577fa

Browse files
committed
fix installed apps
1 parent 7f01ab2 commit 3f577fa

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

lib/data/repositories/app/android_app_repository.dart

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class AndroidAppRepository implements AppRepository {
1717

1818
@override
1919
AsyncResult<AppEntity> addInfo(AppEntity app) async {
20+
if (app.appNotInstall) {
21+
return Failure(AndroidPluginException('App not installed: ${app.repository.projectName}'));
22+
}
23+
2024
final package = await _androidPackage.getInfoById(app.packageInfo.id);
2125
if (package == null) {
2226
return Failure(AndroidPluginException('Package not found: ${app.packageInfo.id}'));
@@ -101,22 +105,30 @@ class AndroidAppRepository implements AppRepository {
101105
.map(_jsonDecode)
102106
.map(_listToApps)
103107
.recover(_recoverEmptyList)
104-
.map(
105-
(apps) => apps.map((a) => a.toNotInstalled()).toList(),
106-
);
108+
.flatMap(_addInfos);
107109
}
108110

109111
@override
110112
AsyncResult<AppEntity> putApp(AppEntity app) {
111113
return fetchApps() //
112-
.flatMap((apps) {
113-
if (apps.indexWhere((element) => element.repository == app.repository) != -1) {
114-
return const Failure<String, Exception>(RemoteRepositoryException('App already exists'));
114+
.map((apps) {
115+
final index = apps.indexWhere((a) => a.repository == app.repository);
116+
if (index != -1) {
117+
final newApps = [
118+
...apps.sublist(0, index),
119+
app.copyWith.packageInfo(imageBytes: []),
120+
...apps.sublist(index + 1),
121+
].map((a) => a.toJson()).toList();
122+
return newApps;
115123
}
116124

117-
final newApps = [...apps, app.toNotInstalled()].map((a) => a.toJson()).toList();
118-
return Success(jsonEncode(newApps));
125+
final newApps = [
126+
...apps,
127+
app.copyWith.packageInfo(imageBytes: []),
128+
].map((a) => a.toJson()).toList();
129+
return newApps;
119130
})
131+
.map(jsonEncode)
120132
.flatMap((json) => _localStorage.saveData(_localAndroidAppKey, json))
121133
.pure(app);
122134
}
@@ -143,7 +155,7 @@ class AndroidAppRepository implements AppRepository {
143155
}
144156

145157
List<AppEntity> _listToApps(List<Map<String, Object?>> list) {
146-
return list.map(NotInstalledAppEntity.fromJson).toList();
158+
return list.map(AppEntity.fromJson).toList();
147159
}
148160

149161
List<Map<String, Object?>> _jsonDecode(String json) {
@@ -156,4 +168,15 @@ class AndroidAppRepository implements AppRepository {
156168
return [];
157169
}
158170
}
171+
172+
AsyncResult<List<AppEntity>> _addInfos(List<AppEntity> apps) async {
173+
final List<AppEntity> newApps = [];
174+
175+
for (final app in apps) {
176+
final result = await addInfo(app);
177+
newApps.add(result.getOrDefault(app));
178+
}
179+
180+
return Success(newApps);
181+
}
159182
}

lib/domain/usecases/add_this_app_information.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@ class AddThisAppInformation {
1010
AsyncResult<Unit> call() async {
1111
final getapps = AppEntity.thisAppEntity();
1212

13-
return _appRepository //
14-
.putApp(getapps)
13+
return _checkExistApp(getapps) //
1514
.flatMap(_appRepository.addInfo)
1615
.flatMap(_codeHostingRepository.getLastRelease)
1716
.flatMap(_appRepository.putApp)
1817
.pure(unit);
1918
}
19+
20+
AsyncResult<AppEntity> _checkExistApp(AppEntity app) async {
21+
return _appRepository //
22+
.fetchApps()
23+
.flatMap((apps) {
24+
if (apps.contains(app)) {
25+
return Success(app);
26+
} else {
27+
return Failure(Exception('App not found'));
28+
}
29+
});
30+
}
2031
}

lib/domain/usecases/install_app_usecase.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Future<void> _installAppIsolateAction(
100100
.map((app) => app.toLoading())
101101
.onSuccess(installReceivePort.send)
102102
.flatMap(appRepository.installApp)
103-
.map<AppEntity>((app) => app.toInstalled())
103+
.flatMap(appRepository.putApp)
104104
.pureError<AppEntity>(currentState)
105105
.merge();
106106

0 commit comments

Comments
 (0)