2022-08-18 23:26:12 +02:00
|
|
|
// 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:device_apps/device_apps.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter_i18n/flutter_i18n.dart';
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2022-08-18 23:26:12 +02:00
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2022-08-16 15:06:56 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2022-08-06 23:35:35 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2022-08-21 03:30:40 +02:00
|
|
|
import 'package:revanced_manager/main_viewmodel.dart';
|
2022-08-16 15:06:56 +02:00
|
|
|
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-08-18 16:33:33 +02:00
|
|
|
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
|
2022-08-06 14:13:28 +02:00
|
|
|
import 'package:stacked/stacked.dart';
|
|
|
|
|
2022-08-16 15:06:56 +02:00
|
|
|
@lazySingleton
|
2022-08-06 14:13:28 +02:00
|
|
|
class HomeViewModel extends BaseViewModel {
|
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 =
|
|
|
|
FlutterLocalNotificationsPlugin();
|
2022-08-16 15:06:56 +02:00
|
|
|
bool showUpdatableApps = true;
|
2022-08-29 16:01:51 +02:00
|
|
|
List<PatchedApplication> patchedInstalledApps = [];
|
|
|
|
List<PatchedApplication> patchedUpdatableApps = [];
|
2022-08-16 15:06:56 +02:00
|
|
|
|
2022-08-17 19:44:27 +02:00
|
|
|
Future<void> initialize() async {
|
2022-08-18 16:33:33 +02:00
|
|
|
await flutterLocalNotificationsPlugin.initialize(
|
|
|
|
const InitializationSettings(
|
|
|
|
android: AndroidInitializationSettings('ic_notification'),
|
|
|
|
),
|
|
|
|
onSelectNotification: (p) => DeviceApps.openApp('app.revanced.manager'),
|
|
|
|
);
|
2022-08-30 03:07:28 +02:00
|
|
|
_getPatchedApps();
|
|
|
|
_managerAPI.reAssessSavedApps().then((_) => _getPatchedApps());
|
2022-08-17 19:44:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:06:56 +02:00
|
|
|
void toggleUpdatableApps(bool value) {
|
|
|
|
showUpdatableApps = value;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-21 03:30:40 +02:00
|
|
|
void navigateToPatcher(PatchedApplication app) async {
|
2022-08-18 16:33:33 +02:00
|
|
|
locator<PatcherViewModel>().selectedApp = app;
|
|
|
|
locator<PatcherViewModel>().selectedPatches =
|
2022-08-29 18:44:45 +02:00
|
|
|
await _patcherAPI.getAppliedPatches(app.appliedPatches);
|
2022-08-21 03:30:40 +02:00
|
|
|
locator<PatcherViewModel>().notifyListeners();
|
|
|
|
locator<MainViewModel>().setIndex(1);
|
2022-08-17 19:44:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 03:07:28 +02:00
|
|
|
void _getPatchedApps() {
|
2022-08-31 10:33:54 +02:00
|
|
|
patchedInstalledApps = _managerAPI
|
|
|
|
.getPatchedApps()
|
|
|
|
.where((app) => app.hasUpdates == false)
|
|
|
|
.toList();
|
2022-08-29 16:01:51 +02:00
|
|
|
patchedUpdatableApps = _managerAPI
|
|
|
|
.getPatchedApps()
|
|
|
|
.where((app) => app.hasUpdates == true)
|
|
|
|
.toList();
|
|
|
|
notifyListeners();
|
2022-08-16 15:06:56 +02:00
|
|
|
}
|
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 =
|
2022-08-26 03:01:53 +02:00
|
|
|
int.parse(latestVersion.replaceAll(RegExp('[^0-9]'), ''));
|
2022-08-18 18:32:58 +02:00
|
|
|
int currentVersionInt =
|
2022-08-26 03:01:53 +02:00
|
|
|
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 {
|
2022-08-18 23:26:12 +02:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'homeView.downloadingMessage',
|
|
|
|
),
|
|
|
|
toastLength: Toast.LENGTH_LONG,
|
|
|
|
gravity: ToastGravity.CENTER,
|
|
|
|
);
|
2022-08-19 20:13:43 +02:00
|
|
|
File? managerApk = await _managerAPI.downloadManager('.apk');
|
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',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2022-08-18 23:26:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-08-06 14:13:28 +02:00
|
|
|
}
|