import 'package:device_apps/device_apps.dart'; import 'package:flutter_background/flutter_background.dart'; import 'package:revanced_manager/app/app.locator.dart'; import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patched_application.dart'; import 'package:revanced_manager/services/patcher_api.dart'; import 'package:revanced_manager/ui/views/app_selector/app_selector_viewmodel.dart'; import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart'; import 'package:revanced_manager/ui/views/patches_selector/patches_selector_viewmodel.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:stacked/stacked.dart'; class InstallerViewModel extends BaseViewModel { double? progress = 0.0; String logs = ''; bool isPatching = false; bool isInstalled = false; Future initialize() async { await FlutterBackground.initialize( androidConfig: const FlutterBackgroundAndroidConfig( notificationTitle: 'Patching', notificationText: 'ReVanced Manager is patching', notificationImportance: AndroidNotificationImportance.Default, notificationIcon: AndroidResource( name: 'ic_notification', defType: 'drawable', ), ), ); await FlutterBackground.enableBackgroundExecution(); await locator().handlePlatformChannelMethods(); await runPatcher(); } void updateProgress(double value) { progress = value; isInstalled = false; isPatching = progress == 1.0 ? false : true; if (progress == 0.0) { logs = ''; } notifyListeners(); } void updateLog(String message) { if (message.isNotEmpty && !message.startsWith('Merging L')) { if (logs.isNotEmpty) { logs += '\n'; } logs += message; notifyListeners(); } } Future runPatcher() async { updateProgress(0.0); PatchedApplication? selectedApp = locator().selectedApp; List selectedPatches = locator().selectedPatches; if (selectedApp != null && selectedPatches.isNotEmpty) { String apkFilePath = selectedApp.apkFilePath; try { updateLog('Initializing installer'); if (selectedApp.isRooted && !selectedApp.isFromStorage) { updateLog('Checking if an old patched version exists'); bool oldExists = await locator().checkOldPatch(selectedApp); if (oldExists) { updateLog('Deleting old patched version'); await locator().deleteOldPatch(selectedApp); } } updateLog('Creating working directory'); bool mergeIntegrations = false; bool resourcePatching = false; if (selectedApp.packageName == 'com.google.android.youtube') { mergeIntegrations = true; resourcePatching = true; } else if (selectedApp.packageName == 'com.google.android.apps.youtube.music') { resourcePatching = true; } await locator().initPatcher(mergeIntegrations); await locator().runPatcher( apkFilePath, selectedPatches, mergeIntegrations, resourcePatching, ); } on Exception { updateLog('An error occurred! Aborting'); } } else { updateLog('No app or patches selected! Aborting'); } await FlutterBackground.disableBackgroundExecution(); isPatching = false; } void installResult() async { PatchedApplication? selectedApp = locator().selectedApp; if (selectedApp != null) { updateLog(selectedApp.isRooted ? 'Installing patched file using root method' : 'Installing patched file using nonroot method'); isInstalled = await locator().installPatchedFile(selectedApp); if (isInstalled) { updateLog('Done'); await saveApp(selectedApp); } else { updateLog('An error occurred! Aborting'); } } } void shareResult() { PatchedApplication? selectedApp = locator().selectedApp; if (selectedApp != null) { locator().sharePatchedFile( selectedApp.name, selectedApp.version, ); } } Future cleanWorkplace() async { locator().cleanPatcher(); locator().selectedApp = null; locator().selectedPatches.clear(); locator().notifyListeners(); } void openApp() { PatchedApplication? selectedApp = locator().selectedApp; if (selectedApp != null) { DeviceApps.openApp(selectedApp.packageName); } } Future saveApp(PatchedApplication selectedApp) async { SharedPreferences prefs = await SharedPreferences.getInstance(); List patchedApps = prefs.getStringList('patchedApps') ?? []; String app = selectedApp.toJson().toString(); if (!patchedApps.contains(app)) { patchedApps.add(app); prefs.setStringList('patchedApps', patchedApps); } } }