2022-08-18 22:26:12 +01:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
2022-08-18 15:33:33 +01:00
|
|
|
import 'dart:io';
|
2023-01-30 18:05:06 +05:30
|
|
|
|
2022-08-18 15:33:33 +01:00
|
|
|
import 'package:app_installer/app_installer.dart';
|
2022-09-12 00:35:41 +01:00
|
|
|
import 'package:cross_connectivity/cross_connectivity.dart';
|
2022-08-18 15:33:33 +01:00
|
|
|
import 'package:device_apps/device_apps.dart';
|
2022-09-07 13:03:54 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2022-08-18 15:33:33 +01:00
|
|
|
import 'package:flutter_i18n/flutter_i18n.dart';
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2022-08-16 18:36:56 +05:30
|
|
|
import 'package:injectable/injectable.dart';
|
2022-08-06 22:35:35 +01:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2022-09-05 15:30:26 +01:00
|
|
|
import 'package:revanced_manager/app/app.router.dart';
|
2022-08-16 18:36:56 +05:30
|
|
|
import 'package:revanced_manager/models/patched_application.dart';
|
2023-01-30 18:05:06 +05:30
|
|
|
import 'package:revanced_manager/services/github_api.dart';
|
2022-08-06 22:35:35 +01:00
|
|
|
import 'package:revanced_manager/services/manager_api.dart';
|
2022-08-17 18:44:27 +01:00
|
|
|
import 'package:revanced_manager/services/patcher_api.dart';
|
2022-09-20 00:49:31 +01:00
|
|
|
import 'package:revanced_manager/services/toast.dart';
|
2022-09-05 09:07:38 +01:00
|
|
|
import 'package:revanced_manager/ui/views/navigation/navigation_viewmodel.dart';
|
2022-08-18 15:33:33 +01:00
|
|
|
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
|
2022-11-29 16:45:44 +01:00
|
|
|
import 'package:revanced_manager/ui/widgets/homeView/update_confirmation_dialog.dart';
|
2022-10-14 23:35:33 +05:30
|
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
2022-08-06 17:43:28 +05:30
|
|
|
import 'package:stacked/stacked.dart';
|
2022-09-05 15:30:26 +01:00
|
|
|
import 'package:stacked_services/stacked_services.dart';
|
2022-09-20 00:03:37 +01:00
|
|
|
import 'package:timezone/timezone.dart' as tz;
|
2022-08-06 17:43:28 +05:30
|
|
|
|
2022-08-16 18:36:56 +05:30
|
|
|
@lazySingleton
|
2022-08-06 17:43:28 +05:30
|
|
|
class HomeViewModel extends BaseViewModel {
|
2022-09-05 15:30:26 +01:00
|
|
|
final NavigationService _navigationService = locator<NavigationService>();
|
2022-08-25 00:51:47 +01:00
|
|
|
final ManagerAPI _managerAPI = locator<ManagerAPI>();
|
2022-08-18 15:33:33 +01:00
|
|
|
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
|
2022-11-29 16:45:44 +01:00
|
|
|
final GithubAPI _githubAPI = locator<GithubAPI>();
|
2022-09-20 00:49:31 +01:00
|
|
|
final Toast _toast = locator<Toast>();
|
2022-09-11 02:01:06 +01:00
|
|
|
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
|
|
|
DateTime? _lastUpdate;
|
2022-10-20 00:15:48 +03:00
|
|
|
bool showUpdatableApps = false;
|
2022-08-29 15:01:51 +01:00
|
|
|
List<PatchedApplication> patchedInstalledApps = [];
|
|
|
|
List<PatchedApplication> patchedUpdatableApps = [];
|
2022-08-16 18:36:56 +05:30
|
|
|
|
2022-09-12 00:35:41 +01:00
|
|
|
Future<void> initialize(BuildContext context) async {
|
2022-08-18 15:33:33 +01:00
|
|
|
await flutterLocalNotificationsPlugin.initialize(
|
|
|
|
const InitializationSettings(
|
|
|
|
android: AndroidInitializationSettings('ic_notification'),
|
|
|
|
),
|
2022-09-12 18:23:34 +05:30
|
|
|
onSelectNotification: (p) =>
|
|
|
|
DeviceApps.openApp('app.revanced.manager.flutter'),
|
2022-08-18 15:33:33 +01:00
|
|
|
);
|
2022-09-20 00:03:37 +01:00
|
|
|
flutterLocalNotificationsPlugin
|
|
|
|
.resolvePlatformSpecificImplementation<
|
|
|
|
AndroidFlutterLocalNotificationsPlugin>()
|
|
|
|
?.requestPermission();
|
2023-01-30 18:05:06 +05:30
|
|
|
final bool isConnected = await Connectivity().checkConnection();
|
2022-09-12 00:35:41 +01:00
|
|
|
if (!isConnected) {
|
2022-11-12 21:25:33 +05:30
|
|
|
_toast.showBottom('homeView.noConnection');
|
2022-09-12 00:35:41 +01:00
|
|
|
}
|
2022-08-30 02:07:28 +01:00
|
|
|
_getPatchedApps();
|
2022-10-11 20:27:29 +05:30
|
|
|
_managerAPI.reAssessSavedApps().then((_) => _getPatchedApps());
|
2022-08-17 18:44:27 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 15:30:26 +01:00
|
|
|
void navigateToAppInfo(PatchedApplication app) {
|
|
|
|
_navigationService.navigateTo(
|
|
|
|
Routes.appInfoView,
|
|
|
|
arguments: AppInfoViewArguments(app: app),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-16 18:36:56 +05:30
|
|
|
void toggleUpdatableApps(bool value) {
|
|
|
|
showUpdatableApps = value;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2023-01-30 18:05:06 +05:30
|
|
|
Future<void> navigateToPatcher(PatchedApplication app) async {
|
2022-08-18 15:33:33 +01:00
|
|
|
locator<PatcherViewModel>().selectedApp = app;
|
|
|
|
locator<PatcherViewModel>().selectedPatches =
|
2022-08-29 17:44:45 +01:00
|
|
|
await _patcherAPI.getAppliedPatches(app.appliedPatches);
|
2022-08-21 02:30:40 +01:00
|
|
|
locator<PatcherViewModel>().notifyListeners();
|
2022-09-05 09:07:38 +01:00
|
|
|
locator<NavigationViewModel>().setIndex(1);
|
2022-08-17 18:44:27 +01:00
|
|
|
}
|
|
|
|
|
2022-08-30 02:07:28 +01:00
|
|
|
void _getPatchedApps() {
|
2023-01-31 18:12:00 +05:30
|
|
|
patchedInstalledApps = _managerAPI.getPatchedApps().toList();
|
2022-08-29 15:01:51 +01:00
|
|
|
patchedUpdatableApps = _managerAPI
|
|
|
|
.getPatchedApps()
|
|
|
|
.where((app) => app.hasUpdates == true)
|
|
|
|
.toList();
|
|
|
|
notifyListeners();
|
2022-08-16 18:36:56 +05:30
|
|
|
}
|
2022-08-18 15:33:33 +01:00
|
|
|
|
2022-08-18 17:32:58 +01:00
|
|
|
Future<bool> hasManagerUpdates() async {
|
2023-01-30 18:05:06 +05:30
|
|
|
final String? latestVersion = await _managerAPI.getLatestManagerVersion();
|
|
|
|
final String currentVersion = await _managerAPI.getCurrentManagerVersion();
|
2022-08-18 17:32:58 +01:00
|
|
|
if (latestVersion != null) {
|
|
|
|
try {
|
2023-01-30 18:05:06 +05:30
|
|
|
final int latestVersionInt =
|
2022-08-26 02:01:53 +01:00
|
|
|
int.parse(latestVersion.replaceAll(RegExp('[^0-9]'), ''));
|
2023-01-30 18:05:06 +05:30
|
|
|
final int currentVersionInt =
|
2022-08-26 02:01:53 +01:00
|
|
|
int.parse(currentVersion.replaceAll(RegExp('[^0-9]'), ''));
|
2022-08-18 17:32:58 +01:00
|
|
|
return latestVersionInt > currentVersionInt;
|
2022-10-14 23:35:33 +05:30
|
|
|
} on Exception catch (e, s) {
|
2023-02-21 18:06:56 +05:45
|
|
|
Sentry.captureException(e, stackTrace: s).ignore();
|
2022-08-18 17:32:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-20 00:03:37 +01:00
|
|
|
Future<void> updateManager(BuildContext context) async {
|
|
|
|
try {
|
2022-11-12 21:25:33 +05:30
|
|
|
_toast.showBottom('homeView.downloadingMessage');
|
2023-01-30 18:05:06 +05:30
|
|
|
final File? managerApk = await _managerAPI.downloadManager();
|
2022-09-20 00:03:37 +01:00
|
|
|
if (managerApk != null) {
|
|
|
|
await flutterLocalNotificationsPlugin.zonedSchedule(
|
|
|
|
0,
|
|
|
|
FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'homeView.notificationTitle',
|
|
|
|
),
|
|
|
|
FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'homeView.notificationText',
|
|
|
|
),
|
|
|
|
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
|
|
|
|
const NotificationDetails(
|
|
|
|
android: AndroidNotificationDetails(
|
|
|
|
'revanced_manager_channel',
|
|
|
|
'ReVanced Manager Channel',
|
|
|
|
importance: Importance.max,
|
|
|
|
priority: Priority.high,
|
|
|
|
ticker: 'ticker',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
androidAllowWhileIdle: true,
|
|
|
|
uiLocalNotificationDateInterpretation:
|
|
|
|
UILocalNotificationDateInterpretation.absoluteTime,
|
|
|
|
);
|
2022-11-12 21:25:33 +05:30
|
|
|
_toast.showBottom('homeView.installingMessage');
|
2022-08-18 22:26:12 +01:00
|
|
|
await AppInstaller.installApk(managerApk.path);
|
2022-09-20 00:03:37 +01:00
|
|
|
} else {
|
2022-11-12 21:25:33 +05:30
|
|
|
_toast.showBottom('homeView.errorDownloadMessage');
|
2022-08-18 22:26:12 +01:00
|
|
|
}
|
2022-10-14 23:35:33 +05:30
|
|
|
} on Exception catch (e, s) {
|
2023-02-21 18:06:56 +05:45
|
|
|
Sentry.captureException(e, stackTrace: s).ignore();
|
2022-11-12 21:25:33 +05:30
|
|
|
_toast.showBottom('homeView.errorInstallMessage');
|
2022-08-18 15:33:33 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 13:03:54 +01:00
|
|
|
|
2022-10-19 16:32:54 +05:30
|
|
|
void updatesAreDisabled() {
|
2022-11-12 21:25:33 +05:30
|
|
|
_toast.showBottom('homeView.updatesDisabled');
|
2022-10-19 16:32:54 +05:30
|
|
|
}
|
|
|
|
|
2022-11-29 16:45:44 +01:00
|
|
|
Future<void> showUpdateConfirmationDialog(BuildContext parentContext) {
|
|
|
|
return showModalBottomSheet(
|
2022-09-20 00:03:37 +01:00
|
|
|
context: parentContext,
|
2022-11-29 16:45:44 +01:00
|
|
|
isScrollControlled: true,
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.vertical(top: Radius.circular(24.0)),
|
2022-09-07 13:03:54 +01:00
|
|
|
),
|
2022-11-29 16:45:44 +01:00
|
|
|
builder: (context) => const UpdateConfirmationDialog(),
|
2022-09-07 13:03:54 +01:00
|
|
|
);
|
|
|
|
}
|
2022-09-11 02:01:06 +01:00
|
|
|
|
2022-11-29 16:45:44 +01:00
|
|
|
Future<Map<String, dynamic>?> getLatestManagerRelease() {
|
|
|
|
return _githubAPI.getLatestRelease(_managerAPI.defaultManagerRepo);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String?> getLatestPatcherReleaseTime() {
|
2022-09-11 02:01:06 +01:00
|
|
|
return _managerAPI.getLatestPatcherReleaseTime();
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:45:44 +01:00
|
|
|
Future<String?> getLatestManagerReleaseTime() {
|
2022-09-11 02:01:06 +01:00
|
|
|
return _managerAPI.getLatestManagerReleaseTime();
|
|
|
|
}
|
|
|
|
|
2022-09-12 00:35:41 +01:00
|
|
|
Future<void> forceRefresh(BuildContext context) async {
|
2022-09-11 02:01:06 +01:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
if (_lastUpdate == null ||
|
2022-10-17 00:32:45 +05:30
|
|
|
_lastUpdate!.difference(DateTime.now()).inSeconds > 2) {
|
2022-09-11 02:01:06 +01:00
|
|
|
_managerAPI.clearAllData();
|
|
|
|
}
|
2022-09-12 00:35:41 +01:00
|
|
|
initialize(context);
|
2022-09-11 02:01:06 +01:00
|
|
|
}
|
2022-08-06 17:43:28 +05:30
|
|
|
}
|