Skip to content

Commit

Permalink
feat(patches-selector): show New tag for new patches (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAabedKhan authored Aug 7, 2023
1 parent 131df28 commit 1714c3f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 17 deletions.
6 changes: 5 additions & 1 deletion assets/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"yesButton": "Yes",
"noButton": "No",
"warning": "Warning",
"new": "New",
"navigationView": {
"dashboardTab": "Dashboard",
"patcherTab": "Patcher",
Expand Down Expand Up @@ -130,7 +131,10 @@
},
"patchItem": {
"unsupportedDialogText": "Selecting this patch may result in patching errors.\n\nApp version: {packageVersion}\nSupported versions:\n{supportedVersions}",
"unsupportedPatchVersion": "Patch is not supported for this app version. Enable the experimental toggle in settings to proceed."
"unsupportedPatchVersion": "Patch is not supported for this app version. Enable the experimental toggle in settings to proceed.",

"newPatchDialogText": "This is a new patch that has been added since the last time you have patched this app.",
"newPatch": "New patch"
},
"installerView": {
"widgetTitle": "Installer",
Expand Down
15 changes: 15 additions & 0 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ class ManagerAPI {
await _prefs.setBool('patchesAutoUpdate', value);
}

List<Patch> getSavedPatches(String packageName) {
final List<String> patchesJson = _prefs.getStringList('savedPatches-$packageName') ?? [];
final List<Patch> patches = patchesJson.map((String patchJson) {
return Patch.fromJson(jsonDecode(patchJson));
}).toList();
return patches;
}

Future<void> savePatches(List<Patch> patches, String packageName) async {
final List<String> patchesJson = patches.map((Patch patch) {
return jsonEncode(patch.toJson());
}).toList();
await _prefs.setStringList('savedPatches-$packageName', patchesJson);
}

String getIntegrationsRepo() {
return _prefs.getString('integrationsRepo') ?? defaultIntegrationsRepo;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ui/views/installer/installer_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class InstallerViewModel extends BaseViewModel {
});
}

void update(double value, String header, String log) {
Future<void> update(double value, String header, String log) async {
if (value >= 0.0) {
progress = value;
}
Expand All @@ -97,6 +97,10 @@ class InstallerViewModel extends BaseViewModel {
} else if (value == 1.0) {
isPatching = false;
hasErrors = false;
await _managerAPI.savePatches(
_patcherAPI.getFilteredPatches(_app.packageName),
_app.packageName,
);
} else if (value == -100.0) {
isPatching = false;
hasErrors = true;
Expand Down
10 changes: 8 additions & 2 deletions lib/ui/views/patches_selector/patches_selector_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,14 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
name: patch.name,
simpleName: patch.getSimpleName(),
description: patch.description,
packageVersion: model.getAppVersion(),
packageVersion: model.getAppInfo().version,
supportedPackageVersions:
model.getSupportedVersions(patch),
isUnsupported: !isPatchSupported(patch),
isNew: model.isPatchNew(
patch,
model.getAppInfo().packageName,
),
isSelected: model.isSelected(patch),
onChanged: (value) =>
model.selectPatch(patch, value),
Expand Down Expand Up @@ -206,10 +210,12 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
name: patch.name,
simpleName: patch.getSimpleName(),
description: patch.description,
packageVersion: model.getAppVersion(),
packageVersion:
model.getAppInfo().version,
supportedPackageVersions:
model.getSupportedVersions(patch),
isUnsupported: !isPatchSupported(patch),
isNew: false,
isSelected: model.isSelected(patch),
onChanged: (value) =>
model.selectPatch(patch, value),
Expand Down
26 changes: 22 additions & 4 deletions lib/ui/views/patches_selector/patches_selector_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PatchesSelectorViewModel extends BaseViewModel {
final List<Patch> patches = [];
final List<Patch> selectedPatches =
locator<PatcherViewModel>().selectedPatches;
PatchedApplication? selectedApp = locator<PatcherViewModel>().selectedApp;
String? patchesVersion = '';
bool isDefaultPatchesRepo() {
return _managerAPI.getPatchesRepo() == 'revanced/revanced-patches';
Expand All @@ -24,10 +25,17 @@ class PatchesSelectorViewModel extends BaseViewModel {
getPatchesVersion().whenComplete(() => notifyListeners());
patches.addAll(
_patcherAPI.getFilteredPatches(
locator<PatcherViewModel>().selectedApp!.originalPackageName,
selectedApp!.originalPackageName,
),
);
patches.sort((a, b) => a.name.compareTo(b.name));
patches.sort((a, b) {
if (isPatchNew(a, selectedApp!.packageName) ==
isPatchNew(b, selectedApp!.packageName)) {
return a.name.compareTo(b.name);
} else {
return isPatchNew(b, selectedApp!.packageName) ? 1 : -1;
}
});
notifyListeners();
}

Expand Down Expand Up @@ -99,8 +107,18 @@ class PatchesSelectorViewModel extends BaseViewModel {
}
}

String getAppVersion() {
return locator<PatcherViewModel>().selectedApp!.version;
PatchedApplication getAppInfo() {
return locator<PatcherViewModel>().selectedApp!;
}

bool isPatchNew(Patch patch, String packageName) {
final List<Patch> savedPatches = _managerAPI.getSavedPatches(packageName);
if (savedPatches.isEmpty) {
return false;
} else {
return !savedPatches
.any((p) => p.name == patch.name.toLowerCase().replaceAll(' ', '-'));
}
}

List<String> getSupportedVersions(Patch patch) {
Expand Down
62 changes: 53 additions & 9 deletions lib/ui/widgets/patchesSelectorView/patch_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PatchItem extends StatefulWidget {
required this.packageVersion,
required this.supportedPackageVersions,
required this.isUnsupported,
required this.isNew,
required this.isSelected,
required this.onChanged,
this.child,
Expand All @@ -26,6 +27,7 @@ class PatchItem extends StatefulWidget {
final String packageVersion;
final List<String> supportedPackageVersions;
final bool isUnsupported;
final bool isNew;
bool isSelected;
final Function(bool) onChanged;
final Widget? child;
Expand Down Expand Up @@ -132,12 +134,12 @@ class _PatchItemState extends State<PatchItem> {
),
],
),
if (widget.isUnsupported &&
widget._managerAPI.areExperimentalPatchesEnabled())
Row(
children: <Widget>[
Row(
children: [
if (widget.isUnsupported &&
widget._managerAPI.areExperimentalPatchesEnabled())
Padding(
padding: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.only(top: 8, right: 8),
child: TextButton.icon(
label: I18nText('warning'),
icon: const Icon(Icons.warning, size: 20.0),
Expand All @@ -160,10 +162,33 @@ class _PatchItemState extends State<PatchItem> {
),
),
),
],
)
else
Container(),
if (widget.isNew)
Padding(
padding: const EdgeInsets.only(top: 8),
child: TextButton.icon(
label: I18nText('new'),
icon: const Icon(Icons.star, size: 20.0),
onPressed: () => _showNewPatchDialog(),
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: BorderSide(
color: Theme.of(context).colorScheme.secondary,
),
),
),
backgroundColor: MaterialStateProperty.all(
Colors.transparent,
),
foregroundColor: MaterialStateProperty.all(
Theme.of(context).colorScheme.secondary,
),
),
),
)
],
),
widget.child ?? const SizedBox(),
],
),
Expand Down Expand Up @@ -195,4 +220,23 @@ class _PatchItemState extends State<PatchItem> {
),
);
}

Future<void> _showNewPatchDialog() {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText('patchItem.newPatch'),
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
content: I18nText(
'patchItem.newPatchDialogText',
),
actions: <Widget>[
CustomMaterialButton(
label: I18nText('okButton'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
}
}

0 comments on commit 1714c3f

Please sign in to comment.