2022-08-18 23:26:12 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
2023-04-20 01:15:46 +02:00
|
|
|
import 'dart:async';
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'dart:io';
|
2023-01-30 13:35:06 +01:00
|
|
|
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'package:app_installer/app_installer.dart';
|
2022-09-12 01:35:41 +02:00
|
|
|
import 'package:cross_connectivity/cross_connectivity.dart';
|
2023-03-05 10:12:46 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-09-07 14:03:54 +02:00
|
|
|
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';
|
2022-08-16 15:06:56 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2023-04-20 01:15:46 +02:00
|
|
|
import 'package:path_provider/path_provider.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';
|
2022-08-16 15:06:56 +02:00
|
|
|
import 'package:revanced_manager/models/patched_application.dart';
|
2023-01-30 13:35:06 +01:00
|
|
|
import 'package:revanced_manager/services/github_api.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';
|
2023-04-20 01:15:46 +02:00
|
|
|
import 'package:revanced_manager/services/revanced_api.dart';
|
2022-09-20 01:49:31 +02:00
|
|
|
import 'package:revanced_manager/services/toast.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';
|
2022-11-29 16:45:44 +01:00
|
|
|
import 'package:revanced_manager/ui/widgets/homeView/update_confirmation_dialog.dart';
|
2023-04-20 01:15:46 +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-16 15:06:56 +02:00
|
|
|
@lazySingleton
|
2022-08-06 14:13:28 +02:00
|
|
|
class HomeViewModel extends BaseViewModel {
|
2023-04-20 01:15:46 +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>();
|
2022-11-29 16:45:44 +01:00
|
|
|
final GithubAPI _githubAPI = locator<GithubAPI>();
|
2023-04-20 01:15:46 +02:00
|
|
|
final RevancedAPI _revancedAPI = locator<RevancedAPI>();
|
2022-09-20 01:49:31 +02:00
|
|
|
final Toast _toast = locator<Toast>();
|
2023-04-20 01:15:46 +02:00
|
|
|
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
2022-09-11 03:01:06 +02:00
|
|
|
DateTime? _lastUpdate;
|
2022-10-19 23:15:48 +02:00
|
|
|
bool showUpdatableApps = false;
|
2022-08-29 16:01:51 +02:00
|
|
|
List<PatchedApplication> patchedInstalledApps = [];
|
|
|
|
List<PatchedApplication> patchedUpdatableApps = [];
|
2023-05-10 12:23:02 +02:00
|
|
|
String? _latestManagerVersion = '';
|
2022-08-16 15:06:56 +02:00
|
|
|
|
2022-09-12 01:35:41 +02:00
|
|
|
Future<void> initialize(BuildContext context) async {
|
2023-05-10 12:23:02 +02:00
|
|
|
_latestManagerVersion = await _managerAPI.getLatestManagerVersion();
|
2022-08-18 16:33:33 +02:00
|
|
|
await flutterLocalNotificationsPlugin.initialize(
|
|
|
|
const InitializationSettings(
|
|
|
|
android: AndroidInitializationSettings('ic_notification'),
|
|
|
|
),
|
2023-04-18 16:15:29 +02:00
|
|
|
onDidReceiveNotificationResponse: (response) async {
|
|
|
|
if (response.id == 0) {
|
|
|
|
_toast.showBottom('homeView.installingMessage');
|
|
|
|
final File? managerApk = await _managerAPI.downloadManager();
|
|
|
|
if (managerApk != null) {
|
|
|
|
await AppInstaller.installApk(managerApk.path);
|
|
|
|
} else {
|
|
|
|
_toast.showBottom('homeView.errorDownloadMessage');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-08-18 16:33:33 +02:00
|
|
|
);
|
2022-09-20 01:03:37 +02:00
|
|
|
flutterLocalNotificationsPlugin
|
|
|
|
.resolvePlatformSpecificImplementation<
|
|
|
|
AndroidFlutterLocalNotificationsPlugin>()
|
|
|
|
?.requestPermission();
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool isConnected = await Connectivity().checkConnection();
|
2022-09-12 01:35:41 +02:00
|
|
|
if (!isConnected) {
|
2022-11-12 16:55:33 +01:00
|
|
|
_toast.showBottom('homeView.noConnection');
|
2022-09-12 01:35:41 +02:00
|
|
|
}
|
2023-04-18 16:15:29 +02:00
|
|
|
final NotificationAppLaunchDetails? notificationAppLaunchDetails =
|
2023-04-20 01:15:46 +02:00
|
|
|
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
|
|
|
|
if (notificationAppLaunchDetails?.didNotificationLaunchApp ?? false) {
|
2023-04-18 16:15:29 +02:00
|
|
|
_toast.showBottom('homeView.installingMessage');
|
|
|
|
final File? managerApk = await _managerAPI.downloadManager();
|
|
|
|
if (managerApk != null) {
|
|
|
|
await AppInstaller.installApk(managerApk.path);
|
|
|
|
} else {
|
|
|
|
_toast.showBottom('homeView.errorDownloadMessage');
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 03:07:28 +02:00
|
|
|
_getPatchedApps();
|
2022-10-11 16:57:29 +02:00
|
|
|
_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),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:06:56 +02:00
|
|
|
void toggleUpdatableApps(bool value) {
|
|
|
|
showUpdatableApps = value;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2023-01-30 13:35:06 +01:00
|
|
|
Future<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();
|
2022-09-05 10:07:38 +02:00
|
|
|
locator<NavigationViewModel>().setIndex(1);
|
2022-08-17 19:44:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 03:07:28 +02:00
|
|
|
void _getPatchedApps() {
|
2023-01-31 13:42:00 +01:00
|
|
|
patchedInstalledApps = _managerAPI.getPatchedApps().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 {
|
2023-04-29 21:09:42 +02:00
|
|
|
String currentVersion = await _managerAPI.getCurrentManagerVersion();
|
2023-06-12 02:25:53 +02:00
|
|
|
_latestManagerVersion = await _managerAPI.getLatestManagerVersion();
|
2023-06-12 02:31:57 +02:00
|
|
|
|
2023-04-29 21:09:42 +02:00
|
|
|
// add v to current version
|
|
|
|
if (!currentVersion.startsWith('v')) {
|
|
|
|
currentVersion = 'v$currentVersion';
|
|
|
|
}
|
|
|
|
|
2023-05-10 12:23:02 +02:00
|
|
|
if (_latestManagerVersion != currentVersion) {
|
2023-04-20 23:36:59 +02:00
|
|
|
return true;
|
2022-08-18 18:32:58 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-18 09:57:26 +02:00
|
|
|
Future<bool> hasPatchesUpdates() async {
|
|
|
|
final String? latestVersion = await _managerAPI.getLatestPatchesVersion();
|
|
|
|
final String? currentVersion = await _managerAPI.getCurrentPatchesVersion();
|
|
|
|
if (latestVersion != null) {
|
|
|
|
try {
|
|
|
|
final int latestVersionInt =
|
|
|
|
int.parse(latestVersion.replaceAll(RegExp('[^0-9]'), ''));
|
|
|
|
final int currentVersionInt =
|
|
|
|
int.parse(currentVersion!.replaceAll(RegExp('[^0-9]'), ''));
|
|
|
|
return latestVersionInt > currentVersionInt;
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-20 01:15:46 +02:00
|
|
|
Future<File?> downloadManager() async {
|
|
|
|
try {
|
|
|
|
final response = await _revancedAPI.downloadManager();
|
|
|
|
final bytes = await response!.readAsBytes();
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
|
|
final tempFile = File('${tempDir.path}/revanced-manager.apk');
|
|
|
|
await tempFile.writeAsBytes(bytes);
|
|
|
|
return tempFile;
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 01:03:37 +02:00
|
|
|
Future<void> updateManager(BuildContext context) async {
|
|
|
|
try {
|
2022-11-12 16:55:33 +01:00
|
|
|
_toast.showBottom('homeView.downloadingMessage');
|
2023-04-20 01:15:46 +02:00
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => SimpleDialog(
|
|
|
|
contentPadding: const EdgeInsets.all(16.0),
|
|
|
|
title: I18nText(
|
|
|
|
'homeView.downloadingMessage',
|
|
|
|
child: Text(
|
|
|
|
'',
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 20,
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
|
|
),
|
2022-09-20 01:03:37 +02:00
|
|
|
),
|
|
|
|
),
|
2023-04-20 01:15:46 +02:00
|
|
|
children: [
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.new_releases_outlined,
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 8.0),
|
|
|
|
Text(
|
2023-05-26 12:54:52 +02:00
|
|
|
'$_latestManagerVersion',
|
2023-04-20 01:15:46 +02:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18,
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16.0),
|
|
|
|
StreamBuilder<double>(
|
|
|
|
initialData: 0.0,
|
|
|
|
stream: _revancedAPI.managerUpdateProgress.stream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return LinearProgressIndicator(
|
|
|
|
value: snapshot.data! * 0.01,
|
|
|
|
valueColor: AlwaysStoppedAnimation<Color>(
|
|
|
|
Theme.of(context).colorScheme.secondary,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16.0),
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
child: CustomMaterialButton(
|
|
|
|
label: I18nText('cancelButton'),
|
|
|
|
onPressed: () {
|
|
|
|
_revancedAPI.disposeManagerUpdateProgress();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
final File? managerApk = await downloadManager();
|
|
|
|
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 16:55:33 +01:00
|
|
|
_toast.showBottom('homeView.installingMessage');
|
2022-08-18 23:26:12 +02:00
|
|
|
await AppInstaller.installApk(managerApk.path);
|
2022-09-20 01:03:37 +02:00
|
|
|
} else {
|
2022-11-12 16:55:33 +01:00
|
|
|
_toast.showBottom('homeView.errorDownloadMessage');
|
2022-08-18 23:26:12 +02:00
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-11-12 16:55:33 +01:00
|
|
|
_toast.showBottom('homeView.errorInstallMessage');
|
2022-08-18 16:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 14:03:54 +02:00
|
|
|
|
2022-10-19 13:02:54 +02:00
|
|
|
void updatesAreDisabled() {
|
2022-11-12 16:55:33 +01:00
|
|
|
_toast.showBottom('homeView.updatesDisabled');
|
2022-10-19 13:02:54 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:15:29 +02:00
|
|
|
Future<void> showUpdateConfirmationDialog(
|
|
|
|
BuildContext parentContext,
|
|
|
|
) {
|
2022-11-29 16:45:44 +01:00
|
|
|
return showModalBottomSheet(
|
2022-09-20 01:03:37 +02: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 14:03:54 +02:00
|
|
|
),
|
2022-11-29 16:45:44 +01:00
|
|
|
builder: (context) => const UpdateConfirmationDialog(),
|
2022-09-07 14:03:54 +02:00
|
|
|
);
|
|
|
|
}
|
2022-09-11 03:01:06 +02: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 03:01:06 +02:00
|
|
|
return _managerAPI.getLatestPatcherReleaseTime();
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:45:44 +01:00
|
|
|
Future<String?> getLatestManagerReleaseTime() {
|
2022-09-11 03:01:06 +02:00
|
|
|
return _managerAPI.getLatestManagerReleaseTime();
|
|
|
|
}
|
|
|
|
|
2022-09-12 01:35:41 +02:00
|
|
|
Future<void> forceRefresh(BuildContext context) async {
|
2022-09-11 03:01:06 +02:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
if (_lastUpdate == null ||
|
2022-10-16 21:02:45 +02:00
|
|
|
_lastUpdate!.difference(DateTime.now()).inSeconds > 2) {
|
2022-09-11 03:01:06 +02:00
|
|
|
_managerAPI.clearAllData();
|
|
|
|
}
|
2023-04-18 09:57:26 +02:00
|
|
|
_toast.showBottom('homeView.refreshSuccess');
|
2022-09-12 01:35:41 +02:00
|
|
|
initialize(context);
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
2022-08-06 14:13:28 +02:00
|
|
|
}
|