From 36b9acf451fc4ffb027ac2b63dbc202487bfd562 Mon Sep 17 00:00:00 2001 From: Kosuke Saigusa Date: Wed, 27 Sep 2023 18:18:56 +0900 Subject: [PATCH] feat: refactor codes --- .../firestore_repositories/chat_message.dart | 2 +- .../lib/chat/chat_room.dart | 3 +- .../lib/chat/chat_rooms.dart | 3 +- .../lib/chat/ui/chat_room.dart | 145 +++--------------- 4 files changed, 28 insertions(+), 125 deletions(-) diff --git a/packages/firebase_common/lib/src/firestore_repositories/chat_message.dart b/packages/firebase_common/lib/src/firestore_repositories/chat_message.dart index dca0b8c8..0c61c5b7 100644 --- a/packages/firebase_common/lib/src/firestore_repositories/chat_message.dart +++ b/packages/firebase_common/lib/src/firestore_repositories/chat_message.dart @@ -115,6 +115,6 @@ class ChatMessageRepository { ) .orderBy('createdAt', descending: true) .limit(limit) - : query.orderBy('createAt', descending: true).limit(limit), + : query.orderBy('createdAt', descending: true).limit(limit), ); } diff --git a/packages/mottai_flutter_app/lib/chat/chat_room.dart b/packages/mottai_flutter_app/lib/chat/chat_room.dart index 43243021..31c91aae 100644 --- a/packages/mottai_flutter_app/lib/chat/chat_room.dart +++ b/packages/mottai_flutter_app/lib/chat/chat_room.dart @@ -235,7 +235,8 @@ class StartChat { await _chatMessageRepository.addChatMessage( chatRoomId: chatRoomId, senderId: workerId, - chatMessageType: ChatMessageType.host, + // メッセージを始めるのは常にワーカー。 + chatMessageType: ChatMessageType.worker, content: content, ); return chatRoomId; diff --git a/packages/mottai_flutter_app/lib/chat/chat_rooms.dart b/packages/mottai_flutter_app/lib/chat/chat_rooms.dart index 513931a6..32f77063 100644 --- a/packages/mottai_flutter_app/lib/chat/chat_rooms.dart +++ b/packages/mottai_flutter_app/lib/chat/chat_rooms.dart @@ -48,7 +48,8 @@ const _maxUnReadCount = 10; /// 取得する [StreamProvider]. final _unReadCountStreamProvider = StreamProvider.autoDispose.family((ref, readChatRoom) { - final readStatus = ref.watch(myReadStatusStreamProvider(readChatRoom)).value; + final readStatus = + ref.watch(myReadStatusStreamProvider(readChatRoom)).valueOrNull; final lastReadAt = readStatus?.lastReadAt; return ref .watch(chatMessageRepositoryProvider) diff --git a/packages/mottai_flutter_app/lib/chat/ui/chat_room.dart b/packages/mottai_flutter_app/lib/chat/ui/chat_room.dart index d46d2fba..b7fac427 100644 --- a/packages/mottai_flutter_app/lib/chat/ui/chat_room.dart +++ b/packages/mottai_flutter_app/lib/chat/ui/chat_room.dart @@ -103,45 +103,32 @@ class _ChatRoomPageState extends ConsumerState { )) { return const Unavailable('そのチャットルームは表示できません。'); } - return Stack( + return Column( children: [ - Column( - children: [ - Expanded( - child: ListView.builder( - itemCount: state.readChatMessages.length, - reverse: true, - controller: _scrollController, - itemBuilder: (context, index) { - final readChatMessage = state.readChatMessages[index]; - return Padding( - padding: const EdgeInsets.only(bottom: 24), - child: _ChatMessageItem( - readChatRoom: readChatRoom, - readChatMessage: readChatMessage, - isMyMessage: readChatMessage.senderId == userId, - chatRoomId: widget.chatRoomId, - ), - ); - }, - ), - ), - _MessageTextField( - chatRoomId: widget.chatRoomId, - userId: userId, - ), - const Gap(24), - ], - ), - Positioned( - child: Align( - alignment: Alignment.topCenter, - child: _DebugIndicator( - chatRoomId: widget.chatRoomId, - readChatRoom: readChatRoom, - ), + Expanded( + child: ListView.builder( + itemCount: state.readChatMessages.length, + reverse: true, + controller: _scrollController, + itemBuilder: (context, index) { + final readChatMessage = state.readChatMessages[index]; + return Padding( + padding: const EdgeInsets.only(bottom: 24), + child: _ChatMessageItem( + readChatRoom: readChatRoom, + readChatMessage: readChatMessage, + isMyMessage: readChatMessage.senderId == userId, + chatRoomId: widget.chatRoomId, + ), + ); + }, ), ), + _MessageTextField( + chatRoomId: widget.chatRoomId, + userId: userId, + ), + const Gap(24), ], ); }, @@ -436,89 +423,3 @@ class _MessageTextFieldState extends ConsumerState<_MessageTextField> { } } } - -// TODO: 後で消す -/// 開発時のみ表示する、無限スクロールのデバッグ用ウィジェット。 -class _DebugIndicator extends ConsumerWidget { - const _DebugIndicator({ - required this.chatRoomId, - required this.readChatRoom, - }); - - final String chatRoomId; - - final ReadChatRoom readChatRoom; - - @override - Widget build(BuildContext context, WidgetRef ref) { - final state = ref.watch(chatRoomStateNotifierProvider(chatRoomId)); - final readChatMessages = state.readChatMessages; - final lastReadChatMessageId = state.lastReadChatMessageId; - final partnerLastReadAt = ref.watch( - chatPartnerLastReadAtProvider( - readChatRoom, - ), - ); - return Container( - width: double.infinity, - margin: const EdgeInsets.only(top: 16, left: 16, right: 16), - padding: const EdgeInsets.all(16), - decoration: const BoxDecoration( - color: Colors.black38, - borderRadius: BorderRadius.all(Radius.circular(8)), - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - Text( - 'デバッグウィンドウ', - style: Theme.of(context) - .textTheme - .titleSmall! - .copyWith(color: Colors.white), - ), - const Gap(4), - Text( - '取得したメッセージ:${readChatMessages.length.withComma} 件', - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(color: Colors.white), - ), - Text( - '取得中?:${state.fetching}', - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(color: Colors.white), - ), - Text( - 'まだ取得できる?:${state.hasMore}', - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(color: Colors.white), - ), - if (lastReadChatMessageId != null) - Text( - '最後に取得したドキュメント ID:$lastReadChatMessageId', - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(color: Colors.white), - ), - Text( - 'パートナーの既読時間:$partnerLastReadAt', - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(color: Colors.white), - ), - const Gap(8), - ], - ), - ); - } -}