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/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<String> attachments, allAttachments;
final Map<String, bool> isGettingFlag;
final Function(String) onGetting, afterGetting;
@override
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
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(
padding: const EdgeInsets.symmetric(
vertical: kDefaultPadding / 2,
@ -133,143 +86,75 @@ class _GroupChatMessageBubbleState extends State<GroupChatMessageBubble> {
),
),
),
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<GroupChatMessageBubble> {
if (remarkInGroupChat.isNotEmpty) {
return Text(remarkInGroupChat);
} else {
return Text(remarkInGroupChat = getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[widget.contactId]![widget.senderId]!
.nickname);
return Text(
getIt.get<ContactAccountProfile>().friends[widget.senderId]!.nickname,
);
}
} else if (isOther) {
String remarkInGroupChat = getIt
@ -359,10 +243,12 @@ class _GroupChatMessageBubbleState extends State<GroupChatMessageBubble> {
if (remarkInGroupChat.isNotEmpty) {
return Text(remarkInGroupChat);
} else {
return Text(remarkInGroupChat = getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[widget.contactId]![widget.senderId]!
.nickname);
return Text(
getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[widget.contactId]![widget.senderId]!
.nickname,
);
}
} else {
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/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<GroupChatMessageScreen> {
final ScrollController _controller = ScrollController();
final Box<ChatSetting> _chatSettingBox =
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
void dispose() {
@ -145,21 +194,60 @@ class _GroupChatMessageScreenState extends State<GroupChatMessageScreen> {
}
}
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<UserAccount>().id;
// Because myself is also in `friends` map
bool isFriend = getIt
.get<Contact>()
.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<GroupChatMessageScreen> {
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<Contact>().clear();
getIt.get<ContactAccountProfile>().clear();
// Hive.deleteFromDisk();
Hive.deleteFromDisk();
await HiveDatabase.close();
},
);