Added category filter

This commit is contained in:
Imran Remtulla
2022-12-21 04:15:39 -05:00
parent 26f953dbb0
commit 118460ccb9
3 changed files with 46 additions and 36 deletions

View File

@@ -177,20 +177,8 @@ class _AppPageState extends State<AppPage> {
title: 'Pick a Category', title: 'Pick a Category',
items: [ items: [
[ [
GeneratedFormItem( settingsProvider
'category', // TODO .getCategoryFormItem()
label: 'Category',
opts: [
MapEntry('',
'No Category'),
...categories
.entries
.map((e) =>
MapEntry(
e.key,
e.key))
.toList()
])
] ]
]); ]);
}).then((value) { }).then((value) {

View File

@@ -80,28 +80,31 @@ class AppsPageState extends State<AppsPage> {
!(filter!.includeNonInstalled)) { !(filter!.includeNonInstalled)) {
return false; return false;
} }
if (filter!.nameFilter.isEmpty && filter!.authorFilter.isEmpty) { if (filter!.nameFilter.isNotEmpty || filter!.authorFilter.isNotEmpty) {
return true; List<String> nameTokens = filter!.nameFilter
} .split(' ')
List<String> nameTokens = filter!.nameFilter .where((element) => element.trim().isNotEmpty)
.split(' ') .toList();
.where((element) => element.trim().isNotEmpty) List<String> authorTokens = filter!.authorFilter
.toList(); .split(' ')
List<String> authorTokens = filter!.authorFilter .where((element) => element.trim().isNotEmpty)
.split(' ') .toList();
.where((element) => element.trim().isNotEmpty)
.toList();
for (var t in nameTokens) { for (var t in nameTokens) {
var name = app.installedInfo?.name ?? app.app.name; var name = app.installedInfo?.name ?? app.app.name;
if (!name.toLowerCase().contains(t.toLowerCase())) { if (!name.toLowerCase().contains(t.toLowerCase())) {
return false; return false;
}
}
for (var t in authorTokens) {
if (!app.app.author.toLowerCase().contains(t.toLowerCase())) {
return false;
}
} }
} }
for (var t in authorTokens) { if (filter!.categoryFilter.isNotEmpty &&
if (!app.app.author.toLowerCase().contains(t.toLowerCase())) { filter!.categoryFilter != app.app.category) {
return false; return false;
}
} }
return true; return true;
}).toList(); }).toList();
@@ -726,6 +729,10 @@ class AppsPageState extends State<AppsPage> {
label: tr('nonInstalledApps'), label: tr('nonInstalledApps'),
type: FormItemType.bool, type: FormItemType.bool,
defaultValue: vals['nonInstalledApps']) defaultValue: vals['nonInstalledApps'])
], // TODO
[
settingsProvider.getCategoryFormItem(
initCategory: vals['category'] ?? '')
] ]
]); ]);
}).then((values) { }).then((values) {
@@ -752,19 +759,22 @@ class AppsFilter {
late String authorFilter; late String authorFilter;
late bool includeUptodate; late bool includeUptodate;
late bool includeNonInstalled; late bool includeNonInstalled;
late String categoryFilter;
AppsFilter( AppsFilter(
{this.nameFilter = '', {this.nameFilter = '',
this.authorFilter = '', this.authorFilter = '',
this.includeUptodate = true, this.includeUptodate = true,
this.includeNonInstalled = true}); this.includeNonInstalled = true,
this.categoryFilter = ''});
Map<String, String> toValuesMap() { Map<String, String> toValuesMap() {
return { return {
'appName': nameFilter, 'appName': nameFilter,
'author': authorFilter, 'author': authorFilter,
'upToDateApps': includeUptodate ? 'true' : '', 'upToDateApps': includeUptodate ? 'true' : '',
'nonInstalledApps': includeNonInstalled ? 'true' : '' 'nonInstalledApps': includeNonInstalled ? 'true' : '',
'category': categoryFilter
}; };
} }
@@ -773,11 +783,13 @@ class AppsFilter {
authorFilter = values['author']!; authorFilter = values['author']!;
includeUptodate = values['upToDateApps'] == 'true'; includeUptodate = values['upToDateApps'] == 'true';
includeNonInstalled = values['nonInstalledApps'] == 'true'; includeNonInstalled = values['nonInstalledApps'] == 'true';
categoryFilter = values['category']!;
} }
bool isIdenticalTo(AppsFilter other) => bool isIdenticalTo(AppsFilter other) =>
authorFilter.trim() == other.authorFilter.trim() && authorFilter.trim() == other.authorFilter.trim() &&
nameFilter.trim() == other.nameFilter.trim() && nameFilter.trim() == other.nameFilter.trim() &&
includeUptodate == other.includeUptodate && includeUptodate == other.includeUptodate &&
includeNonInstalled == other.includeNonInstalled; includeNonInstalled == other.includeNonInstalled &&
categoryFilter.trim() == other.categoryFilter.trim();
} }

View File

@@ -6,6 +6,7 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; import 'package:fluttertoast/fluttertoast.dart';
import 'package:obtainium/app_sources/github.dart'; import 'package:obtainium/app_sources/github.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@@ -153,4 +154,13 @@ class SettingsProvider with ChangeNotifier {
set categories(Map<String, int> cats) { set categories(Map<String, int> cats) {
prefs?.setString('categories', jsonEncode(cats)); prefs?.setString('categories', jsonEncode(cats));
} }
getCategoryFormItem({String initCategory = ''}) =>
GeneratedFormItem('category', // TODO
label: 'Category',
opts: [
const MapEntry('', 'No Category'),
...categories.entries.map((e) => MapEntry(e.key, e.key)).toList()
],
defaultValue: initCategory);
} }