Added early basic media viewing in Talk

This commit is contained in:
2023-02-24 23:22:04 +01:00
parent 0558be1d22
commit 37a0d5cb4a
8 changed files with 287 additions and 114 deletions

View File

@ -56,7 +56,7 @@ class _ChatListState extends State<ChatList> {
chats.add(ListTile(
title: Text(chatRoom.displayName),
subtitle: Text("${Jiffy.unixFromSecondsSinceEpoch(chatRoom.lastMessage.timestamp).fromNow()}: ${RichObjectStringProcessor.parse(chatRoom.lastMessage.message.replaceAll("\n", " "), chatRoom.lastMessage.messageParameters)}", overflow: TextOverflow.ellipsis),
subtitle: Text("${Jiffy.unixFromSecondsSinceEpoch(chatRoom.lastMessage.timestamp).fromNow()}: ${RichObjectStringProcessor.parseTextPreview(chatRoom.lastMessage.message.replaceAll("\n", " "), chatRoom.lastMessage.messageParameters)}", overflow: TextOverflow.ellipsis),
trailing: Visibility(
visible: chatRoom.unreadMessages > 0,
child: Container(
@ -83,7 +83,7 @@ class _ChatListState extends State<ChatList> {
onTap: () async {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return ChatView(
user: chatRoom,
room: chatRoom,
selfId: username,
avatar: circleAvatar,
);

View File

@ -10,11 +10,11 @@ import 'package:marianum_mobile/data/chatList/chatProps.dart';
import 'package:provider/provider.dart';
class ChatView extends StatefulWidget {
final GetRoomResponseObject user;
final GetRoomResponseObject room;
final String selfId;
final CircleAvatar avatar;
const ChatView({Key? key, required this.user, required this.selfId, required this.avatar}) : super(key: key);
const ChatView({Key? key, required this.room, required this.selfId, required this.avatar}) : super(key: key);
@override
State<ChatView> createState() => _ChatViewState();
@ -56,7 +56,7 @@ class _ChatViewState extends State<ChatView> {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
Provider.of<ChatProps>(context, listen: false).setQueryToken(widget.user.token);
Provider.of<ChatProps>(context, listen: false).setQueryToken(widget.room.token);
});
}
@ -67,11 +67,13 @@ class _ChatViewState extends State<ChatView> {
List<Bubble> messages = List<Bubble>.empty(growable: true);
if(!data.primaryLoading()) {
bool showMetadata = true;
bool showActorDisplayName = true;
bool showBubbleTime = true;
data.getChatResponse.sortByTimestamp().forEach((element) {
showMetadata = element.messageType == GetRoomResponseObjectMessageType.comment;
showActorDisplayName = element.messageType == GetRoomResponseObjectMessageType.comment && widget.room.type != GetRoomResponseObjectConversationType.oneToOne;
showBubbleTime = element.messageType != GetRoomResponseObjectMessageType.system;
BubbleStyle currentStyle;
if(element.messageType == GetRoomResponseObjectMessageType.comment) {
@ -84,38 +86,85 @@ class _ChatViewState extends State<ChatView> {
currentStyle = styleSystem;
}
var _actorTextStyle = TextStyle(color: Theme.of(context).primaryColor, fontWeight: FontWeight.bold);
messages.add(Bubble(
margin: BubbleEdges.only(bottom: element == data.getChatResponse.sortByTimestamp().last ? 20 : 0),
style: currentStyle,
child: Stack(
children: [
Visibility(
visible: showMetadata,
child: Positioned(
top: 0,
left: 0,
child: Text(element.actorDisplayName, style: TextStyle(fontWeight: FontWeight.bold, color: Theme.of(context).primaryColor)),
child: Container(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.9,
minWidth: _textSize(element.actorDisplayName, _actorTextStyle).width,
),
child: Stack(
children: [
Padding(
padding: EdgeInsets.only(bottom: showBubbleTime ? 18 : 0, top: showActorDisplayName ? 18 : 0),
child: FutureBuilder(
future: RichObjectStringProcessor.parseAnyToWidget(element.message, element.messageParameters),
builder: (context, snapshot) {
if(!snapshot.hasData) return const CircularProgressIndicator();
return snapshot.data ?? const Icon(Icons.error);
},
)
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: showMetadata ? 18 : 0),
child: Text(RichObjectStringProcessor.parse(element.message, element.messageParameters)),
),
Visibility(
visible: showMetadata,
child: Positioned(
bottom: 0,
right: 0,
child: Text(
"${Jiffy.unixFromSecondsSinceEpoch(element.timestamp).yMMMMd} - ${Jiffy.unixFromSecondsSinceEpoch(element.timestamp).format("HH:mm")}",
style: TextStyle(color: Theme.of(context).disabledColor),
Visibility(
visible: showActorDisplayName,
child: Positioned(
top: 0,
left: 0,
child: Text(
element.actorDisplayName,
textAlign: TextAlign.start,
style: _actorTextStyle,
),
),
),
),
],
Visibility(
visible: showBubbleTime,
child: Positioned(
bottom: 0,
right: 0,
child: Text(
Jiffy.unixFromSecondsSinceEpoch(element.timestamp).format("HH:mm"),
textAlign: TextAlign.end,
style: const TextStyle(color: Colors.grey, fontSize: 12),
),
),
),
],
),
),
// Stack(
// children: [
// Visibility(
// visible: showMetadata,
// child: Positioned(
// top: 0,
// left: 0,
// child: Expanded(child: Text(element.actorDisplayName, style: TextStyle(fontWeight: FontWeight.bold, color: Theme.of(context).primaryColor))),
// ),
// ),
// Padding(
// padding: EdgeInsets.symmetric(vertical: showMetadata ? 18 : 0),
// child: Text(RichObjectStringProcessor.parse(element.message, element.messageParameters)),
// ),
// Visibility(
// visible: showMetadata,
// child: Positioned(
// bottom: 0,
// right: 0,
// child: Text(
// "${Jiffy.unixFromSecondsSinceEpoch(element.timestamp).yMMMMd} - ${Jiffy.unixFromSecondsSinceEpoch(element.timestamp).format("HH:mm")}",
// style: TextStyle(color: Theme.of(context).disabledColor),
// ),
// ),
// ),
// ],
// ),
));
});
}
@ -127,7 +176,9 @@ class _ChatViewState extends State<ChatView> {
children: [
widget.avatar,
const SizedBox(width: 10),
Text(widget.user.displayName, overflow: TextOverflow.ellipsis, maxLines: 1),
Expanded(
child: Text(widget.room.displayName, overflow: TextOverflow.ellipsis, maxLines: 1),
)
],
),
),
@ -175,7 +226,7 @@ class _ChatViewState extends State<ChatView> {
setState(() {
sending = true;
});
SendMessage(widget.user.token, SendMessageParams(_textBoxController.text)).run().then((value) => {
SendMessage(widget.room.token, SendMessageParams(_textBoxController.text)).run().then((value) => {
Provider.of<ChatProps>(context, listen: false).run(),
_textBoxController.text = "",
setState(() {
@ -196,4 +247,13 @@ class _ChatViewState extends State<ChatView> {
},
);
}
Size _textSize(String text, TextStyle style) {
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 1,
textDirection: TextDirection.ltr)
..layout(minWidth: 0, maxWidth: double.infinity);
return textPainter.size;
}
}