get member name and avatar if local storage doesn't have

main
htylight 2023-10-04 15:44:28 +08:00
parent 55ed3de9d1
commit 67377bea97
3 changed files with 185 additions and 223 deletions

View File

@ -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/contact_model.dart';
import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/init_get_it.dart';
import 'package:together_mobile/models/user_model.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/request/server.dart';
import 'package:together_mobile/screens/message/components/attachment_container.dart'; import 'package:together_mobile/screens/message/components/attachment_container.dart';
@ -21,23 +20,21 @@ class GroupChatMessageBubble extends StatefulWidget {
required this.length, required this.length,
required this.contactId, required this.contactId,
required this.senderId, required this.senderId,
required this.isFriend,
required this.hasNameAvatar,
required this.isOther,
required this.type, required this.type,
required this.dateTime, required this.dateTime,
required this.isShowTime, required this.isShowTime,
required this.text, required this.text,
required this.attachments, required this.attachments,
required this.allAttachments, required this.allAttachments,
required this.isGettingFlag,
required this.onGetting,
required this.afterGetting,
}); });
final int index, length; final int index, length;
final String contactId, senderId, type, dateTime, text; final String contactId, senderId, type, dateTime, text;
final bool isShowTime; final bool isShowTime, isFriend, isOther, hasNameAvatar;
final List<String> attachments, allAttachments; final List<String> attachments, allAttachments;
final Map<String, bool> isGettingFlag;
final Function(String) onGetting, afterGetting;
@override @override
State<GroupChatMessageBubble> createState() => _GroupChatMessageBubbleState(); State<GroupChatMessageBubble> createState() => _GroupChatMessageBubbleState();
@ -69,52 +66,8 @@ class _GroupChatMessageBubbleState extends State<GroupChatMessageBubble> {
} }
} }
Future<bool> _getMemberProfile(bool isFriend, bool isOther) async {
if (!isOther) {
return true;
}
if (widget.isGettingFlag.containsKey(widget.senderId)) {
return true;
}
if (!getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles
.containsKey(widget.contactId) ||
!getIt
.get<ContactAccountProfile>()
.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<ContactAccountProfile>().addGroupChatMemberProfile(
widget.contactId,
widget.senderId,
res['data'],
);
widget.afterGetting(widget.senderId);
}
}
return Future(() => true);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
bool isOther = widget.senderId != getIt.get<UserAccount>().id;
// Because myself is also in `friends` map
bool isFriend =
getIt.get<Contact>().friends.containsKey(widget.senderId) && isOther;
return Container( return Container(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
vertical: kDefaultPadding / 2, vertical: kDefaultPadding / 2,
@ -133,143 +86,75 @@ class _GroupChatMessageBubbleState extends State<GroupChatMessageBubble> {
), ),
), ),
), ),
FutureBuilder( Row(
future: _getMemberProfile(isFriend, isOther), textDirection:
builder: (context, snapshot) { widget.isOther ? TextDirection.ltr : TextDirection.rtl,
return Row( crossAxisAlignment: CrossAxisAlignment.start,
textDirection: isOther ? TextDirection.ltr : TextDirection.rtl, children: [
crossAxisAlignment: CrossAxisAlignment.start, widget.hasNameAvatar
children: [ ? _showAvatar(widget.isOther)
snapshot.hasData : const CircleAvatar(
? _showAvatar(isOther) backgroundImage: AssetImage('assets/images/user_5.png'),
: 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,
);
},
),
],
),
),
],
), ),
), const SizedBox(
], width: 10,
); ),
}, Expanded(
), child: Column(
// Row( crossAxisAlignment: widget.isOther
// textDirection: isOther ? TextDirection.ltr : TextDirection.rtl, ? CrossAxisAlignment.start
// crossAxisAlignment: CrossAxisAlignment.start, : CrossAxisAlignment.end,
// children: [ children: [
// _showAvatar(isOther), widget.hasNameAvatar
// const SizedBox( ? _showName(widget.isOther, widget.isFriend)
// width: 10, : const Text(''),
// ), const SizedBox(
// Expanded( height: 5,
// child: Column( ),
// crossAxisAlignment: isOther if (widget.type == 'text/multipart')
// ? CrossAxisAlignment.start // message box
// : CrossAxisAlignment.end, Container(
// children: [ padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
// _showName(isOther, isFriend), decoration: BoxDecoration(
// const SizedBox( color: widget.isOther
// height: 5, ? const Color.fromARGB(255, 241, 241, 241)
// ), : kPrimaryColor,
// if (widget.type == 'text/multipart') borderRadius: BorderRadius.circular(10.0),
// // message box ),
// Container( child: Column(
// padding: const EdgeInsets.fromLTRB(10, 10, 10, 0), crossAxisAlignment: CrossAxisAlignment.start,
// decoration: BoxDecoration( children: [
// color: isOther if (_isHideMsg) const Text('消息已隐藏'),
// ? 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 // text message content
// if (widget.text.isNotEmpty && !_isHideMsg) if (widget.text.isNotEmpty && !_isHideMsg)
// Text( Text(
// widget.text, widget.text,
// textWidthBasis: TextWidthBasis.longestLine, textWidthBasis: TextWidthBasis.longestLine,
// ), ),
// const SizedBox( const SizedBox(
// height: 10, height: 10,
// ), ),
// // image content if have // image content if have
// if (widget.attachments.isNotEmpty && !_isHideMsg) if (widget.attachments.isNotEmpty && !_isHideMsg)
// ...List.generate( ...List.generate(
// widget.attachments.length, widget.attachments.length,
// (int index) { (int index) {
// return AttachmentContainer( return AttachmentContainer(
// isMyself: !isOther, isMyself: !widget.isOther,
// filename: widget.attachments[index], filename: widget.attachments[index],
// onTapImage: _onTapImage, onTapImage: _onTapImage,
// ); );
// }, },
// ), ),
// ], ],
// ), ),
// ), ),
// ], ],
// ), ),
// ), ),
// ], ],
// ), ),
TextButton( TextButton(
onPressed: () {}, onPressed: () {},
style: TextButton.styleFrom( style: TextButton.styleFrom(
@ -345,10 +230,9 @@ class _GroupChatMessageBubbleState extends State<GroupChatMessageBubble> {
if (remarkInGroupChat.isNotEmpty) { if (remarkInGroupChat.isNotEmpty) {
return Text(remarkInGroupChat); return Text(remarkInGroupChat);
} else { } else {
return Text(remarkInGroupChat = getIt return Text(
.get<ContactAccountProfile>() getIt.get<ContactAccountProfile>().friends[widget.senderId]!.nickname,
.grouChatMemberProfiles[widget.contactId]![widget.senderId]! );
.nickname);
} }
} else if (isOther) { } else if (isOther) {
String remarkInGroupChat = getIt String remarkInGroupChat = getIt
@ -359,10 +243,12 @@ class _GroupChatMessageBubbleState extends State<GroupChatMessageBubble> {
if (remarkInGroupChat.isNotEmpty) { if (remarkInGroupChat.isNotEmpty) {
return Text(remarkInGroupChat); return Text(remarkInGroupChat);
} else { } else {
return Text(remarkInGroupChat = getIt return Text(
.get<ContactAccountProfile>() getIt
.grouChatMemberProfiles[widget.contactId]![widget.senderId]! .get<ContactAccountProfile>()
.nickname); .grouChatMemberProfiles[widget.contactId]![widget.senderId]!
.nickname,
);
} }
} else { } else {
String remarkInGroupChat = String remarkInGroupChat =

View File

@ -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/contact_model.dart';
import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/init_get_it.dart';
import 'package:together_mobile/models/route_state_model.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 'package:together_mobile/utils/format_datetime.dart';
import 'components/group_chat_message_bubble.dart'; import 'components/group_chat_message_bubble.dart';
import 'components/message_input_box.dart'; import 'components/message_input_box.dart';
@ -29,7 +31,54 @@ class _GroupChatMessageScreenState extends State<GroupChatMessageScreen> {
final ScrollController _controller = ScrollController(); final ScrollController _controller = ScrollController();
final Box<ChatSetting> _chatSettingBox = final Box<ChatSetting> _chatSettingBox =
Hive.box<ChatSetting>('chat_setting'); Hive.box<ChatSetting>('chat_setting');
final Map<String, bool> _isGettingFlag = {}; final Map<String, bool> _isGettingNameAvatar = {};
Future<bool> _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<ContactAccountProfile>()
.grouChatMemberProfiles
.containsKey(widget.groupChatId) ||
!getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[widget.groupChatId]!
.containsKey(senderId)) {
_isGettingNameAvatar[senderId] = true;
final res = await getGroupChatMemberNameAvatar(
widget.groupChatId,
senderId,
isFriend,
);
if (res['code'] == 10800) {
getIt.get<ContactAccountProfile>().addGroupChatMemberProfile(
widget.groupChatId,
senderId,
res['data'],
);
}
}
print('22222222222222');
_isGettingNameAvatar.remove(senderId);
return Future(() => true);
}
@override @override
void dispose() { void dispose() {
@ -145,21 +194,60 @@ class _GroupChatMessageScreenState extends State<GroupChatMessageScreen> {
} }
} }
return GroupChatMessageBubble( bool isOther =
key: ValueKey(i), messageT.senderId != getIt.get<UserAccount>().id;
index: i, // Because myself is also in `friends` map
length: length, bool isFriend = getIt
contactId: widget.groupChatId, .get<Contact>()
senderId: messageT.senderId, .friends
dateTime: formatMessageDateTime(messageT.dateTime), .containsKey(messageT.senderId) &&
isShowTime: messageT.isShowTime, isOther;
type: messageT.type,
text: messageT.text, return FutureBuilder(
attachments: messageT.attachments, future: _getMemberProfile(
allAttachments: allAttachments, isFriend,
isGettingFlag: _isGettingFlag, isOther,
onGetting: _onGettingMemberProfile, messageT.senderId,
afterGetting: _afterGettingMemberProfile, ),
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<GroupChatMessageScreen> {
bottomNavigationBar: null, bottomNavigationBar: null,
); );
} }
void _onGettingMemberProfile(String userId) {
setState(() {
_isGettingFlag[userId] = true;
});
}
void _afterGettingMemberProfile(String userId) {
setState(() {
_isGettingFlag.remove(userId);
});
}
} }

View File

@ -46,7 +46,7 @@ class SettingScreen extends StatelessWidget {
getIt.get<ApplyList>().clear(); getIt.get<ApplyList>().clear();
getIt.get<Contact>().clear(); getIt.get<Contact>().clear();
getIt.get<ContactAccountProfile>().clear(); getIt.get<ContactAccountProfile>().clear();
// Hive.deleteFromDisk(); Hive.deleteFromDisk();
await HiveDatabase.close(); await HiveDatabase.close();
}, },
); );