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

124 lines
4.3 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/app/app.locator.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-09 03:30:12 +02:00
final List<PatchItem> patches = [];
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(
disposeViewModel: false,
onModelReady: (model) => model.initialise(),
viewModelBuilder: () => locator<PatchesSelectorViewModel>(),
2022-08-09 01:01:06 +02:00
builder: (context, model, child) => Scaffold(
floatingActionButton: FloatingActionButton.extended(
2022-08-09 03:30:12 +02:00
onPressed: () {
model.selectPatches(patches);
Navigator.of(context).pop();
},
label: I18nText('patchesSelectorView.fabButton'),
icon: const Icon(Icons.check),
backgroundColor: const Color(0xff7792BA),
foregroundColor: Colors.white,
),
body: SafeArea(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
child: model.patches != null && model.patches!.isNotEmpty
? Column(
children: [
SearchBar(
hintText: FlutterI18n.translate(
context,
'patchesSelectorView.searchBarHint',
),
onQueryChanged: (searchQuery) {
setState(() {
query = searchQuery;
});
2022-08-07 21:15:52 +02:00
},
),
const SizedBox(height: 12),
query.isEmpty || query.length < 2
? _getAllResults(model)
: _getFilteredResults(model)
],
)
: const Center(
child: CircularProgressIndicator(
color: Color(0xff7792BA),
),
),
2022-08-07 21:15:52 +02:00
),
),
),
);
}
Widget _getAllResults(PatchesSelectorViewModel model) {
2022-08-09 03:30:12 +02:00
patches.clear();
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
itemBuilder: (context, index) {
model.patches!.sort((a, b) => a.simpleName.compareTo(b.simpleName));
2022-08-09 03:30:12 +02:00
PatchItem item = PatchItem(
name: model.patches![index].name,
simpleName: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
2022-08-09 03:30:12 +02:00
isSelected: model.selectedPatches.any(
(element) => element.name == model.patches![index].name,
),
);
2022-08-09 03:30:12 +02:00
patches.add(item);
return item;
},
),
);
}
Widget _getFilteredResults(PatchesSelectorViewModel model) {
2022-08-09 03:30:12 +02:00
patches.clear();
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
itemBuilder: (context, index) {
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(
name: model.patches![index].name,
simpleName: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
2022-08-09 03:30:12 +02:00
isSelected: model.selectedPatches.any(
(element) => element.name == model.patches![index].name,
),
);
2022-08-09 03:30:12 +02:00
patches.add(item);
return item;
} else {
return const SizedBox();
}
},
),
);
}
2022-08-07 21:15:52 +02:00
}