revanced-manager/lib/ui/views/patches_selector/patches_selector_view.dart

133 lines
4.9 KiB
Dart
Raw Normal View History

2022-08-07 21:15:52 +02:00
import 'package:flutter/material.dart';
2022-08-08 01:31:55 +02:00
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:revanced_manager/theme.dart';
2022-08-07 21:15:52 +02:00
import 'package:revanced_manager/ui/views/patches_selector/patches_selector_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/patch_item.dart';
import 'package:revanced_manager/ui/widgets/search_bar.dart';
import 'package:stacked/stacked.dart';
class PatchesSelectorView extends StatefulWidget {
const PatchesSelectorView({Key? key}) : super(key: key);
@override
State<PatchesSelectorView> createState() => _PatchesSelectorViewState();
}
class _PatchesSelectorViewState extends State<PatchesSelectorView> {
2022-08-18 16:33:33 +02:00
final List<PatchItem> _items = [];
2022-08-07 21:15:52 +02:00
String query = '';
@override
Widget build(BuildContext context) {
2022-08-09 01:01:06 +02:00
return ViewModelBuilder<PatchesSelectorViewModel>.reactive(
onModelReady: (model) => model.initialize(),
2022-08-18 16:33:33 +02:00
viewModelBuilder: () => PatchesSelectorViewModel(),
2022-08-09 01:01:06 +02:00
builder: (context, model, child) => Scaffold(
body: SafeArea(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
2022-08-17 19:44:27 +02:00
child: model.patches.isNotEmpty
? Column(
children: [
SearchBar(
showSelectIcon: true,
2022-08-11 22:17:10 +02:00
fillColor:
isDark ? const Color(0xff1B222B) : Colors.grey[200],
hintText: FlutterI18n.translate(
context,
'patchesSelectorView.searchBarHint',
),
hintTextColor: Theme.of(context).colorScheme.tertiary,
onQueryChanged: (searchQuery) {
setState(() {
query = searchQuery;
});
2022-08-07 21:15:52 +02:00
},
onSelectAll: (value) => model.selectAllPatches(value),
),
const SizedBox(height: 12),
query.isEmpty || query.length < 2
? _getAllResults(model)
: _getFilteredResults(model),
MaterialButton(
textColor: Colors.white,
color: Theme.of(context).colorScheme.secondary,
minWidth: double.infinity,
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 8,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
2022-08-18 16:33:33 +02:00
onPressed: () {
model.selectPatches();
Navigator.of(context).pop();
},
child: I18nText('patchesSelectorView.fabButton'),
),
],
)
: Center(
child: CircularProgressIndicator(
color: Theme.of(context).colorScheme.secondary,
),
),
2022-08-07 21:15:52 +02:00
),
),
),
);
}
Widget _getAllResults(PatchesSelectorViewModel model) {
2022-08-18 16:33:33 +02:00
_items.clear();
return Expanded(
child: ListView.builder(
2022-08-17 19:44:27 +02:00
itemCount: model.patches.length,
itemBuilder: (context, index) {
2022-08-17 19:44:27 +02:00
model.patches.sort((a, b) => a.simpleName.compareTo(b.simpleName));
2022-08-09 03:30:12 +02:00
PatchItem item = PatchItem(
2022-08-17 19:44:27 +02:00
name: model.patches[index].name,
simpleName: model.patches[index].simpleName,
version: model.patches[index].version,
description: model.patches[index].description,
isSelected: model.isSelected(index),
onChanged: (value) => model.selectPatch(index, value),
);
2022-08-18 16:33:33 +02:00
_items.add(item);
2022-08-09 03:30:12 +02:00
return item;
},
),
);
}
Widget _getFilteredResults(PatchesSelectorViewModel model) {
2022-08-18 16:33:33 +02:00
_items.clear();
return Expanded(
child: ListView.builder(
2022-08-17 19:44:27 +02:00
itemCount: model.patches.length,
itemBuilder: (context, index) {
2022-08-17 19:44:27 +02:00
model.patches.sort((a, b) => a.simpleName.compareTo(b.simpleName));
if (model.patches[index].simpleName.toLowerCase().contains(
query.toLowerCase(),
)) {
2022-08-09 03:30:12 +02:00
PatchItem item = PatchItem(
2022-08-17 19:44:27 +02:00
name: model.patches[index].name,
simpleName: model.patches[index].simpleName,
version: model.patches[index].version,
description: model.patches[index].description,
isSelected: model.isSelected(index),
onChanged: (value) => model.selectPatch(index, value),
);
2022-08-18 16:33:33 +02:00
_items.add(item);
2022-08-09 03:30:12 +02:00
return item;
} else {
return const SizedBox();
}
},
),
);
}
2022-08-07 21:15:52 +02:00
}