diff --git a/lib/chat/cubits/active_chat_cubit.dart b/lib/chat/cubits/active_chat_cubit.dart index d8cdc3e..e47caec 100644 --- a/lib/chat/cubits/active_chat_cubit.dart +++ b/lib/chat/cubits/active_chat_cubit.dart @@ -4,7 +4,7 @@ import 'package:veilid_support/veilid_support.dart'; class ActiveChatCubit extends Cubit { ActiveChatCubit(super.initialState); - void setActiveChat(TypedKey? activeChat) { - emit(activeChat); + void setActiveChat(TypedKey? activeChatRemoteConversationRecordKey) { + emit(activeChatRemoteConversationRecordKey); } } diff --git a/lib/chat/views/chat_component.dart b/lib/chat/views/chat_component.dart index 62ca94f..dd6aa65 100644 --- a/lib/chat/views/chat_component.dart +++ b/lib/chat/views/chat_component.dart @@ -6,8 +6,11 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_chat_types/flutter_chat_types.dart' as types; import 'package:flutter_chat_ui/flutter_chat_ui.dart'; +import '../../account_manager/account_manager.dart'; +import '../../contacts/contacts.dart'; import '../../proto/proto.dart' as proto; import '../../theme/theme.dart'; +import '../../tools/tools.dart'; import '../chat.dart'; class ChatComponent extends StatefulWidget { @@ -109,73 +112,99 @@ class ChatComponentState extends State { final scale = theme.extension()!; final textTheme = Theme.of(context).textTheme; final chatTheme = makeChatTheme(scale, textTheme); - final contactName = widget.activeChatContact.editedProfile.name; - final protoMessages = - ref.watch(activeConversationMessagesProvider).asData?.value; - if (protoMessages == null) { - return waitingPage(context); - } - final messages = []; - for (final protoMessage in protoMessages) { - final message = protoMessageToMessage(protoMessage); - messages.insert(0, message); - } + final activeChatCubit = context.watch(); + final contactListCubit = context.watch(); - return DefaultTextStyle( - style: textTheme.bodySmall!, - child: Align( - alignment: AlignmentDirectional.centerEnd, - child: Stack( - children: [ - Column( - children: [ - Container( - height: 48, - decoration: BoxDecoration( - color: scale.primaryScale.subtleBorder, + final activeChatContactKey = activeChatCubit.state; + if (activeChatContactKey == null) { + return const NoConversationWidget(); + } + return contactListCubit.state.builder((context, contactList) { + + // Get active chat contact profile + final activeChatContactIdx = contactList.indexWhere( + (c) => activeChatContactKey == c.remoteConversationRecordKey); + late final proto.Contact activeChatContact; + if (activeChatContactIdx == -1) { + activeChatCubit.setActiveChat(null); + return const NoConversationWidget(); + } else { + activeChatContact = contactList[activeChatContactIdx]; + } + final contactName = activeChatContact.editedProfile.name; + + // Make a messages cubit for this conversation + xxx + + final protoMessages = + ref.watch(activeConversationMessagesProvider).asData?.value; + if (protoMessages == null) { + return waitingPage(context); + } + final messages = []; + for (final protoMessage in protoMessages) { + final message = protoMessageToMessage(protoMessage); + messages.insert(0, message); + } + + return DefaultTextStyle( + style: textTheme.bodySmall!, + child: Align( + alignment: AlignmentDirectional.centerEnd, + child: Stack( + children: [ + Column( + children: [ + Container( + height: 48, + decoration: BoxDecoration( + color: scale.primaryScale.subtleBorder, + ), + child: Row(children: [ + Align( + alignment: AlignmentDirectional.centerStart, + child: Padding( + padding: const EdgeInsetsDirectional.fromSTEB( + 16, 0, 16, 0), + child: Text(contactName, + textAlign: TextAlign.start, + style: textTheme.titleMedium), + )), + const Spacer(), + IconButton( + icon: const Icon(Icons.close), + onPressed: () async { + context + .read() + .setActiveChat(null); + }).paddingLTRB(16, 0, 16, 0) + ]), ), - child: Row(children: [ - Align( - alignment: AlignmentDirectional.centerStart, - child: Padding( - padding: const EdgeInsetsDirectional.fromSTEB( - 16, 0, 16, 0), - child: Text(contactName, - textAlign: TextAlign.start, - style: textTheme.titleMedium), - )), - const Spacer(), - IconButton( - icon: const Icon(Icons.close), - onPressed: () async { - context.read().setActiveChat(null); - }).paddingLTRB(16, 0, 16, 0) - ]), - ), - Expanded( - child: DecoratedBox( - decoration: const BoxDecoration(), - child: Chat( - theme: chatTheme, - messages: messages, - //onAttachmentPressed: _handleAttachmentPressed, - //onMessageTap: _handleMessageTap, - //onPreviewDataFetched: _handlePreviewDataFetched, + Expanded( + child: DecoratedBox( + decoration: const BoxDecoration(), + child: Chat( + theme: chatTheme, + messages: messages, + //onAttachmentPressed: _handleAttachmentPressed, + //onMessageTap: _handleMessageTap, + //onPreviewDataFetched: _handlePreviewDataFetched, - onSendPressed: (message) { - unawaited(_handleSendPressed(message)); - }, - //showUserAvatars: false, - //showUserNames: true, - user: _localUser, + onSendPressed: (message) { + unawaited(_handleSendPressed(message)); + }, + //showUserAvatars: false, + //showUserNames: true, + user: _localUser, + ), ), ), - ), - ], - ), - ], - ), - )); + ], + ), + ], + ), + )); + }); } } diff --git a/lib/chat/views/no_conversation_widget.dart b/lib/chat/views/no_conversation_widget.dart index 4657966..1b8545f 100644 --- a/lib/chat/views/no_conversation_widget.dart +++ b/lib/chat/views/no_conversation_widget.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; -//XXX should rename this -class NoContactWidget extends StatelessWidget { - const NoContactWidget({super.key}); +class NoConversationWidget extends StatelessWidget { + const NoConversationWidget({super.key}); @override // ignore: prefer_expression_function_bodies