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> { ...@@ -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); final f = simple(event.query);
_filteredList = _users if (f.isEmpty) {
.where((User user) => simple(user.toMap.values.toString()).contains(f)) _filteredList = _users;
.toList() } else {
..sort((User a, User b) => f == a.name ? -1 : 0); _filteredList =
_users.where((User user) => simple(user.toMap.values.toString()).contains(f)).toList()
emit(UsersState.result(_filteredList)); ..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)); emit(UsersState.result(_filteredList));
}); });
} }
...@@ -149,9 +143,7 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> { ...@@ -149,9 +143,7 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
void onCreateUser(String name, String password, {int card, int pin}) => void onCreateUser(String name, String password, {int card, int pin}) =>
add(CreateUser(name, password, card: card, pin: pin)); add(CreateUser(name, password, card: card, pin: pin));
void onFilter(String filter) => add(FilterAll(filter)); void onFilter(String query) => add(Filter(query));
void onFilterByUser(String query) => add(FilterUsers(query));
void onGetCredit(int userId) => add(GetCredit(userId)); void onGetCredit(int userId) => add(GetCredit(userId));
......
...@@ -21,26 +21,15 @@ class CreateUser extends UsersEvent { ...@@ -21,26 +21,15 @@ class CreateUser extends UsersEvent {
String toString() => '[CreateUser ${removePassword(toMap)}]'; String toString() => '[CreateUser ${removePassword(toMap)}]';
} }
class FilterAll extends UsersEvent { class Filter extends UsersEvent {
final String filter;
FilterAll(this.filter);
Map<String, dynamic> get toMap => {'filter': filter};
@override
String toString() => '[FilterAll $toMap]';
}
class FilterUsers extends UsersEvent {
final String query; final String query;
FilterUsers(this.query); Filter(this.query);
Map<String, dynamic> get toMap => {'query': query}; Map<String, dynamic> get toMap => {'query': query};
@override @override
String toString() => '[FilterUsers $toMap]'; String toString() => '[Filter $toMap]';
} }
class GetCredit extends UsersEvent { 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