Skip to content

Commit

Permalink
add sorting and emit when not closed
Browse files Browse the repository at this point in the history
  • Loading branch information
LGro committed Apr 10, 2024
1 parent 13be1f6 commit 1bd5caf
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/ui/contact_list/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import 'package:equatable/equatable.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:collection/collection.dart';

import '../../data/models/coag_contact.dart';
import '../../data/repositories/contacts.dart';
Expand All @@ -15,31 +16,37 @@ part 'state.dart';
class ContactListCubit extends HydratedCubit<ContactListState> {
ContactListCubit(this.contactsRepository)
: super(const ContactListState(ContactListStatus.initial)) {
// TODO: Is there an emit.forEach in Cubits like with Blocs?
contactsRepository.getUpdateStatus().listen((event) {
// TODO: Is there something smarter than always replacing the full state?
emit(ContactListState(ContactListStatus.success,
contacts: contactsRepository.coagContacts.values));
if (!isClosed) {
filter('');
}
});

emit(ContactListState(ContactListStatus.success,
contacts: contactsRepository.coagContacts.values));
if (!isClosed) {
filter('');
}
}

final ContactsRepository contactsRepository;

// TODO: Refine filtering by only searching through all values (not like now, also the field names)
void filter(String filter) => emit(ContactListState(ContactListStatus.success,
contacts: contactsRepository.coagContacts.values.where((c) =>
(c.details != null &&
c.details!
.toString()
.toLowerCase()
.contains(filter.toLowerCase())) ||
(c.systemContact != null &&
c.systemContact!
.toString()
.toLowerCase()
.contains(filter.toLowerCase())))));
contacts: contactsRepository.coagContacts.values
.where((c) =>
(c.details != null &&
c.details!
.toString()
.toLowerCase()
.contains(filter.toLowerCase())) ||
(c.systemContact != null &&
c.systemContact!
.toString()
.toLowerCase()
.contains(filter.toLowerCase())))
.toList()
..sort((a, b) =>
compareNatural(a.details!.displayName, b.details!.displayName))));

@override
ContactListState fromJson(Map<String, dynamic> json) =>
Expand Down

0 comments on commit 1bd5caf

Please sign in to comment.