Skip to content

Commit

Permalink
fix: cleanup remember patches feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Mipirakas committed Jan 15, 2023
1 parent 27e3041 commit 1162310
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
20 changes: 5 additions & 15 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -424,30 +424,20 @@ class ManagerAPI {
} else {
patchesMap[app] = patches;
}
if (selectedPatchesFile.existsSync()) {
selectedPatchesFile.createSync(recursive: true);
}
selectedPatchesFile.writeAsString(jsonEncode(patchesMap));
}

Future<List<String>> getSelectedPatches(String app) async {
Map<String, dynamic> patchesMap = await readSelectedPatchesFile();
if (patchesMap.isNotEmpty) {
final List<String> patches =
List.from(patchesMap.putIfAbsent(app, () => List.empty()));
return patches;
}
return List.empty();
return List.from(patchesMap.putIfAbsent(app, () => List.empty()));
}

Future<Map<String, dynamic>> readSelectedPatchesFile() async {
final File selectedPatchesFile = File(storedPatchesFile);
if (selectedPatchesFile.existsSync()) {
String string = selectedPatchesFile.readAsStringSync();
if (string.trim().isEmpty) return {};
return json.decode(string);
}
return {};
if (!selectedPatchesFile.existsSync()) return {};
String string = selectedPatchesFile.readAsStringSync();
if (string.trim().isEmpty) return {};
return jsonDecode(string);
}

Future<void> resetLastSelectedPatches() async {
Expand Down
2 changes: 0 additions & 2 deletions lib/ui/views/app_selector/app_selector_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class AppSelectorViewModel extends BaseViewModel {
patchDate: DateTime.now(),
);
locator<PatcherViewModel>().loadLastSelectedPatches();
locator<PatcherViewModel>().notifyListeners();
}

Future<void> selectAppFromStorage(BuildContext context) async {
Expand Down Expand Up @@ -78,7 +77,6 @@ class AppSelectorViewModel extends BaseViewModel {
isFromStorage: true,
);
locator<PatcherViewModel>().loadLastSelectedPatches();
locator<PatcherViewModel>().notifyListeners();
}
}
} on Exception catch (e, s) {
Expand Down
3 changes: 1 addition & 2 deletions lib/ui/views/patcher/patcher_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ class PatcherViewModel extends BaseViewModel {
await _managerAPI.getSelectedPatches(selectedApp!.originalPackageName);
List<Patch> patches =
_patcherAPI.getFilteredPatches(selectedApp!.originalPackageName);
this
.selectedPatches
this.selectedPatches
.addAll(patches.where((patch) => selectedPatches.contains(patch.name)));
notifyListeners();
}
Expand Down
14 changes: 5 additions & 9 deletions lib/ui/views/settings/settings_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class SettingsViewModel extends BaseViewModel {
await CRFileSaver.saveFileWithDialog(SaveFileDialogParams(
sourceFilePath: tempFilePath, destinationFileName: ''));
File(tempFilePath).delete();
locator<Toast>().showBottom('settingsView.exportedPatches');
_toast.showBottom('settingsView.exportedPatches');
} else {
locator<Toast>().showBottom('settingsView.noExportFileFound');
_toast.showBottom('settingsView.noExportFileFound');
}
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s);
Expand All @@ -97,20 +97,16 @@ class SettingsViewModel extends BaseViewModel {
);
if (result != null && result.files.single.path != null) {
File inFile = File(result.files.single.path!);
final File storedPatchesFile = File(_managerAPI.storedPatchesFile);
if (!storedPatchesFile.existsSync()) {
storedPatchesFile.createSync(recursive: true);
}
inFile.copySync(storedPatchesFile.path);
inFile.copySync(_managerAPI.storedPatchesFile);
inFile.delete();
if (locator<PatcherViewModel>().selectedApp != null) {
locator<PatcherViewModel>().loadLastSelectedPatches();
}
locator<Toast>().showBottom('settingsView.importedPatches');
_toast.showBottom('settingsView.importedPatches');
}
} on Exception catch (e, s) {
await Sentry.captureException(e, stackTrace: s);
locator<Toast>().showBottom('settingsView.jsonSelectorErrorMessage');
_toast.showBottom('settingsView.jsonSelectorErrorMessage');
}
}

Expand Down

0 comments on commit 1162310

Please sign in to comment.