From c42736ce24a9e9697914a909a4e248ae287f931f Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Thu, 20 Jun 2024 21:38:59 -0400 Subject: [PATCH] fix titles --- lib/chat/cubits/chat_component_cubit.dart | 64 ++++++++++++------- .../cubits/conversation_cubit.dart | 2 + 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/lib/chat/cubits/chat_component_cubit.dart b/lib/chat/cubits/chat_component_cubit.dart index cd0f724..83e3d21 100644 --- a/lib/chat/cubits/chat_component_cubit.dart +++ b/lib/chat/cubits/chat_component_cubit.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:typed_data'; import 'package:async_tools/async_tools.dart'; +import 'package:bloc_advanced_tools/bloc_advanced_tools.dart'; import 'package:fast_immutable_collections/fast_immutable_collections.dart'; import 'package:fixnum/fixnum.dart'; import 'package:flutter/widgets.dart'; @@ -24,6 +25,7 @@ const metadataKeyIdentityPublicKey = 'identityPublicKey'; const metadataKeyExpirationDuration = 'expiration'; const metadataKeyViewLimit = 'view_limit'; const metadataKeyAttachments = 'attachments'; +const _sfChangedContacts = 'changedContacts'; class ChatComponentCubit extends Cubit { ChatComponentCubit._({ @@ -80,11 +82,17 @@ class ChatComponentCubit extends Cubit { // Subscribe to messages _messagesSubscription = _messagesCubit.stream.listen(_onChangedMessages); _onChangedMessages(_messagesCubit.state); + + // Subscribe to contact list changes + _contactListSubscription = + _contactListCubit.stream.listen(_onChangedContacts); + _onChangedContacts(_contactListCubit.state); } @override Future close() async { await _initWait(); + await _contactListSubscription.cancel(); await _accountRecordSubscription.cancel(); await _messagesSubscription.cancel(); await _conversationSubscriptions.values.map((v) => v.cancel()).wait; @@ -170,6 +178,13 @@ class ChatComponentCubit extends Cubit { emit(_convertMessages(state, avMessagesState)); } + void _onChangedContacts( + BlocBusyState>>> + bavContacts) { + // Rewrite users when contacts change + singleFuture((this, _sfChangedContacts), _updateConversationSubscriptions); + } + void _onChangedConversation( TypedKey remoteIdentityPublicKey, AsyncValue avConversationState, @@ -180,11 +195,23 @@ class ChatComponentCubit extends Cubit { // Don't change user information on loading state return; } - emit(state.copyWith( + emit(_updateTitle(state.copyWith( remoteUsers: state.remoteUsers.add( remoteIdentityPublicKey, _convertRemoteUser( - remoteIdentityPublicKey, activeConversationState)))); + remoteIdentityPublicKey, activeConversationState))))); + } + + static ChatComponentState _updateTitle(ChatComponentState currentState) { + if (currentState.remoteUsers.length == 0) { + return currentState.copyWith(title: 'Empty Chat'); + } + if (currentState.remoteUsers.length == 1) { + final remoteUser = currentState.remoteUsers.values.first; + return currentState.copyWith(title: remoteUser.firstName ?? ''); + } + return currentState.copyWith( + title: ''); } types.User _convertRemoteUser(TypedKey remoteIdentityPublicKey, @@ -227,18 +254,18 @@ class ChatComponentCubit extends Cubit { // If the cubit is already being listened to we have nothing to do if (existing.remove(remoteIdentityPublicKey)) { - continue; + // If the cubit is not already being listened to we should do that + _conversationSubscriptions[remoteIdentityPublicKey] = cc.stream.listen( + (avConv) => + _onChangedConversation(remoteIdentityPublicKey, avConv)); } - // If the cubit is not already being listened to we should do that - _conversationSubscriptions[remoteIdentityPublicKey] = cc.stream.listen( - (avConv) => _onChangedConversation(remoteIdentityPublicKey, avConv)); final activeConversationState = cc.state.asData?.value; if (activeConversationState != null) { - final remoteUser = _convertRemoteUser( - remoteIdentityPublicKey, activeConversationState); - currentRemoteUsersState = - currentRemoteUsersState.add(remoteIdentityPublicKey, remoteUser); + currentRemoteUsersState = currentRemoteUsersState.add( + remoteIdentityPublicKey, + _convertRemoteUser( + remoteIdentityPublicKey, activeConversationState)); } } // Purge remote users we didn't see in the cubit list any more @@ -253,18 +280,6 @@ class ChatComponentCubit extends Cubit { emit(_updateTitle(state.copyWith(remoteUsers: currentRemoteUsersState))); } - ChatComponentState _updateTitle(ChatComponentState currentState) { - if (currentState.remoteUsers.length == 0) { - return currentState.copyWith(title: 'Empty Chat'); - } - if (currentState.remoteUsers.length == 1) { - final remoteUser = currentState.remoteUsers.values.first; - return currentState.copyWith(title: remoteUser.firstName ?? ''); - } - return currentState.copyWith( - title: ''); - } - (ChatComponentState, types.Message?) _messageStateToChatMessage( ChatComponentState currentState, MessageState message) { final authorIdentityPublicKey = message.content.author.toVeilid(); @@ -417,6 +432,9 @@ class ChatComponentCubit extends Cubit { final Map>> _conversationSubscriptions = {}; late StreamSubscription _messagesSubscription; - + late StreamSubscription< + BlocBusyState< + AsyncValue>>>> + _contactListSubscription; double scrollOffset = 0; } diff --git a/lib/conversation/cubits/conversation_cubit.dart b/lib/conversation/cubits/conversation_cubit.dart index 45bc251..728489f 100644 --- a/lib/conversation/cubits/conversation_cubit.dart +++ b/lib/conversation/cubits/conversation_cubit.dart @@ -320,6 +320,7 @@ class ConversationCubit extends Cubit> { open: open, decodeState: proto.Conversation.fromBuffer); _localSubscription = _localConversationCubit!.stream.listen(_updateLocalConversationState); + _updateLocalConversationState(_localConversationCubit!.state); } // Open remote converation key @@ -330,6 +331,7 @@ class ConversationCubit extends Cubit> { open: open, decodeState: proto.Conversation.fromBuffer); _remoteSubscription = _remoteConversationCubit!.stream.listen(_updateRemoteConversationState); + _updateRemoteConversationState(_remoteConversationCubit!.state); } // Initialize local messages