refactor: cleanup remember patches feature (#630)

This commit is contained in:
Mipirakas 2023-01-30 13:03:55 +01:00 committed by GitHub
parent 4df690c2a2
commit 1c965c3788
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 29 deletions

View File

@ -20,8 +20,8 @@ class ManagerAPI {
final RootAPI _rootAPI = RootAPI(); final RootAPI _rootAPI = RootAPI();
final String patcherRepo = 'revanced-patcher'; final String patcherRepo = 'revanced-patcher';
final String cliRepo = 'revanced-cli'; final String cliRepo = 'revanced-cli';
late String storedPatchesFile = '/selected-patches.json';
late SharedPreferences _prefs; late SharedPreferences _prefs;
String storedPatchesFile = '/selected-patches.json';
String defaultApiUrl = 'https://releases.revanced.app/'; String defaultApiUrl = 'https://releases.revanced.app/';
String defaultRepoUrl = 'https://api.github.com'; String defaultRepoUrl = 'https://api.github.com';
String defaultPatcherRepo = 'revanced/revanced-patcher'; String defaultPatcherRepo = 'revanced/revanced-patcher';
@ -424,30 +424,20 @@ class ManagerAPI {
} else { } else {
patchesMap[app] = patches; patchesMap[app] = patches;
} }
if (selectedPatchesFile.existsSync()) {
selectedPatchesFile.createSync(recursive: true);
}
selectedPatchesFile.writeAsString(jsonEncode(patchesMap)); selectedPatchesFile.writeAsString(jsonEncode(patchesMap));
} }
Future<List<String>> getSelectedPatches(String app) async { Future<List<String>> getSelectedPatches(String app) async {
Map<String, dynamic> patchesMap = await readSelectedPatchesFile(); Map<String, dynamic> patchesMap = await readSelectedPatchesFile();
if (patchesMap.isNotEmpty) { return List.from(patchesMap.putIfAbsent(app, () => List.empty()));
final List<String> patches =
List.from(patchesMap.putIfAbsent(app, () => List.empty()));
return patches;
}
return List.empty();
} }
Future<Map<String, dynamic>> readSelectedPatchesFile() async { Future<Map<String, dynamic>> readSelectedPatchesFile() async {
final File selectedPatchesFile = File(storedPatchesFile); final File selectedPatchesFile = File(storedPatchesFile);
if (selectedPatchesFile.existsSync()) { if (!selectedPatchesFile.existsSync()) return {};
String string = selectedPatchesFile.readAsStringSync(); String string = selectedPatchesFile.readAsStringSync();
if (string.trim().isEmpty) return {}; if (string.trim().isEmpty) return {};
return json.decode(string); return jsonDecode(string);
}
return {};
} }
Future<void> resetLastSelectedPatches() async { Future<void> resetLastSelectedPatches() async {

View File

@ -42,7 +42,6 @@ class AppSelectorViewModel extends BaseViewModel {
patchDate: DateTime.now(), patchDate: DateTime.now(),
); );
locator<PatcherViewModel>().loadLastSelectedPatches(); locator<PatcherViewModel>().loadLastSelectedPatches();
locator<PatcherViewModel>().notifyListeners();
} }
Future<void> selectAppFromStorage(BuildContext context) async { Future<void> selectAppFromStorage(BuildContext context) async {
@ -78,7 +77,6 @@ class AppSelectorViewModel extends BaseViewModel {
isFromStorage: true, isFromStorage: true,
); );
locator<PatcherViewModel>().loadLastSelectedPatches(); locator<PatcherViewModel>().loadLastSelectedPatches();
locator<PatcherViewModel>().notifyListeners();
} }
} }
} on Exception catch (e, s) { } on Exception catch (e, s) {

View File

@ -114,8 +114,7 @@ class PatcherViewModel extends BaseViewModel {
await _managerAPI.getSelectedPatches(selectedApp!.originalPackageName); await _managerAPI.getSelectedPatches(selectedApp!.originalPackageName);
List<Patch> patches = List<Patch> patches =
_patcherAPI.getFilteredPatches(selectedApp!.originalPackageName); _patcherAPI.getFilteredPatches(selectedApp!.originalPackageName);
this this.selectedPatches
.selectedPatches
.addAll(patches.where((patch) => selectedPatches.contains(patch.name))); .addAll(patches.where((patch) => selectedPatches.contains(patch.name)));
notifyListeners(); notifyListeners();
} }

View File

@ -80,9 +80,9 @@ class SettingsViewModel extends BaseViewModel {
await CRFileSaver.saveFileWithDialog(SaveFileDialogParams( await CRFileSaver.saveFileWithDialog(SaveFileDialogParams(
sourceFilePath: tempFilePath, destinationFileName: '')); sourceFilePath: tempFilePath, destinationFileName: ''));
File(tempFilePath).delete(); File(tempFilePath).delete();
locator<Toast>().showBottom('settingsView.exportedPatches'); _toast.showBottom('settingsView.exportedPatches');
} else { } else {
locator<Toast>().showBottom('settingsView.noExportFileFound'); _toast.showBottom('settingsView.noExportFileFound');
} }
} on Exception catch (e, s) { } on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s); Sentry.captureException(e, stackTrace: s);
@ -97,20 +97,16 @@ class SettingsViewModel extends BaseViewModel {
); );
if (result != null && result.files.single.path != null) { if (result != null && result.files.single.path != null) {
File inFile = File(result.files.single.path!); File inFile = File(result.files.single.path!);
final File storedPatchesFile = File(_managerAPI.storedPatchesFile); inFile.copySync(_managerAPI.storedPatchesFile);
if (!storedPatchesFile.existsSync()) {
storedPatchesFile.createSync(recursive: true);
}
inFile.copySync(storedPatchesFile.path);
inFile.delete(); inFile.delete();
if (locator<PatcherViewModel>().selectedApp != null) { if (locator<PatcherViewModel>().selectedApp != null) {
locator<PatcherViewModel>().loadLastSelectedPatches(); locator<PatcherViewModel>().loadLastSelectedPatches();
} }
locator<Toast>().showBottom('settingsView.importedPatches'); _toast.showBottom('settingsView.importedPatches');
} }
} on Exception catch (e, s) { } on Exception catch (e, s) {
await Sentry.captureException(e, stackTrace: s); await Sentry.captureException(e, stackTrace: s);
locator<Toast>().showBottom('settingsView.jsonSelectorErrorMessage'); _toast.showBottom('settingsView.jsonSelectorErrorMessage');
} }
} }