From 67377bea9707ae7561e63f4fd81c3481d71b00a3 Mon Sep 17 00:00:00 2001 From: htylight Date: Wed, 4 Oct 2023 15:44:28 +0800 Subject: [PATCH] get member name and avatar if local storage doesn't have --- .../components/group_chat_message_bubble.dart | 274 +++++------------- .../message/group_chat_message_screen.dart | 132 +++++++-- .../more/setting_screen/setting_screen.dart | 2 +- 3 files changed, 185 insertions(+), 223 deletions(-) diff --git a/lib/screens/message/components/group_chat_message_bubble.dart b/lib/screens/message/components/group_chat_message_bubble.dart index d432588..c50e98c 100755 --- a/lib/screens/message/components/group_chat_message_bubble.dart +++ b/lib/screens/message/components/group_chat_message_bubble.dart @@ -10,7 +10,6 @@ import 'package:together_mobile/database/box_type.dart'; import 'package:together_mobile/models/contact_model.dart'; import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/user_model.dart'; -import 'package:together_mobile/request/group_chat.dart'; import 'package:together_mobile/request/server.dart'; import 'package:together_mobile/screens/message/components/attachment_container.dart'; @@ -21,23 +20,21 @@ class GroupChatMessageBubble extends StatefulWidget { required this.length, required this.contactId, required this.senderId, + required this.isFriend, + required this.hasNameAvatar, + required this.isOther, required this.type, required this.dateTime, required this.isShowTime, required this.text, required this.attachments, required this.allAttachments, - required this.isGettingFlag, - required this.onGetting, - required this.afterGetting, }); final int index, length; final String contactId, senderId, type, dateTime, text; - final bool isShowTime; + final bool isShowTime, isFriend, isOther, hasNameAvatar; final List attachments, allAttachments; - final Map isGettingFlag; - final Function(String) onGetting, afterGetting; @override State createState() => _GroupChatMessageBubbleState(); @@ -69,52 +66,8 @@ class _GroupChatMessageBubbleState extends State { } } - Future _getMemberProfile(bool isFriend, bool isOther) async { - if (!isOther) { - return true; - } - - if (widget.isGettingFlag.containsKey(widget.senderId)) { - return true; - } - - if (!getIt - .get() - .grouChatMemberProfiles - .containsKey(widget.contactId) || - !getIt - .get() - .grouChatMemberProfiles[widget.contactId]! - .containsKey(widget.senderId)) { - final res = await getGroupChatMemberNameAvatar( - widget.contactId, - widget.senderId, - isFriend, - ); - - widget.onGetting(widget.senderId); - - if (res['code'] == 10800) { - getIt.get().addGroupChatMemberProfile( - widget.contactId, - widget.senderId, - res['data'], - ); - - widget.afterGetting(widget.senderId); - } - } - - return Future(() => true); - } - @override Widget build(BuildContext context) { - bool isOther = widget.senderId != getIt.get().id; - // Because myself is also in `friends` map - bool isFriend = - getIt.get().friends.containsKey(widget.senderId) && isOther; - return Container( padding: const EdgeInsets.symmetric( vertical: kDefaultPadding / 2, @@ -133,143 +86,75 @@ class _GroupChatMessageBubbleState extends State { ), ), ), - FutureBuilder( - future: _getMemberProfile(isFriend, isOther), - builder: (context, snapshot) { - return Row( - textDirection: isOther ? TextDirection.ltr : TextDirection.rtl, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - snapshot.hasData - ? _showAvatar(isOther) - : const CircleAvatar( - backgroundImage: - AssetImage('assets/images/loading.gif'), - ), - const SizedBox( - width: 10, - ), - Expanded( - child: Column( - crossAxisAlignment: isOther - ? CrossAxisAlignment.start - : CrossAxisAlignment.end, - children: [ - snapshot.hasData - ? _showName(isOther, isFriend) - : const Text(''), - const SizedBox( - height: 5, - ), - if (widget.type == 'text/multipart') - // message box - Container( - padding: const EdgeInsets.fromLTRB(10, 10, 10, 0), - decoration: BoxDecoration( - color: isOther - ? const Color.fromARGB(255, 241, 241, 241) - : kPrimaryColor, - borderRadius: BorderRadius.circular(10.0), - ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (_isHideMsg) const Text('消息已隐藏'), - - // text message content - if (widget.text.isNotEmpty && !_isHideMsg) - Text( - widget.text, - textWidthBasis: TextWidthBasis.longestLine, - ), - const SizedBox( - height: 10, - ), - // image content if have - if (widget.attachments.isNotEmpty && - !_isHideMsg) - ...List.generate( - widget.attachments.length, - (int index) { - return AttachmentContainer( - isMyself: !isOther, - filename: widget.attachments[index], - onTapImage: _onTapImage, - ); - }, - ), - ], - ), - ), - ], + Row( + textDirection: + widget.isOther ? TextDirection.ltr : TextDirection.rtl, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + widget.hasNameAvatar + ? _showAvatar(widget.isOther) + : const CircleAvatar( + backgroundImage: AssetImage('assets/images/user_5.png'), ), - ), - ], - ); - }, - ), - // Row( - // textDirection: isOther ? TextDirection.ltr : TextDirection.rtl, - // crossAxisAlignment: CrossAxisAlignment.start, - // children: [ - // _showAvatar(isOther), - // const SizedBox( - // width: 10, - // ), - // Expanded( - // child: Column( - // crossAxisAlignment: isOther - // ? CrossAxisAlignment.start - // : CrossAxisAlignment.end, - // children: [ - // _showName(isOther, isFriend), - // const SizedBox( - // height: 5, - // ), - // if (widget.type == 'text/multipart') - // // message box - // Container( - // padding: const EdgeInsets.fromLTRB(10, 10, 10, 0), - // decoration: BoxDecoration( - // color: isOther - // ? const Color.fromARGB(255, 241, 241, 241) - // : kPrimaryColor, - // borderRadius: BorderRadius.circular(10.0), - // ), - // child: Column( - // crossAxisAlignment: CrossAxisAlignment.start, - // children: [ - // if (_isHideMsg) const Text('消息已隐藏'), + const SizedBox( + width: 10, + ), + Expanded( + child: Column( + crossAxisAlignment: widget.isOther + ? CrossAxisAlignment.start + : CrossAxisAlignment.end, + children: [ + widget.hasNameAvatar + ? _showName(widget.isOther, widget.isFriend) + : const Text(''), + const SizedBox( + height: 5, + ), + if (widget.type == 'text/multipart') + // message box + Container( + padding: const EdgeInsets.fromLTRB(10, 10, 10, 0), + decoration: BoxDecoration( + color: widget.isOther + ? const Color.fromARGB(255, 241, 241, 241) + : kPrimaryColor, + borderRadius: BorderRadius.circular(10.0), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (_isHideMsg) const Text('消息已隐藏'), - // // text message content - // if (widget.text.isNotEmpty && !_isHideMsg) - // Text( - // widget.text, - // textWidthBasis: TextWidthBasis.longestLine, - // ), - // const SizedBox( - // height: 10, - // ), - // // image content if have - // if (widget.attachments.isNotEmpty && !_isHideMsg) - // ...List.generate( - // widget.attachments.length, - // (int index) { - // return AttachmentContainer( - // isMyself: !isOther, - // filename: widget.attachments[index], - // onTapImage: _onTapImage, - // ); - // }, - // ), - // ], - // ), - // ), - // ], - // ), - // ), - // ], - // ), + // text message content + if (widget.text.isNotEmpty && !_isHideMsg) + Text( + widget.text, + textWidthBasis: TextWidthBasis.longestLine, + ), + const SizedBox( + height: 10, + ), + // image content if have + if (widget.attachments.isNotEmpty && !_isHideMsg) + ...List.generate( + widget.attachments.length, + (int index) { + return AttachmentContainer( + isMyself: !widget.isOther, + filename: widget.attachments[index], + onTapImage: _onTapImage, + ); + }, + ), + ], + ), + ), + ], + ), + ), + ], + ), TextButton( onPressed: () {}, style: TextButton.styleFrom( @@ -345,10 +230,9 @@ class _GroupChatMessageBubbleState extends State { if (remarkInGroupChat.isNotEmpty) { return Text(remarkInGroupChat); } else { - return Text(remarkInGroupChat = getIt - .get() - .grouChatMemberProfiles[widget.contactId]![widget.senderId]! - .nickname); + return Text( + getIt.get().friends[widget.senderId]!.nickname, + ); } } else if (isOther) { String remarkInGroupChat = getIt @@ -359,10 +243,12 @@ class _GroupChatMessageBubbleState extends State { if (remarkInGroupChat.isNotEmpty) { return Text(remarkInGroupChat); } else { - return Text(remarkInGroupChat = getIt - .get() - .grouChatMemberProfiles[widget.contactId]![widget.senderId]! - .nickname); + return Text( + getIt + .get() + .grouChatMemberProfiles[widget.contactId]![widget.senderId]! + .nickname, + ); } } else { String remarkInGroupChat = diff --git a/lib/screens/message/group_chat_message_screen.dart b/lib/screens/message/group_chat_message_screen.dart index b08d07d..c2c990f 100755 --- a/lib/screens/message/group_chat_message_screen.dart +++ b/lib/screens/message/group_chat_message_screen.dart @@ -9,6 +9,8 @@ import 'package:together_mobile/database/box_type.dart'; import 'package:together_mobile/models/contact_model.dart'; import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/route_state_model.dart'; +import 'package:together_mobile/models/user_model.dart'; +import 'package:together_mobile/request/group_chat.dart'; import 'package:together_mobile/utils/format_datetime.dart'; import 'components/group_chat_message_bubble.dart'; import 'components/message_input_box.dart'; @@ -29,7 +31,54 @@ class _GroupChatMessageScreenState extends State { final ScrollController _controller = ScrollController(); final Box _chatSettingBox = Hive.box('chat_setting'); - final Map _isGettingFlag = {}; + final Map _isGettingNameAvatar = {}; + + Future _getMemberProfile( + bool isFriend, + bool isOther, + String senderId, + ) async { + if (!isOther) { + return true; + } + + if (_isGettingNameAvatar.containsKey(senderId)) { + await Future.doWhile(() async { + await Future.delayed(const Duration(milliseconds: 300)); + return _isGettingNameAvatar.containsKey(senderId); + }); + return Future(() => true); + } + + if (!getIt + .get() + .grouChatMemberProfiles + .containsKey(widget.groupChatId) || + !getIt + .get() + .grouChatMemberProfiles[widget.groupChatId]! + .containsKey(senderId)) { + _isGettingNameAvatar[senderId] = true; + + final res = await getGroupChatMemberNameAvatar( + widget.groupChatId, + senderId, + isFriend, + ); + + if (res['code'] == 10800) { + getIt.get().addGroupChatMemberProfile( + widget.groupChatId, + senderId, + res['data'], + ); + } + } + print('22222222222222'); + _isGettingNameAvatar.remove(senderId); + + return Future(() => true); + } @override void dispose() { @@ -145,21 +194,60 @@ class _GroupChatMessageScreenState extends State { } } - return GroupChatMessageBubble( - key: ValueKey(i), - index: i, - length: length, - contactId: widget.groupChatId, - senderId: messageT.senderId, - dateTime: formatMessageDateTime(messageT.dateTime), - isShowTime: messageT.isShowTime, - type: messageT.type, - text: messageT.text, - attachments: messageT.attachments, - allAttachments: allAttachments, - isGettingFlag: _isGettingFlag, - onGetting: _onGettingMemberProfile, - afterGetting: _afterGettingMemberProfile, + bool isOther = + messageT.senderId != getIt.get().id; + // Because myself is also in `friends` map + bool isFriend = getIt + .get() + .friends + .containsKey(messageT.senderId) && + isOther; + + return FutureBuilder( + future: _getMemberProfile( + isFriend, + isOther, + messageT.senderId, + ), + builder: (context, snapshot) { + if (snapshot.hasData) { + return GroupChatMessageBubble( + key: ValueKey(i), + index: i, + length: length, + contactId: widget.groupChatId, + senderId: messageT.senderId, + isFriend: isFriend, + isOther: isOther, + hasNameAvatar: true, + dateTime: + formatMessageDateTime(messageT.dateTime), + isShowTime: messageT.isShowTime, + type: messageT.type, + text: messageT.text, + attachments: messageT.attachments, + allAttachments: allAttachments, + ); + } else { + return GroupChatMessageBubble( + key: ValueKey(i), + index: i, + length: length, + contactId: widget.groupChatId, + senderId: messageT.senderId, + isFriend: isFriend, + isOther: isOther, + hasNameAvatar: false, + dateTime: + formatMessageDateTime(messageT.dateTime), + isShowTime: messageT.isShowTime, + type: messageT.type, + text: messageT.text, + attachments: messageT.attachments, + allAttachments: allAttachments, + ); + } + }, ); }, ); @@ -177,16 +265,4 @@ class _GroupChatMessageScreenState extends State { bottomNavigationBar: null, ); } - - void _onGettingMemberProfile(String userId) { - setState(() { - _isGettingFlag[userId] = true; - }); - } - - void _afterGettingMemberProfile(String userId) { - setState(() { - _isGettingFlag.remove(userId); - }); - } } diff --git a/lib/screens/more/setting_screen/setting_screen.dart b/lib/screens/more/setting_screen/setting_screen.dart index 7c98935..6176dc9 100644 --- a/lib/screens/more/setting_screen/setting_screen.dart +++ b/lib/screens/more/setting_screen/setting_screen.dart @@ -46,7 +46,7 @@ class SettingScreen extends StatelessWidget { getIt.get().clear(); getIt.get().clear(); getIt.get().clear(); - // Hive.deleteFromDisk(); + Hive.deleteFromDisk(); await HiveDatabase.close(); }, );