2022-08-09 01:01:06 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2022-08-07 21:15:52 +02:00
|
|
|
import 'package:revanced_manager/models/patch.dart';
|
|
|
|
import 'package:revanced_manager/services/patcher_api.dart';
|
2022-08-09 03:30:12 +02:00
|
|
|
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
|
2022-08-07 21:15:52 +02:00
|
|
|
import 'package:stacked/stacked.dart';
|
|
|
|
|
|
|
|
class PatchesSelectorViewModel extends BaseViewModel {
|
2022-08-18 16:33:33 +02:00
|
|
|
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
|
|
|
|
final List<Patch> patches = [];
|
|
|
|
final List<Patch> selectedPatches =
|
|
|
|
locator<PatcherViewModel>().selectedPatches;
|
2022-08-07 21:15:52 +02:00
|
|
|
|
2022-08-13 11:56:30 +02:00
|
|
|
Future<void> initialize() async {
|
2022-08-18 16:33:33 +02:00
|
|
|
patches.addAll(await _patcherAPI.getFilteredPatches(
|
|
|
|
locator<PatcherViewModel>().selectedApp,
|
|
|
|
));
|
2022-08-24 17:07:40 +02:00
|
|
|
patches.sort((a, b) => a.simpleName.compareTo(b.simpleName));
|
2022-08-21 03:58:40 +02:00
|
|
|
for (Patch p in patches) {
|
|
|
|
if (p.include) {
|
|
|
|
selectedPatches.add(p);
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 02:16:33 +02:00
|
|
|
notifyListeners();
|
2022-08-07 21:15:52 +02:00
|
|
|
}
|
2022-08-09 01:01:06 +02:00
|
|
|
|
2022-08-24 17:07:40 +02:00
|
|
|
bool isSelected(Patch patch) {
|
2022-08-21 03:03:36 +02:00
|
|
|
return selectedPatches.any(
|
2022-08-24 17:07:40 +02:00
|
|
|
(element) => element.name == patch.name,
|
2022-08-21 03:03:36 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-24 17:07:40 +02:00
|
|
|
void selectPatch(Patch patch, bool isSelected) {
|
2022-08-18 16:33:33 +02:00
|
|
|
if (isSelected && !selectedPatches.contains(patch)) {
|
|
|
|
selectedPatches.add(patch);
|
2022-08-18 00:06:02 +02:00
|
|
|
} else {
|
2022-08-18 16:33:33 +02:00
|
|
|
selectedPatches.remove(patch);
|
2022-08-09 03:30:12 +02:00
|
|
|
}
|
2022-08-18 16:33:33 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-21 03:03:36 +02:00
|
|
|
void selectAllPatches(bool isSelected) {
|
|
|
|
selectedPatches.clear();
|
|
|
|
if (isSelected) {
|
|
|
|
selectedPatches.addAll(patches);
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:33:33 +02:00
|
|
|
void selectPatches() {
|
|
|
|
locator<PatcherViewModel>().selectedPatches = selectedPatches;
|
2022-08-09 03:30:12 +02:00
|
|
|
locator<PatcherViewModel>().notifyListeners();
|
|
|
|
}
|
2022-08-24 17:07:40 +02:00
|
|
|
|
2022-08-25 11:28:40 +02:00
|
|
|
List<Patch> getQueriedPatches(String query) {
|
2022-08-24 17:07:40 +02:00
|
|
|
return patches
|
|
|
|
.where((patch) =>
|
|
|
|
query.isEmpty ||
|
|
|
|
query.length < 2 ||
|
|
|
|
patch.simpleName.toLowerCase().contains(
|
|
|
|
query.toLowerCase(),
|
|
|
|
))
|
|
|
|
.toList();
|
|
|
|
}
|
2022-08-09 01:01:06 +02:00
|
|
|
}
|