revanced-manager/lib/ui/views/home/home_viewmodel.dart

201 lines
6.5 KiB
Dart
Raw Normal View History

// ignore_for_file: use_build_context_synchronously
2022-08-18 16:33:33 +02:00
import 'dart:io';
import 'package:app_installer/app_installer.dart';
import 'package:cross_connectivity/cross_connectivity.dart';
2022-08-18 16:33:33 +02:00
import 'package:device_apps/device_apps.dart';
import 'package:flutter/material.dart';
2022-08-18 16:33:33 +02:00
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:injectable/injectable.dart';
2022-08-06 23:35:35 +02:00
import 'package:revanced_manager/app/app.locator.dart';
2022-09-05 16:30:26 +02:00
import 'package:revanced_manager/app/app.router.dart';
import 'package:revanced_manager/models/patched_application.dart';
2022-08-06 23:35:35 +02:00
import 'package:revanced_manager/services/manager_api.dart';
2022-08-17 19:44:27 +02:00
import 'package:revanced_manager/services/patcher_api.dart';
2022-09-05 10:07:38 +02:00
import 'package:revanced_manager/ui/views/navigation/navigation_viewmodel.dart';
2022-08-18 16:33:33 +02:00
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/installerView/custom_material_button.dart';
import 'package:stacked/stacked.dart';
2022-09-05 16:30:26 +02:00
import 'package:stacked_services/stacked_services.dart';
@lazySingleton
class HomeViewModel extends BaseViewModel {
2022-09-05 16:30:26 +02:00
final NavigationService _navigationService = locator<NavigationService>();
2022-08-25 01:51:47 +02:00
final ManagerAPI _managerAPI = locator<ManagerAPI>();
2022-08-18 16:33:33 +02:00
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
DateTime? _lastUpdate;
bool showUpdatableApps = true;
List<PatchedApplication> patchedInstalledApps = [];
List<PatchedApplication> patchedUpdatableApps = [];
Future<void> initialize(BuildContext context) async {
2022-08-18 16:33:33 +02:00
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('ic_notification'),
),
2022-09-12 14:53:34 +02:00
onSelectNotification: (p) =>
DeviceApps.openApp('app.revanced.manager.flutter'),
2022-08-18 16:33:33 +02:00
);
bool isConnected = await Connectivity().checkConnection();
if (!isConnected) {
Fluttertoast.showToast(
msg: FlutterI18n.translate(
context,
'homeView.noConnection',
),
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
);
}
_getPatchedApps();
_managerAPI.reAssessSavedApps().then((_) => _getPatchedApps());
2022-08-17 19:44:27 +02:00
}
2022-09-05 16:30:26 +02:00
void navigateToAppInfo(PatchedApplication app) {
_navigationService.navigateTo(
Routes.appInfoView,
arguments: AppInfoViewArguments(app: app),
);
}
void toggleUpdatableApps(bool value) {
showUpdatableApps = value;
notifyListeners();
}
void navigateToPatcher(PatchedApplication app) async {
2022-08-18 16:33:33 +02:00
locator<PatcherViewModel>().selectedApp = app;
locator<PatcherViewModel>().selectedPatches =
await _patcherAPI.getAppliedPatches(app.appliedPatches);
locator<PatcherViewModel>().notifyListeners();
2022-09-05 10:07:38 +02:00
locator<NavigationViewModel>().setIndex(1);
2022-08-17 19:44:27 +02:00
}
void _getPatchedApps() {
patchedInstalledApps = _managerAPI.getPatchedApps().toList();
patchedUpdatableApps = _managerAPI
.getPatchedApps()
.where((app) => app.hasUpdates == true)
.toList();
notifyListeners();
}
2022-08-18 16:33:33 +02:00
2022-08-18 18:32:58 +02:00
Future<bool> hasManagerUpdates() async {
String? latestVersion = await _managerAPI.getLatestManagerVersion();
String currentVersion = await _managerAPI.getCurrentManagerVersion();
if (latestVersion != null) {
try {
int latestVersionInt =
int.parse(latestVersion.replaceAll(RegExp('[^0-9]'), ''));
2022-08-18 18:32:58 +02:00
int currentVersionInt =
int.parse(currentVersion.replaceAll(RegExp('[^0-9]'), ''));
2022-08-18 18:32:58 +02:00
return latestVersionInt > currentVersionInt;
} on Exception {
return false;
}
}
return false;
}
2022-08-18 16:33:33 +02:00
void updateManager(BuildContext context) async {
Fluttertoast.showToast(
msg: FlutterI18n.translate(
context,
'homeView.downloadingMessage',
),
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
);
File? managerApk = await _managerAPI.downloadManager();
2022-08-18 16:33:33 +02:00
if (managerApk != null) {
flutterLocalNotificationsPlugin.show(
0,
FlutterI18n.translate(
context,
'homeView.notificationTitle',
),
FlutterI18n.translate(
context,
'homeView.notificationText',
),
const NotificationDetails(
android: AndroidNotificationDetails(
'revanced_manager_channel',
'ReVanced Manager Channel',
),
),
);
try {
Fluttertoast.showToast(
msg: FlutterI18n.translate(
context,
'homeView.installingMessage',
),
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
);
await AppInstaller.installApk(managerApk.path);
} on Exception {
Fluttertoast.showToast(
msg: FlutterI18n.translate(
context,
'homeView.errorInstallMessage',
),
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
);
}
} else {
Fluttertoast.showToast(
msg: FlutterI18n.translate(
context,
'homeView.errorDownloadMessage',
),
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
);
2022-08-18 16:33:33 +02:00
}
}
Future<void> showUpdateConfirmationDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText('homeView.updateDialogTitle'),
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
content: I18nText('homeView.updateDialogText'),
actions: <Widget>[
CustomMaterialButton(
isFilled: false,
label: I18nText('cancelButton'),
onPressed: () => Navigator.of(context).pop(),
),
CustomMaterialButton(
label: I18nText('okButton'),
onPressed: () => updateManager(context),
)
],
),
);
}
Future<String?> getLatestPatcherReleaseTime() async {
return _managerAPI.getLatestPatcherReleaseTime();
}
Future<String?> getLatestManagerReleaseTime() async {
return _managerAPI.getLatestManagerReleaseTime();
}
Future<void> forceRefresh(BuildContext context) async {
await Future.delayed(const Duration(seconds: 1));
if (_lastUpdate == null ||
_lastUpdate!.difference(DateTime.now()).inSeconds > 60) {
_managerAPI.clearAllData();
}
initialize(context);
}
}