fix: improve patches selector.

This commit is contained in:
Alberto Ponces 2022-08-09 02:30:12 +01:00
parent 6338eb5dd1
commit 84923127e2
9 changed files with 95 additions and 26 deletions

View File

@ -37,7 +37,9 @@
},
"patchSelectorCard": {
"widgetTitle": "Select patches",
"widgetSubtitle": "Select an application first."
"widgetFirstSubtitle": "Select an application first.",
"widgetSecondSubtitle": "No patches selected.",
"widgetThirdSubtitle": "{selected} patch(es) selected."
},
"appSelectorView": {
"searchBarHint": "Search applications"

View File

@ -5,7 +5,6 @@ import 'package:path_provider/path_provider.dart' as p;
import 'package:revanced_manager/constants.dart';
import 'package:revanced_manager/services/github_api.dart';
// use path_provider to get the path of the storage directory
@lazySingleton
class ManagerAPI {
Dio dio = Dio();

View File

@ -21,6 +21,7 @@ class AppSelectorViewModel extends BaseViewModel {
void selectApp(AppInfo appInfo) {
locator<AppSelectorViewModel>().selectedApp = appInfo;
locator<PatcherViewModel>().dimPatchCard = false;
locator<PatcherViewModel>().notifyListeners();
}
}

View File

@ -17,12 +17,15 @@ class PatcherView extends StatelessWidget {
disposeViewModel: false,
viewModelBuilder: () => locator<PatcherViewModel>(),
builder: (context, model, child) => Scaffold(
floatingActionButton: FloatingActionButton.extended(
onPressed: () => {},
label: I18nText('patcherView.fabButton'),
icon: const Icon(Icons.build),
backgroundColor: const Color(0xff7792BA),
foregroundColor: Colors.white,
floatingActionButton: Visibility(
visible: locator<PatcherViewModel>().hideFabButton,
child: FloatingActionButton.extended(
onPressed: () => {},
label: I18nText('patcherView.fabButton'),
icon: const Icon(Icons.build),
backgroundColor: const Color(0xff7792BA),
foregroundColor: Colors.white,
),
),
body: SafeArea(
child: Padding(
@ -46,8 +49,11 @@ class PatcherView extends StatelessWidget {
onPressed: model.navigateToAppSelector,
),
const SizedBox(height: 16),
PatchSelectorCard(
onPressed: model.navigateToPatchesSelector,
Opacity(
opacity: model.dimPatchCard ? 0.5 : 1,
child: PatchSelectorCard(
onPressed: model.navigateToPatchesSelector,
),
),
],
),

View File

@ -5,6 +5,8 @@ import 'package:stacked_services/stacked_services.dart';
class PatcherViewModel extends BaseViewModel {
final _navigationService = locator<NavigationService>();
bool dimPatchCard = true;
bool hideFabButton = true;
void navigateToAppSelector() {
_navigationService.navigateTo(Routes.appSelectorView);

View File

@ -14,6 +14,7 @@ class PatchesSelectorView extends StatefulWidget {
}
class _PatchesSelectorViewState extends State<PatchesSelectorView> {
final List<PatchItem> patches = [];
String query = '';
@override
@ -24,7 +25,10 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
viewModelBuilder: () => locator<PatchesSelectorViewModel>(),
builder: (context, model, child) => Scaffold(
floatingActionButton: FloatingActionButton.extended(
onPressed: () => {},
onPressed: () {
model.selectPatches(patches);
Navigator.of(context).pop();
},
label: I18nText('patchesSelectorView.fabButton'),
icon: const Icon(Icons.check),
backgroundColor: const Color(0xff7792BA),
@ -66,23 +70,30 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
}
Widget _getAllResults(PatchesSelectorViewModel model) {
patches.clear();
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
itemBuilder: (context, index) {
model.patches!.sort((a, b) => a.simpleName.compareTo(b.simpleName));
return PatchItem(
name: model.patches![index].simpleName,
PatchItem item = PatchItem(
name: model.patches![index].name,
simpleName: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
isSelected: false,
isSelected: model.selectedPatches.any(
(element) => element.name == model.patches![index].name,
),
);
patches.add(item);
return item;
},
),
);
}
Widget _getFilteredResults(PatchesSelectorViewModel model) {
patches.clear();
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
@ -91,12 +102,17 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
if (model.patches![index].simpleName.toLowerCase().contains(
query.toLowerCase(),
)) {
return PatchItem(
name: model.patches![index].simpleName,
PatchItem item = PatchItem(
name: model.patches![index].name,
simpleName: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
isSelected: false,
isSelected: model.selectedPatches.any(
(element) => element.name == model.patches![index].name,
),
);
patches.add(item);
return item;
} else {
return const SizedBox();
}

View File

@ -3,6 +3,8 @@ import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patch.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/widgets/patch_item.dart';
import 'package:stacked/stacked.dart';
class PatchesSelectorViewModel extends BaseViewModel {
@ -20,5 +22,19 @@ class PatchesSelectorViewModel extends BaseViewModel {
patches = await patcherAPI.getFilteredPatches(appInfo);
}
void selectPatches(List<Patch> patches) {}
void selectPatches(List<PatchItem> patchItems) {
selectedPatches.clear();
if (patches != null) {
for (PatchItem patch in patchItems) {
if (patch.isSelected) {
selectedPatches.add(
patches!.firstWhere((element) => element.name == patch.name),
);
}
}
}
locator<PatcherViewModel>().hideFabButton =
selectedPatches.isEmpty ? true : false;
locator<PatcherViewModel>().notifyListeners();
}
}

View File

@ -4,6 +4,7 @@ import 'package:google_fonts/google_fonts.dart';
// ignore: must_be_immutable
class PatchItem extends StatefulWidget {
final String name;
final String simpleName;
final String description;
final String version;
bool isSelected;
@ -11,6 +12,7 @@ class PatchItem extends StatefulWidget {
PatchItem({
Key? key,
required this.name,
required this.simpleName,
required this.description,
required this.version,
required this.isSelected,
@ -43,7 +45,7 @@ class _PatchItemState extends State<PatchItem> {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
widget.name,
widget.simpleName,
style: GoogleFonts.inter(
fontSize: 16,
fontWeight: FontWeight.w600,

View File

@ -1,7 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/constants.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';
class PatchSelectorCard extends StatelessWidget {
final Function()? onPressed;
@ -35,13 +38,35 @@ class PatchSelectorCard extends StatelessWidget {
),
),
const SizedBox(height: 10),
I18nText(
'patchSelectorCard.widgetSubtitle',
child: Text(
'',
style: robotoTextStyle,
),
),
locator<AppSelectorViewModel>().selectedApp == null
? I18nText(
'patchSelectorCard.widgetFirstSubtitle',
child: Text(
'',
style: robotoTextStyle,
),
)
: locator<PatchesSelectorViewModel>().selectedPatches.isEmpty
? I18nText(
'patchSelectorCard.widgetSecondSubtitle',
child: Text(
'',
style: robotoTextStyle,
),
)
: I18nText(
'patchSelectorCard.widgetThirdSubtitle',
translationParams: {
'selected': locator<PatchesSelectorViewModel>()
.selectedPatches
.length
.toString()
},
child: Text(
'',
style: robotoTextStyle,
),
),
],
),
),