revanced-manager/lib/ui/widgets/appInfoView/app_info_viewmodel.dart

173 lines
5.4 KiB
Dart
Raw Normal View History

// ignore_for_file: use_build_context_synchronously
2022-09-05 14:43:13 +02:00
import 'package:device_apps/device_apps.dart';
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:intl/intl.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patched_application.dart';
import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/services/root_api.dart';
import 'package:revanced_manager/services/toast.dart';
import 'package:revanced_manager/ui/views/home/home_viewmodel.dart';
2022-09-05 14:43:13 +02:00
import 'package:revanced_manager/ui/views/navigation/navigation_viewmodel.dart';
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
import 'package:stacked/stacked.dart';
class AppInfoViewModel extends BaseViewModel {
final ManagerAPI _managerAPI = locator<ManagerAPI>();
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
final RootAPI _rootAPI = RootAPI();
final Toast _toast = locator<Toast>();
2022-09-05 14:43:13 +02:00
Future<void> uninstallApp(
BuildContext context,
PatchedApplication app,
bool onlyUnpatch,
) async {
2023-12-23 20:53:51 +01:00
var isUninstalled = onlyUnpatch;
if (!onlyUnpatch) {
2023-12-23 21:11:24 +01:00
// TODO(Someone): Wait for the app to uninstall successfully.
isUninstalled = await DeviceApps.uninstallApp(app.packageName);
}
2023-12-23 20:53:51 +01:00
if (isUninstalled && app.isRooted && await _rootAPI.hasRootPermissions()) {
await _rootAPI.uninstall(app.packageName);
}
if (isUninstalled) {
await _managerAPI.deletePatchedApp(app);
locator<HomeViewModel>().initialize(context);
2022-09-05 14:43:13 +02:00
}
}
Future<void> navigateToPatcher(PatchedApplication app) async {
2022-09-05 14:43:13 +02:00
locator<PatcherViewModel>().selectedApp = app;
locator<PatcherViewModel>().selectedPatches =
await _patcherAPI.getAppliedPatches(app.appliedPatches);
locator<PatcherViewModel>().notifyListeners();
locator<NavigationViewModel>().setIndex(1);
}
void updateNotImplemented(BuildContext context) {
_toast.showBottom('appInfoView.updateNotImplemented');
}
Future<void> showUninstallDialog(
2022-09-05 14:43:13 +02:00
BuildContext context,
PatchedApplication app,
bool onlyUnpatch,
2022-09-05 14:43:13 +02:00
) async {
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
if (app.isRooted && !hasRootPermissions) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText('appInfoView.rootDialogTitle'),
content: I18nText('appInfoView.rootDialogText'),
actions: <Widget>[
FilledButton(
onPressed: () => Navigator.of(context).pop(),
child: I18nText('okButton'),
),
],
),
);
2022-09-05 14:43:13 +02:00
} else {
if (onlyUnpatch) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText(
'appInfoView.unpatchButton',
2022-09-05 14:43:13 +02:00
),
content: I18nText(
'appInfoView.unpatchDialogText',
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: I18nText('noButton'),
),
FilledButton(
onPressed: () {
2023-12-23 21:11:24 +01:00
uninstallApp(context, app, true);
Navigator.of(context).pop();
Navigator.of(context).pop();
},
child: I18nText('yesButton'),
),
],
),
);
} else {
2023-12-23 21:11:24 +01:00
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText(
'appInfoView.uninstallButton',
),
content: I18nText(
'appInfoView.uninstallDialogText',
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: I18nText('noButton'),
),
FilledButton(
onPressed: () {
uninstallApp(context, app, false);
Navigator.of(context).pop();
Navigator.of(context).pop();
},
child: I18nText('yesButton'),
),
],
),
);
}
2022-09-05 14:43:13 +02:00
}
}
String getPrettyDate(BuildContext context, DateTime dateTime) {
return DateFormat.yMMMMd(Localizations.localeOf(context).languageCode)
.format(dateTime);
}
String getPrettyTime(BuildContext context, DateTime dateTime) {
return DateFormat.jm(Localizations.localeOf(context).languageCode)
.format(dateTime);
}
Future<void> showAppliedPatchesDialog(
BuildContext context,
PatchedApplication app,
) async {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText('appInfoView.appliedPatchesLabel'),
content: SingleChildScrollView(
child: Text(getAppliedPatchesString(app.appliedPatches)),
),
actions: <Widget>[
FilledButton(
2022-09-05 14:43:13 +02:00
onPressed: () => Navigator.of(context).pop(),
child: I18nText('okButton'),
),
2022-09-05 14:43:13 +02:00
],
),
);
}
String getAppliedPatchesString(List<String> appliedPatches) {
2023-10-12 02:00:39 +02:00
return '${appliedPatches.join('\n')}';
2022-09-05 14:43:13 +02:00
}
void openApp(PatchedApplication app) {
DeviceApps.openApp(app.packageName);
}
2022-09-05 14:43:13 +02:00
}