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';
|
2022-08-09 02:16:33 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2022-08-12 11:42:43 +02:00
|
|
|
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-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(
|
2022-08-09 02:16:33 +02:00
|
|
|
disposeViewModel: false,
|
2022-08-13 11:56:30 +02:00
|
|
|
onModelReady: (model) => model.initialize(),
|
2022-08-09 02:16:33 +02:00
|
|
|
viewModelBuilder: () => locator<PatchesSelectorViewModel>(),
|
2022-08-09 01:01:06 +02:00
|
|
|
builder: (context, model, child) => Scaffold(
|
2022-08-09 02:16:33 +02:00
|
|
|
body: SafeArea(
|
|
|
|
child: Padding(
|
|
|
|
padding:
|
|
|
|
const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
|
|
|
|
child: model.patches != null && model.patches!.isNotEmpty
|
|
|
|
? Column(
|
|
|
|
children: [
|
|
|
|
SearchBar(
|
2022-08-11 22:17:10 +02:00
|
|
|
fillColor:
|
2022-08-12 11:42:43 +02:00
|
|
|
isDark ? const Color(0xff1B222B) : Colors.grey[200],
|
2022-08-09 02:16:33 +02:00
|
|
|
hintText: FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'patchesSelectorView.searchBarHint',
|
|
|
|
),
|
2022-08-12 11:42:43 +02:00
|
|
|
hintTextColor: Theme.of(context).colorScheme.tertiary,
|
2022-08-09 02:16:33 +02:00
|
|
|
onQueryChanged: (searchQuery) {
|
|
|
|
setState(() {
|
|
|
|
query = searchQuery;
|
|
|
|
});
|
2022-08-07 21:15:52 +02:00
|
|
|
},
|
2022-08-09 02:16:33 +02:00
|
|
|
),
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
query.isEmpty || query.length < 2
|
|
|
|
? _getAllResults(model)
|
2022-08-10 10:42:24 +02:00
|
|
|
: _getFilteredResults(model),
|
|
|
|
MaterialButton(
|
|
|
|
textColor: Colors.white,
|
2022-08-13 11:56:30 +02:00
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
2022-08-10 10:42:24 +02:00
|
|
|
minWidth: double.infinity,
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: 12,
|
|
|
|
horizontal: 8,
|
|
|
|
),
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
model.selectPatches(patches);
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: I18nText('patchesSelectorView.fabButton'),
|
|
|
|
),
|
2022-08-09 02:16:33 +02:00
|
|
|
],
|
|
|
|
)
|
2022-08-12 11:42:43 +02:00
|
|
|
: Center(
|
2022-08-09 02:16:33 +02:00
|
|
|
child: CircularProgressIndicator(
|
2022-08-12 11:42:43 +02:00
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
2022-08-09 02:16:33 +02:00
|
|
|
),
|
|
|
|
),
|
2022-08-07 21:15:52 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-08-09 02:16:33 +02:00
|
|
|
|
|
|
|
Widget _getAllResults(PatchesSelectorViewModel model) {
|
2022-08-09 03:30:12 +02:00
|
|
|
patches.clear();
|
2022-08-09 02:16:33 +02:00
|
|
|
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,
|
2022-08-09 02:16:33 +02:00
|
|
|
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 02:16:33 +02:00
|
|
|
);
|
2022-08-09 03:30:12 +02:00
|
|
|
patches.add(item);
|
|
|
|
return item;
|
2022-08-09 02:16:33 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _getFilteredResults(PatchesSelectorViewModel model) {
|
2022-08-09 03:30:12 +02:00
|
|
|
patches.clear();
|
2022-08-09 02:16:33 +02:00
|
|
|
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,
|
2022-08-09 02:16:33 +02:00
|
|
|
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 02:16:33 +02:00
|
|
|
);
|
2022-08-09 03:30:12 +02:00
|
|
|
patches.add(item);
|
|
|
|
return item;
|
2022-08-09 02:16:33 +02:00
|
|
|
} else {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-08-07 21:15:52 +02:00
|
|
|
}
|