Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1793: recent chats are not recent #1800

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 17 additions & 16 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,11 +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],
searchController: searchController,
room: recentRooms[index],
);
},
),
Expand All @@ -41,20 +43,19 @@ 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.searchController,
required this.room,
});

@override
Widget build(BuildContext context) {
final displayName = room.getLocalizedDisplayname(
MatrixLocals(L10n.of(context)!),
);
return InkWell(
onTap: () {
searchController.goToChatScreenFormRecentChat(user);
},
onTap: () => context.go('/rooms/${room.id}'),
Te-Z marked this conversation as resolved.
Show resolved Hide resolved
child: SizedBox(
width: RecentContactsBannerWidgetStyle.chatRecentContactItemWidth,
child: Column(
Expand All @@ -64,15 +65,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
Loading