Skip to content

Commit

Permalink
Merge pull request #299 from gtalha07/issue287_chat-group-description
Browse files Browse the repository at this point in the history
Issue287 members avatars and names display in chat group description
  • Loading branch information
gtalha07 authored Nov 4, 2022
2 parents b13ed0c + ac8508f commit 30aa794
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 273 deletions.
71 changes: 53 additions & 18 deletions app/lib/controllers/chat_room_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:effektio_flutter_sdk/effektio_flutter_sdk_ffi.dart'
show
Client,
Conversation,
FfiBufferUint8,
FileDescription,
ImageDescription,
Member,
Expand Down Expand Up @@ -42,9 +43,12 @@ class ChatRoomController extends GetxController {
GlobalKey<FlutterMentionsState>();
bool isSendButtonVisible = false;
final List<XFile> _imageFileList = [];
List<Map<String, dynamic>> activeMembers = [];
List<Member> activeMembers = [];
Map<String, String> messageTextMapMarkDown = {};
Map<String, String> messageTextMapHtml = {};
final Map<String, Future<FfiBufferUint8>> _userAvatars = {};
final Map<String, String> _userNames = {};
List<Map<String, dynamic>> mentionList = [];
StreamSubscription<RoomMessage>? _messageSubscription;

ChatRoomController({required this.client}) : super();
Expand Down Expand Up @@ -91,13 +95,17 @@ class ChatRoomController extends GetxController {
messages.clear();
typingUsers.clear();
activeMembers.clear();
mentionList.clear();
_stream = null;
_page = 0;
_currentRoom = null;
} else {
_currentRoom = convoRoom;
update(['room-profile']);
isLoading.value = true;
activeMembers = await _getActiveMembers();
activeMembers = (await _currentRoom!.activeMembers()).toList();
update(['active-members']);
_fetchUserProfiles();
if (_currentRoom == null) {
return; // user may close chat screen before long loading completed
}
Expand Down Expand Up @@ -125,23 +133,47 @@ class ChatRoomController extends GetxController {
return _currentRoom?.getRoomId();
}

Future<List<Map<String, dynamic>>> _getActiveMembers() async {
List<Member> members = (await _currentRoom!.activeMembers())
.where((x) => x.userId() != client.userId().toString())
.toList();
List<Map<String, dynamic>> records = [];
for (Member member in members) {
UserProfile profile = await member.getProfile();
Map<String, dynamic> record = {
'display': profile.getDisplayName(),
'link': member.userId(),
};
Future<void> _fetchUserProfiles() async {
Map<String, Future<FfiBufferUint8>> avatars = {};
Map<String, String> names = {};
List<String> ids = [];
List<Map<String, dynamic>> mentionRecords = [];
for (int i = 0; i < activeMembers.length; i++) {
String userId = activeMembers[i].userId();
ids.add('user-profile-$userId');
UserProfile profile = await activeMembers[i].getProfile();
Map<String, dynamic> record = {};
if (profile.hasAvatar()) {
record['avatar'] = profile.getAvatar();
avatars[userId] = profile.getAvatar();
record['avatar'] = avatars[userId];
}
String? name = profile.getDisplayName();
record['display'] = name;
record['link'] = userId;
if (name != null) {
names[userId] = name;
}
mentionRecords.add(record);
if (i % 3 == 0 || i == activeMembers.length - 1) {
_userAvatars.addAll(avatars);
_userNames.addAll(names);
mentionList.addAll(mentionRecords);
mentionRecords.clear();
update(['chat-input']);
update(ids);
avatars.clear();
names.clear();
ids.clear();
}
records.add(record);
}
return records;
}

Future<FfiBufferUint8>? getUserAvatar(String userId) {
return _userAvatars.containsKey(userId) ? _userAvatars[userId] : null;
}

String? getUserName(String userId) {
return _userNames.containsKey(userId) ? _userNames[userId] : null;
}

//preview message link
Expand Down Expand Up @@ -376,7 +408,10 @@ class ChatRoomController extends GetxController {
void _loadMessage(RoomMessage message) {
String msgtype = message.msgtype();
String sender = message.sender();
var author = types.User(id: sender, firstName: simplifyUserId(sender));
var author = types.User(
id: sender,
firstName: simplifyUserId(sender),
);
int createdAt = message.originServerTs(); // in milliseconds
String eventId = message.eventId();

Expand Down Expand Up @@ -451,7 +486,7 @@ class ChatRoomController extends GetxController {
void sendButtonUpdate() {
isSendButtonVisible =
mentionKey.currentState!.controller!.text.trim().isNotEmpty;
update();
update(['chat-input']);
}

Future<bool> typingNotice(bool typing) async {
Expand Down
Loading

0 comments on commit 30aa794

Please sign in to comment.