feat: call Installer through Home View

This commit is contained in:
Alberto Ponces 2022-08-17 18:44:27 +01:00
parent 750f035104
commit 40f27b3a09
10 changed files with 139 additions and 78 deletions

View File

@ -18,7 +18,7 @@ class PatchedApplication {
final DateTime patchDate;
final bool isRooted;
final bool isFromStorage;
final List<String> appliedPatches;
List<String> appliedPatches;
PatchedApplication({
required this.name,
@ -29,7 +29,7 @@ class PatchedApplication {
required this.patchDate,
required this.isRooted,
required this.isFromStorage,
this.appliedPatches = const <String>[],
required this.appliedPatches,
});
factory PatchedApplication.fromJson(Map<String, dynamic> json) =>

View File

@ -24,8 +24,6 @@ class PatcherAPI {
);
final GithubAPI githubAPI = GithubAPI();
final RootAPI rootAPI = RootAPI();
final List<ApplicationWithIcon> _filteredPackages = [];
final Map<String, List<Patch>> _filteredPatches = <String, List<Patch>>{};
Directory? _tmpDir;
Directory? _workDir;
Directory? _cacheDir;
@ -77,7 +75,8 @@ class PatcherAPI {
}
Future<List<ApplicationWithIcon>> getFilteredInstalledApps() async {
if (_patchBundleFile != null && _filteredPackages.isEmpty) {
List<ApplicationWithIcon> filteredPackages = [];
if (_patchBundleFile != null) {
try {
List<String>? patchesPackages = await patcherChannel
.invokeListMethod<String>('getCompatiblePackages');
@ -87,7 +86,7 @@ class PatcherAPI {
ApplicationWithIcon? app = await DeviceApps.getApp(package, true)
as ApplicationWithIcon?;
if (app != null) {
_filteredPackages.add(app);
filteredPackages.add(app);
}
} catch (e) {
continue;
@ -95,58 +94,91 @@ class PatcherAPI {
}
}
} on Exception {
_filteredPackages.clear();
return List.empty();
}
}
return _filteredPackages;
return filteredPackages;
}
Future<List<Patch>?> getFilteredPatches(
Future<List<Patch>> getFilteredPatches(
PatchedApplication? selectedApp,
) async {
List<Patch> filteredPatches = [];
if (_patchBundleFile != null && selectedApp != null) {
if (_filteredPatches[selectedApp.packageName] == null ||
_filteredPatches[selectedApp.packageName]!.isEmpty) {
_filteredPatches[selectedApp.packageName] = [];
try {
var patches =
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
'getFilteredPatches',
{
'targetPackage': selectedApp.packageName,
'targetVersion': selectedApp.version,
'ignoreVersion': true,
},
);
if (patches != null) {
for (var patch in patches) {
if (!_filteredPatches[selectedApp.packageName]!
.any((element) => element.name == patch['name'])) {
_filteredPatches[selectedApp.packageName]!.add(
Patch(
name: patch['name'],
simpleName: (patch['name'] as String)
.replaceAll('-', ' ')
.split('-')
.join(' ')
.toTitleCase(),
version: patch['version'] ?? '?.?.?',
description: patch['description'] ?? 'N/A',
),
);
}
try {
var patches =
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
'getFilteredPatches',
{
'targetPackage': selectedApp.packageName,
'targetVersion': selectedApp.version,
'ignoreVersion': true,
},
);
if (patches != null) {
for (var patch in patches) {
if (!filteredPatches
.any((element) => element.name == patch['name'])) {
filteredPatches.add(
Patch(
name: patch['name'],
simpleName: (patch['name'] as String)
.replaceAll('-', ' ')
.split('-')
.join(' ')
.toTitleCase(),
version: patch['version'] ?? '?.?.?',
description: patch['description'] ?? 'N/A',
),
);
}
}
} on Exception {
_filteredPatches[selectedApp.packageName]!.clear();
return List.empty();
}
} on Exception {
return List.empty();
}
} else {
return List.empty();
}
return _filteredPatches[selectedApp.packageName];
return filteredPatches;
}
Future<List<Patch>> getAppliedPatches(
PatchedApplication? selectedApp,
) async {
List<Patch> appliedPatches = [];
if (_patchBundleFile != null && selectedApp != null) {
try {
var patches =
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
'getFilteredPatches',
{
'targetPackage': selectedApp.packageName,
'targetVersion': selectedApp.version,
'ignoreVersion': true,
},
);
if (patches != null) {
for (var patch in patches) {
if (selectedApp.appliedPatches.contains(patch['name'])) {
appliedPatches.add(
Patch(
name: patch['name'],
simpleName: (patch['name'] as String)
.replaceAll('-', ' ')
.split('-')
.join(' ')
.toTitleCase(),
version: patch['version'] ?? '?.?.?',
description: patch['description'] ?? 'N/A',
),
);
}
}
}
} on Exception {
return List.empty();
}
}
return appliedPatches;
}
Future<File?> downloadIntegrations() async {
@ -187,7 +219,7 @@ class PatcherAPI {
'patchedFilePath': _patchedFile!.path,
'outFilePath': _outFile!.path,
'integrationsPath': _integrations!.path,
'selectedPatches': selectedPatches.map((e) => e.name).toList(),
'selectedPatches': selectedPatches.map((p) => p.name).toList(),
'cacheDirPath': _cacheDir!.path,
'mergeIntegrations': mergeIntegrations,
'resourcePatching': resourcePatching,

View File

@ -27,7 +27,6 @@ class AppSelectorViewModel extends BaseViewModel {
}
Future<void> getApps() async {
await patcherAPI.loadPatches();
apps = await patcherAPI.getFilteredInstalledApps();
}
@ -42,6 +41,7 @@ class AppSelectorViewModel extends BaseViewModel {
patchDate: DateTime.now(),
isRooted: isRooted,
isFromStorage: isFromStorage,
appliedPatches: [],
);
locator<AppSelectorViewModel>().selectedApp = app;
locator<PatchesSelectorViewModel>().selectedPatches.clear();
@ -70,6 +70,7 @@ class AppSelectorViewModel extends BaseViewModel {
patchDate: DateTime.now(),
isRooted: isRooted,
isFromStorage: isFromStorage,
appliedPatches: [],
);
locator<AppSelectorViewModel>().selectedApp = app;
locator<PatchesSelectorViewModel>().selectedPatches.clear();

View File

@ -17,6 +17,8 @@ class HomeView extends StatelessWidget {
Widget build(BuildContext context) {
return ViewModelBuilder<HomeViewModel>.reactive(
disposeViewModel: false,
fireOnModelReadyOnce: true,
onModelReady: (model) => model.initialize(),
viewModelBuilder: () => locator<HomeViewModel>(),
builder: (context, model, child) => Scaffold(
body: SafeArea(

View File

@ -1,17 +1,28 @@
import 'dart:convert';
import 'package:injectable/injectable.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/app/app.router.dart';
import 'package:revanced_manager/models/patched_application.dart';
import 'package:revanced_manager/services/github_api.dart';
import 'package:revanced_manager/services/manager_api.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/patches_selector/patches_selector_viewmodel.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
@lazySingleton
class HomeViewModel extends BaseViewModel {
final _navigationService = locator<NavigationService>();
final GithubAPI githubAPI = GithubAPI();
final PatcherAPI patcherAPI = locator<PatcherAPI>();
bool showUpdatableApps = true;
Future<void> initialize() async {
await patcherAPI.loadPatches();
}
void toggleUpdatableApps(bool value) {
showUpdatableApps = value;
notifyListeners();
@ -20,6 +31,13 @@ class HomeViewModel extends BaseViewModel {
Future downloadPatches() => locator<ManagerAPI>().downloadPatches();
Future downloadIntegrations() => locator<ManagerAPI>().downloadIntegrations();
void navigateToInstaller(PatchedApplication app) async {
locator<AppSelectorViewModel>().selectedApp = app;
locator<PatchesSelectorViewModel>().selectedPatches =
await patcherAPI.getAppliedPatches(app);
_navigationService.navigateTo(Routes.installerView);
}
Future<List<PatchedApplication>> getPatchedApps(bool isUpdatable) async {
List<PatchedApplication> list = [];
SharedPreferences prefs = await SharedPreferences.getInstance();

View File

@ -109,6 +109,8 @@ class InstallerViewModel extends BaseViewModel {
void installResult() async {
PatchedApplication? selectedApp =
locator<AppSelectorViewModel>().selectedApp;
List<Patch> selectedPatches =
locator<PatchesSelectorViewModel>().selectedPatches;
if (selectedApp != null) {
updateLog(selectedApp.isRooted
? 'Installing patched file using root method'
@ -116,6 +118,8 @@ class InstallerViewModel extends BaseViewModel {
isInstalled = await locator<PatcherAPI>().installPatchedFile(selectedApp);
if (isInstalled) {
updateLog('Done');
selectedApp.appliedPatches
.addAll(selectedPatches.map((p) => p.name).toList());
await saveApp(selectedApp);
} else {
updateLog('An error occurred! Aborting');
@ -153,9 +157,8 @@ class InstallerViewModel extends BaseViewModel {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> patchedApps = prefs.getStringList('patchedApps') ?? [];
String app = json.encode(selectedApp.toJson());
if (!patchedApps.contains(app)) {
patchedApps.add(app);
prefs.setStringList('patchedApps', patchedApps);
}
patchedApps.remove(app);
patchedApps.add(app);
prefs.setStringList('patchedApps', patchedApps);
}
}

View File

@ -22,6 +22,7 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
Widget build(BuildContext context) {
return ViewModelBuilder<PatchesSelectorViewModel>.reactive(
disposeViewModel: false,
fireOnModelReadyOnce: true,
onModelReady: (model) => model.initialize(),
viewModelBuilder: () => locator<PatchesSelectorViewModel>(),
builder: (context, model, child) => Scaffold(
@ -29,7 +30,7 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
child: model.patches != null && model.patches!.isNotEmpty
child: model.patches.isNotEmpty
? Column(
children: [
SearchBar(
@ -84,16 +85,16 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
patches.clear();
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
itemCount: model.patches.length,
itemBuilder: (context, index) {
model.patches!.sort((a, b) => a.simpleName.compareTo(b.simpleName));
model.patches.sort((a, b) => a.simpleName.compareTo(b.simpleName));
PatchItem item = PatchItem(
name: model.patches![index].name,
simpleName: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
name: model.patches[index].name,
simpleName: model.patches[index].simpleName,
version: model.patches[index].version,
description: model.patches[index].description,
isSelected: model.selectedPatches.any(
(element) => element.name == model.patches![index].name,
(element) => element.name == model.patches[index].name,
),
);
patches.add(item);
@ -107,19 +108,19 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
patches.clear();
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
itemCount: model.patches.length,
itemBuilder: (context, index) {
model.patches!.sort((a, b) => a.simpleName.compareTo(b.simpleName));
if (model.patches![index].simpleName.toLowerCase().contains(
model.patches.sort((a, b) => a.simpleName.compareTo(b.simpleName));
if (model.patches[index].simpleName.toLowerCase().contains(
query.toLowerCase(),
)) {
PatchItem item = PatchItem(
name: model.patches![index].name,
simpleName: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
name: model.patches[index].name,
simpleName: model.patches[index].simpleName,
version: model.patches[index].version,
description: model.patches[index].description,
isSelected: model.selectedPatches.any(
(element) => element.name == model.patches![index].name,
(element) => element.name == model.patches[index].name,
),
);
patches.add(item);

View File

@ -9,7 +9,7 @@ import 'package:stacked/stacked.dart';
class PatchesSelectorViewModel extends BaseViewModel {
final PatcherAPI patcherAPI = locator<PatcherAPI>();
List<Patch>? patches = [];
List<Patch> patches = [];
List<Patch> selectedPatches = [];
Future<void> initialize() async {
@ -24,14 +24,12 @@ class PatchesSelectorViewModel extends BaseViewModel {
void selectPatches(List<PatchItem> patchItems) {
selectedPatches.clear();
if (patches != null) {
for (PatchItem item in patchItems) {
if (item.isSelected) {
Patch patch =
patches!.firstWhere((element) => element.name == item.name);
if (!selectedPatches.contains(patch)) {
selectedPatches.add(patch);
}
for (PatchItem item in patchItems) {
if (item.isSelected) {
Patch patch =
patches.firstWhere((element) => element.name == item.name);
if (!selectedPatches.contains(patch)) {
selectedPatches.add(patch);
}
}
}

View File

@ -39,7 +39,10 @@ class AvailableUpdatesCard extends StatelessWidget {
patchDate: snapshot.data![index].patchDate,
changelog: snapshot2.data!,
isUpdatableApp: true,
onPressed: () => {},
onPressed: () =>
locator<HomeViewModel>().navigateToInstaller(
snapshot.data![index],
),
),
),
)

View File

@ -39,7 +39,10 @@ class InstalledAppsCard extends StatelessWidget {
patchDate: snapshot.data![index].patchDate,
changelog: snapshot2.data!,
isUpdatableApp: false,
onPressed: () => {},
onPressed: () =>
locator<HomeViewModel>().navigateToInstaller(
snapshot.data![index],
),
),
),
)