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

107 lines
3.8 KiB
Dart
Raw Normal View History

import 'dart:convert';
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';
import 'package:injectable/injectable.dart';
2022-08-06 23:35:35 +02:00
import 'package:revanced_manager/app/app.locator.dart';
2022-08-17 19:44:27 +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-08-18 16:33:33 +02:00
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:stacked/stacked.dart';
2022-08-17 19:44:27 +02:00
import 'package:stacked_services/stacked_services.dart';
@lazySingleton
class HomeViewModel extends BaseViewModel {
2022-08-18 16:33:33 +02:00
final NavigationService _navigationService = locator<NavigationService>();
final ManagerAPI _managerAPI = ManagerAPI();
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
bool showUpdatableApps = true;
2022-08-17 19:44:27 +02:00
Future<void> initialize() async {
2022-08-18 16:33:33 +02:00
await _patcherAPI.loadPatches();
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('ic_notification'),
),
onSelectNotification: (p) => DeviceApps.openApp('app.revanced.manager'),
);
2022-08-17 19:44:27 +02:00
}
void toggleUpdatableApps(bool value) {
showUpdatableApps = value;
notifyListeners();
}
2022-08-17 19:44:27 +02:00
void navigateToInstaller(PatchedApplication app) async {
2022-08-18 16:33:33 +02:00
locator<PatcherViewModel>().selectedApp = app;
locator<PatcherViewModel>().selectedPatches =
await _patcherAPI.getAppliedPatches(app);
2022-08-17 19:44:27 +02:00
_navigationService.navigateTo(Routes.installerView);
}
Future<List<PatchedApplication>> getPatchedApps(bool isUpdatable) async {
2022-08-17 18:07:00 +02:00
List<PatchedApplication> list = [];
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> patchedApps = prefs.getStringList('patchedApps') ?? [];
2022-08-17 18:07:00 +02:00
for (String str in patchedApps) {
PatchedApplication app = PatchedApplication.fromJson(json.decode(str));
2022-08-18 18:32:58 +02:00
bool hasUpdates = await _managerAPI.hasAppUpdates(app.packageName);
2022-08-17 18:07:00 +02:00
if (hasUpdates == isUpdatable) {
list.add(app);
}
}
return list;
}
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.replaceFirst('v', '').replaceAll('.', ''));
int currentVersionInt =
int.parse(currentVersion.replaceFirst('v', '').replaceAll('.', ''));
return latestVersionInt > currentVersionInt;
} on Exception {
return false;
}
}
return false;
}
2022-08-18 16:33:33 +02:00
void updateManager(BuildContext context) async {
File? managerApk = await _managerAPI.downloadManager();
if (managerApk != null) {
flutterLocalNotificationsPlugin.show(
0,
// ignore: use_build_context_synchronously
FlutterI18n.translate(
context,
'homeView.notificationTitle',
),
// ignore: use_build_context_synchronously
FlutterI18n.translate(
context,
'homeView.notificationText',
),
const NotificationDetails(
android: AndroidNotificationDetails(
'revanced_manager_channel',
'ReVanced Manager Channel',
),
),
);
AppInstaller.installApk(managerApk.path);
}
}
}