From 0a357c8dc0a310d20793ab1d137ed900db39869d Mon Sep 17 00:00:00 2001 From: --global Date: Mon, 27 May 2024 10:42:14 +0700 Subject: [PATCH] TW-1793: recent chats are not recent --- .../app_state/search/pre_search_state.dart | 6 +-- ...pre_search_recent_contacts_interactor.dart | 44 +++++-------------- .../search/recent_contacts_banner_widget.dart | 28 +++++++----- lib/pages/search/search.dart | 2 +- lib/pages/search/search_view.dart | 2 +- 5 files changed, 33 insertions(+), 49 deletions(-) diff --git a/lib/domain/app_state/search/pre_search_state.dart b/lib/domain/app_state/search/pre_search_state.dart index f4c77b46cd..4919916a82 100644 --- a/lib/domain/app_state/search/pre_search_state.dart +++ b/lib/domain/app_state/search/pre_search_state.dart @@ -3,12 +3,12 @@ import 'package:fluffychat/app_state/success.dart'; import 'package:matrix/matrix.dart'; class PreSearchRecentContactsSuccess extends Success { - final List users; + final List rooms; - const PreSearchRecentContactsSuccess({required this.users}); + const PreSearchRecentContactsSuccess({required this.rooms}); @override - List get props => [users]; + List get props => [rooms]; } class PreSearchRecentContactsFailed extends Failure { diff --git a/lib/domain/usecase/search/pre_search_recent_contacts_interactor.dart b/lib/domain/usecase/search/pre_search_recent_contacts_interactor.dart index 852c0c9b9d..d07e27a12c 100644 --- a/lib/domain/usecase/search/pre_search_recent_contacts_interactor.dart +++ b/lib/domain/usecase/search/pre_search_recent_contacts_interactor.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:dartz/dartz.dart'; import 'package:fluffychat/app_state/failure.dart'; import 'package:fluffychat/app_state/success.dart'; @@ -8,43 +10,19 @@ class PreSearchRecentContactsInteractor { PreSearchRecentContactsInteractor(); Stream> execute({ - required List recentRooms, - int? limit, + required List allRooms, + int limit = 5, }) async* { try { - final List result = []; - - for (final room in recentRooms) { - final users = room - .getParticipants() - .where( - (user) => - user.membership.isInvite == true && user.displayName != null, - ) - .toSet(); - - for (final user in users) { - final isDuplicateUser = - result.any((existingUser) => existingUser.id == user.id); - - if (!isDuplicateUser) { - result.add(user); - } - - if (result.length == limit) { - break; - } - } - - if (result.length == limit) { - break; - } - } - if (result.isEmpty) { + final directRooms = allRooms.where((room) => room.isDirectChat).toList(); + if (directRooms.isEmpty) { yield const Left(PreSearchRecentContactsEmpty()); - } else { - yield Right(PreSearchRecentContactsSuccess(users: result)); + return; } + final recentRooms = + directRooms.getRange(0, min(directRooms.length, limit)).toList(); + + yield Right(PreSearchRecentContactsSuccess(rooms: recentRooms)); } catch (e) { yield Left(PreSearchRecentContactsFailed(exception: e)); } diff --git a/lib/pages/search/recent_contacts_banner_widget.dart b/lib/pages/search/recent_contacts_banner_widget.dart index 744ca963ee..4ff75a701d 100644 --- a/lib/pages/search/recent_contacts_banner_widget.dart +++ b/lib/pages/search/recent_contacts_banner_widget.dart @@ -1,22 +1,25 @@ import 'package:fluffychat/pages/search/recent_contacts_banner_widget_style.dart'; import 'package:fluffychat/pages/search/search.dart'; import 'package:fluffychat/utils/display_name_widget.dart'; +import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart'; import 'package:fluffychat/widgets/avatar/avatar.dart'; import 'package:flutter/material.dart' hide SearchController; +import 'package:go_router/go_router.dart'; import 'package:matrix/matrix.dart'; +import 'package:flutter_gen/gen_l10n/l10n.dart'; class PreSearchRecentContactsContainer extends StatelessWidget { final SearchController searchController; - final List contactsList; + final List recentRooms; const PreSearchRecentContactsContainer({ super.key, required this.searchController, - required this.contactsList, + required this.recentRooms, }); @override Widget build(BuildContext context) { - if (contactsList.isEmpty) { + if (recentRooms.isEmpty) { return const SizedBox.shrink(); } else { return Align( @@ -27,10 +30,10 @@ class PreSearchRecentContactsContainer extends StatelessWidget { shrinkWrap: true, physics: const ClampingScrollPhysics(), scrollDirection: Axis.horizontal, - itemCount: contactsList.length, + itemCount: recentRooms.length, itemBuilder: (context, index) { return PreSearchRecentContactWidget( - user: contactsList[index], + room: recentRooms[index], searchController: searchController, ); }, @@ -42,18 +45,21 @@ class PreSearchRecentContactsContainer extends StatelessWidget { class PreSearchRecentContactWidget extends StatelessWidget { final SearchController searchController; - final User user; + final Room room; const PreSearchRecentContactWidget({ super.key, - required this.user, + required this.room, required this.searchController, }); @override Widget build(BuildContext context) { + final displayName = room.getLocalizedDisplayname( + MatrixLocals(L10n.of(context)!), + ); return InkWell( onTap: () { - searchController.goToChatScreenFormRecentChat(user); + context.go('/rooms/${room.id}'); }, child: SizedBox( width: RecentContactsBannerWidgetStyle.chatRecentContactItemWidth, @@ -64,15 +70,15 @@ class PreSearchRecentContactWidget extends StatelessWidget { width: RecentContactsBannerWidgetStyle.avatarWidthSize, height: RecentContactsBannerWidgetStyle.avatarWidthSize, child: Avatar( - mxContent: user.avatarUrl, - name: user.displayName ?? "", + mxContent: room.avatar, + name: displayName, ), ), Padding( padding: RecentContactsBannerWidgetStyle.chatRecentContactItemPadding, child: BuildDisplayName( - profileDisplayName: user.displayName ?? "", + profileDisplayName: displayName, ), ), ], diff --git a/lib/pages/search/search.dart b/lib/pages/search/search.dart index 963a16be99..999f40b556 100644 --- a/lib/pages/search/search.dart +++ b/lib/pages/search/search.dart @@ -81,7 +81,7 @@ class SearchController extends State { void fetchPreSearchRecentContacts() { _preSearchRecentContactsInteractor .execute( - recentRooms: Matrix.of(context).client.rooms, + allRooms: Matrix.of(context).client.rooms, limit: limitPrefetchedRecentContacts, ) .listen((value) { diff --git a/lib/pages/search/search_view.dart b/lib/pages/search/search_view.dart index 93630f2f3d..ec49ace267 100644 --- a/lib/pages/search/search_view.dart +++ b/lib/pages/search/search_view.dart @@ -49,7 +49,7 @@ class SearchView extends StatelessWidget { flexibleSpace: FlexibleSpaceBar( title: PreSearchRecentContactsContainer( searchController: searchController, - contactsList: data.users, + recentRooms: data.rooms, ), titlePadding: const EdgeInsetsDirectional.only(start: 0.0),