feat: ability to store and load selected patches (#469)

* feat: ability to store and load selected patches

* fix: I18n

* fix: do not append but truncate file

* fix: use json file, minor fixes

* fix: better ui

* WIP

* feat: load patches selection after app selection

* feat: import/export json file

* fix: reformat code

* fix: rare bug on import feature fixed

* fix: move export/ipmort to settings page & import full json

* fix: minor improvements

* fix: minor code quality improvements

* fix: export filename fix

* fix: select list element istead of removing it
This commit is contained in:
Boris M 2022-11-09 08:36:04 +01:00 committed by GitHub
parent fd5d71e24d
commit c571cf2c53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 224 additions and 3 deletions

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:device_apps/device_apps.dart';
import 'package:injectable/injectable.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/models/patched_application.dart';
@ -19,6 +20,7 @@ class ManagerAPI {
final RootAPI _rootAPI = RootAPI();
final String patcherRepo = 'revanced-patcher';
final String cliRepo = 'revanced-cli';
late String storedPatchesFile = '/selected-patches.json';
late SharedPreferences _prefs;
String defaultApiUrl = 'https://releases.revanced.app/';
String defaultPatcherRepo = 'revanced/revanced-patcher';
@ -29,6 +31,8 @@ class ManagerAPI {
Future<void> initialize() async {
_prefs = await SharedPreferences.getInstance();
storedPatchesFile =
(await getApplicationDocumentsDirectory()).path + storedPatchesFile;
}
String getApiUrl() {
@ -391,4 +395,43 @@ class ManagerAPI {
}
return app != null && app.isSplit;
}
Future<void> setSelectedPatches(String app, List<String> patches) async {
final File selectedPatchesFile = File(storedPatchesFile);
Map<String, dynamic> patchesMap = await readSelectedPatchesFile();
if (patches.isEmpty) {
patchesMap.remove(app);
} 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();
}
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 {};
}
Future<void> resetLastSelectedPatches() async {
final File selectedPatchesFile = File(storedPatchesFile);
selectedPatchesFile.deleteSync();
}
}