Commit 8d08af30 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

actually refactor whoel filter affair

parent a342a339
......@@ -119,23 +119,17 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
}
});
on<FilterUsers>((FilterUsers event, Emitter<UsersState> emit) async {
on<Filter>((Filter event, Emitter<UsersState> emit) async {
final f = simple(event.query);
_filteredList = _users
.where((User user) => simple(user.toMap.values.toString()).contains(f))
.toList()
..sort((User a, User b) => f == a.name ? -1 : 0);
emit(UsersState.result(_filteredList));
});
if (f.isEmpty) {
_filteredList = _users;
} else {
_filteredList =
_users.where((User user) => simple(user.toMap.values.toString()).contains(f)).toList()
..sort((User a, User b) => f == a.name ? -1 : 0)
..sort((User a, User b) => f == a.email ? -1 : 0);
}
on<FilterAll>((FilterAll event, Emitter<UsersState> emit) async {
final f = event.filter.toLowerCase();
_filteredList = _users
.where((User user) =>
user.name.toLowerCase().contains(f) ||
(user.card?.toString() ?? '').toLowerCase().contains(f))
.toList();
emit(UsersState.result(_filteredList));
});
}
......@@ -149,9 +143,7 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
void onCreateUser(String name, String password, {int card, int pin}) =>
add(CreateUser(name, password, card: card, pin: pin));
void onFilter(String filter) => add(FilterAll(filter));
void onFilterByUser(String query) => add(FilterUsers(query));
void onFilter(String query) => add(Filter(query));
void onGetCredit(int userId) => add(GetCredit(userId));
......
......@@ -21,26 +21,15 @@ class CreateUser extends UsersEvent {
String toString() => '[CreateUser ${removePassword(toMap)}]';
}
class FilterAll extends UsersEvent {
final String filter;
FilterAll(this.filter);
Map<String, dynamic> get toMap => {'filter': filter};
@override
String toString() => '[FilterAll $toMap]';
}
class FilterUsers extends UsersEvent {
class Filter extends UsersEvent {
final String query;
FilterUsers(this.query);
Filter(this.query);
Map<String, dynamic> get toMap => {'query': query};
@override
String toString() => '[FilterUsers $toMap]';
String toString() => '[Filter $toMap]';
}
class GetCredit extends UsersEvent {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment