Skip to content

Commit

Permalink
TW-1793: recent chats are not recent
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed May 27, 2024
1 parent 3d348cd commit 0a357c8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 49 deletions.
6 changes: 3 additions & 3 deletions lib/domain/app_state/search/pre_search_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import 'package:fluffychat/app_state/success.dart';
import 'package:matrix/matrix.dart';

class PreSearchRecentContactsSuccess extends Success {
final List<User> users;
final List<Room> rooms;

const PreSearchRecentContactsSuccess({required this.users});
const PreSearchRecentContactsSuccess({required this.rooms});

@override
List<Object?> get props => [users];
List<Object?> get props => [rooms];
}

class PreSearchRecentContactsFailed extends Failure {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,43 +10,19 @@ class PreSearchRecentContactsInteractor {
PreSearchRecentContactsInteractor();

Stream<Either<Failure, Success>> execute({
required List<Room> recentRooms,
int? limit,
required List<Room> allRooms,
int limit = 5,
}) async* {
try {
final List<User> 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));
}
Expand Down
28 changes: 17 additions & 11 deletions lib/pages/search/recent_contacts_banner_widget.dart
Original file line number Diff line number Diff line change
@@ -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<User> contactsList;
final List<Room> 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(
Expand All @@ -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,
);
},
Expand All @@ -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,
Expand All @@ -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,
),
),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/search/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SearchController extends State<Search> {
void fetchPreSearchRecentContacts() {
_preSearchRecentContactsInteractor
.execute(
recentRooms: Matrix.of(context).client.rooms,
allRooms: Matrix.of(context).client.rooms,
limit: limitPrefetchedRecentContacts,
)
.listen((value) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/search/search_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 0a357c8

Please sign in to comment.