2022-09-15 02:15:13 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_i18n/flutter_i18n.dart';
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2022-09-05 16:30:26 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
|
|
|
import 'package:revanced_manager/app/app.router.dart';
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'package:revanced_manager/models/patch.dart';
|
|
|
|
import 'package:revanced_manager/models/patched_application.dart';
|
2022-09-23 16:31:24 +02:00
|
|
|
import 'package:revanced_manager/services/manager_api.dart';
|
2022-09-15 02:15:13 +02:00
|
|
|
import 'package:revanced_manager/services/patcher_api.dart';
|
2022-09-23 09:30:30 +02:00
|
|
|
import 'package:revanced_manager/ui/widgets/shared/custom_material_button.dart';
|
2022-08-06 14:13:28 +02:00
|
|
|
import 'package:stacked/stacked.dart';
|
2022-09-05 16:30:26 +02:00
|
|
|
import 'package:stacked_services/stacked_services.dart';
|
2022-08-06 14:13:28 +02:00
|
|
|
|
2022-08-18 16:33:33 +02:00
|
|
|
@lazySingleton
|
2022-08-06 14:13:28 +02:00
|
|
|
class PatcherViewModel extends BaseViewModel {
|
2022-09-05 16:30:26 +02:00
|
|
|
final NavigationService _navigationService = locator<NavigationService>();
|
2022-09-23 16:31:24 +02:00
|
|
|
final ManagerAPI _managerAPI = locator<ManagerAPI>();
|
2022-09-15 02:15:13 +02:00
|
|
|
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
|
2022-08-18 16:33:33 +02:00
|
|
|
PatchedApplication? selectedApp;
|
|
|
|
List<Patch> selectedPatches = [];
|
|
|
|
|
2022-09-05 16:30:26 +02:00
|
|
|
void navigateToAppSelector() {
|
|
|
|
_navigationService.navigateTo(Routes.appSelectorView);
|
|
|
|
}
|
|
|
|
|
|
|
|
void navigateToPatchesSelector() {
|
|
|
|
_navigationService.navigateTo(Routes.patchesSelectorView);
|
|
|
|
}
|
|
|
|
|
|
|
|
void navigateToInstaller() {
|
|
|
|
_navigationService.navigateTo(Routes.installerView);
|
|
|
|
}
|
|
|
|
|
2022-08-31 10:32:10 +02:00
|
|
|
bool showPatchButton() {
|
2022-08-18 16:33:33 +02:00
|
|
|
return selectedPatches.isNotEmpty;
|
2022-08-17 13:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool dimPatchesCard() {
|
2022-08-18 16:33:33 +02:00
|
|
|
return selectedApp == null;
|
2022-08-17 13:48:03 +02:00
|
|
|
}
|
2022-09-15 02:15:13 +02:00
|
|
|
|
|
|
|
Future<bool> isValidPatchConfig() async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool needsResourcePatching = await _patcherAPI.needsResourcePatching(
|
2022-09-23 16:31:24 +02:00
|
|
|
selectedPatches,
|
|
|
|
);
|
2022-09-15 02:15:13 +02:00
|
|
|
if (needsResourcePatching && selectedApp != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool isSplit = await _managerAPI.isSplitApk(selectedApp!);
|
2022-09-23 16:31:24 +02:00
|
|
|
return !isSplit;
|
2022-09-15 02:15:13 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> showPatchConfirmationDialog(BuildContext context) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool isValid = await isValidPatchConfig();
|
2023-02-19 04:16:49 +01:00
|
|
|
if (context.mounted) {
|
|
|
|
if (isValid) {
|
|
|
|
navigateToInstaller();
|
|
|
|
} else {
|
|
|
|
return showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
title: I18nText('warning'),
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
|
|
|
|
content: I18nText('patcherView.patchDialogText'),
|
|
|
|
actions: <Widget>[
|
|
|
|
CustomMaterialButton(
|
|
|
|
isFilled: false,
|
|
|
|
label: I18nText('noButton'),
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
),
|
|
|
|
CustomMaterialButton(
|
|
|
|
label: I18nText('yesButton'),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
navigateToInstaller();
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-09-15 02:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-18 04:14:48 +02:00
|
|
|
|
|
|
|
String getAppSelectionString() {
|
|
|
|
String text = '${selectedApp!.name} (${selectedApp!.packageName})';
|
|
|
|
if (text.length > 32) {
|
|
|
|
text = '${text.substring(0, 32)}...)';
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getRecommendedVersionString(BuildContext context) {
|
|
|
|
String recommendedVersion =
|
|
|
|
_patcherAPI.getRecommendedVersion(selectedApp!.packageName);
|
|
|
|
if (recommendedVersion.isEmpty) {
|
|
|
|
recommendedVersion = FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'appSelectorCard.anyVersion',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
recommendedVersion = 'v$recommendedVersion';
|
|
|
|
}
|
|
|
|
return '${FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'appSelectorCard.currentVersion',
|
|
|
|
)}: v${selectedApp!.version}\n${FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'appSelectorCard.recommendedVersion',
|
|
|
|
)}: $recommendedVersion';
|
|
|
|
}
|
2022-11-09 08:36:04 +01:00
|
|
|
|
|
|
|
Future<void> loadLastSelectedPatches() async {
|
|
|
|
this.selectedPatches.clear();
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<String> selectedPatches =
|
2022-11-09 08:36:04 +01:00
|
|
|
await _managerAPI.getSelectedPatches(selectedApp!.originalPackageName);
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<Patch> patches =
|
2022-12-15 19:05:45 +01:00
|
|
|
_patcherAPI.getFilteredPatches(selectedApp!.originalPackageName);
|
2023-02-19 04:16:49 +01:00
|
|
|
this
|
|
|
|
.selectedPatches
|
2022-11-09 08:36:04 +01:00
|
|
|
.addAll(patches.where((patch) => selectedPatches.contains(patch.name)));
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2022-08-06 14:13:28 +02:00
|
|
|
}
|