Further restructuring of chatBubble.dart, added chatMessage.dart

This commit is contained in:
Elias Müller 2023-02-25 14:11:46 +01:00
parent 5098c9d03a
commit 08a2d95882
4 changed files with 67 additions and 50 deletions

View File

@ -1,12 +1,8 @@
import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:marianum_mobile/api/marianumcloud/talk/chat/getChatResponse.dart';
import 'package:shared_preferences/shared_preferences.dart';
class RichObjectStringProcessor {
static String parseTextPreview(String message, Map<String, RichObjectString>? data) {
static String parseToString(String message, Map<String, RichObjectString>? data) {
if(data == null) return message;
data.forEach((key, value) {
@ -15,31 +11,4 @@ class RichObjectStringProcessor {
return message;
}
static Future<Widget> parseAnyToWidget(String message, Map<String, RichObjectString>? data) async {
if(data == null) return SelectableText(message);
if(!message.contains(RegExp("{file}"))) return Text(parseTextPreview(message, data));
SharedPreferences preferences = await SharedPreferences.getInstance();
Widget? back;
data.forEach((key, value) {
back = CachedNetworkImage(
errorWidget: (context, url, error) {
return Text("Datei: ${value.name}", style: const TextStyle(fontWeight: FontWeight.bold));
},
alignment: Alignment.center,
placeholder: (context, url) {
return const Padding(padding: EdgeInsets.all(10), child: CircularProgressIndicator());
},
fadeInDuration: const Duration(seconds: 1),
imageUrl: "https://cloud.marianum-fulda.de/core/preview?fileId=${value.id}&x=100&y=-1&a=1",
httpHeaders: {
"Authorization": "Basic ${base64.encode(utf8.encode("${preferences.getString("username")}:${preferences.getString("password")}"))}"
},
);
});
return back ?? Text("NOPE");
}
}

View File

@ -2,8 +2,8 @@ import 'package:bubble/bubble.dart';
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';
import 'package:marianum_mobile/api/marianumcloud/talk/chat/getChatResponse.dart';
import 'package:marianum_mobile/screen/pages/talk/chatMessage.dart';
import '../../../api/marianumcloud/talk/chat/richObjectStringProcessor.dart';
import '../../../api/marianumcloud/talk/room/getRoomResponse.dart';
class ChatBubble {
@ -11,7 +11,7 @@ class ChatBubble {
color: Color(0xffd4eaf4),
borderWidth: 1,
elevation: 2,
margin: BubbleEdges.only(top: 15),
margin: BubbleEdges.only(bottom: 15),
alignment: Alignment.center,
);
@ -21,7 +21,7 @@ class ChatBubble {
color: seamless ? Colors.transparent : Colors.white,
borderWidth: seamless ? 0 : 1,
elevation: seamless ? 0 : 1,
margin: const BubbleEdges.only(top: 15, left: 10, right: 50),
margin: const BubbleEdges.only(bottom: 15, left: 10, right: 50),
alignment: Alignment.topLeft,
);
}
@ -32,7 +32,7 @@ class ChatBubble {
color: seamless ? Colors.transparent : const Color(0xffd9fdd3),
borderWidth: seamless ? 0 : 1,
elevation: seamless ? 0 : 1,
margin: const BubbleEdges.only(top: 15, right: 10, left: 50),
margin: const BubbleEdges.only(bottom: 15, right: 10, left: 50),
alignment: Alignment.topRight,
);
}
@ -41,50 +41,48 @@ class ChatBubble {
bool isSender;
GetChatResponseObject bubbleData;
GetRoomResponseObject chatData;
late ChatMessage message;
ChatBubble({
required this.context,
required this.isSender,
required this.bubbleData,
required this.chatData,
});
}) {
message = ChatMessage(originalMessage: bubbleData.message, originalData: bubbleData.messageParameters);
}
BubbleStyle getStyle() {
BubbleStyle currentStyle;
if(bubbleData.messageType == GetRoomResponseObjectMessageType.comment) {
if(isSender) {
currentStyle = getStyleSelf(bubbleData.messageParameters?.containsKey("file") ?? false);
return getStyleSelf(message.containsFile);
} else {
currentStyle = getStyleOther(bubbleData.messageParameters?.containsKey("file") ?? false);
return getStyleOther(message.containsFile);
}
} else {
currentStyle = styleSystem;
return styleSystem;
}
return currentStyle;
}
Bubble generateBubble() {
bool showActorDisplayName = bubbleData.messageType == GetRoomResponseObjectMessageType.comment && chatData.type != GetRoomResponseObjectConversationType.oneToOne;
bool showBubbleTime = bubbleData.messageType != GetRoomResponseObjectMessageType.system;
var _actorTextStyle = TextStyle(color: Theme.of(context).primaryColor, fontWeight: FontWeight.bold);
var actorTextStyle = TextStyle(color: Theme.of(context).primaryColor, fontWeight: FontWeight.bold);
return Bubble(
margin: BubbleEdges.only(/*bottom: element == data.getChatResponse.sortByTimestamp().last ? 20 : 0*/),
style: getStyle(),
child: Container(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.9,
minWidth: showActorDisplayName ? _textSize(bubbleData.actorDisplayName, _actorTextStyle).width : 30,
minWidth: showActorDisplayName ? _textSize(bubbleData.actorDisplayName, actorTextStyle).width : 30,
),
child: Stack(
children: [
Padding(
padding: EdgeInsets.only(bottom: showBubbleTime ? 18 : 0, top: showActorDisplayName ? 18 : 0),
child: FutureBuilder(
future: RichObjectStringProcessor.parseAnyToWidget(bubbleData.message, bubbleData.messageParameters),
future: message.getWidget(),
builder: (context, snapshot) {
if(!snapshot.hasData) return const CircularProgressIndicator();
return snapshot.data ?? const Icon(Icons.error);
@ -99,7 +97,7 @@ class ChatBubble {
child: Text(
bubbleData.actorDisplayName,
textAlign: TextAlign.start,
style: _actorTextStyle,
style: actorTextStyle,
),
),
),

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.parseTextPreview(chatRoom.lastMessage.message.replaceAll("\n", " "), chatRoom.lastMessage.messageParameters)}", overflow: TextOverflow.ellipsis),
subtitle: Text("${Jiffy.unixFromSecondsSinceEpoch(chatRoom.lastMessage.timestamp).fromNow()}: ${RichObjectStringProcessor.parseToString(chatRoom.lastMessage.message.replaceAll("\n", " "), chatRoom.lastMessage.messageParameters)}", overflow: TextOverflow.ellipsis),
trailing: Visibility(
visible: chatRoom.unreadMessages > 0,
child: Container(

View File

@ -0,0 +1,50 @@
import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:marianum_mobile/api/marianumcloud/talk/chat/richObjectStringProcessor.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../api/marianumcloud/talk/chat/getChatResponse.dart';
class ChatMessage {
String originalMessage;
Map<String, RichObjectString>? originalData;
RichObjectString? file;
String content = "";
bool get containsFile => file != null;
ChatMessage({required this.originalMessage, this.originalData}) {
if(originalData?.containsKey("file") ?? false) {
file = originalData?['file'];
content = file?.name ?? "Datei";
} else {
content = RichObjectStringProcessor.parseToString(originalMessage, originalData);
}
}
Future<Widget> getWidget() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
if(file == null) {
return SelectableText(content);
}
return CachedNetworkImage(
errorWidget: (context, url, error) {
return Text("Datei: ${file!.name}", style: const TextStyle(fontWeight: FontWeight.bold));
},
alignment: Alignment.center,
placeholder: (context, url) {
return const Padding(padding: EdgeInsets.all(10), child: CircularProgressIndicator());
},
fadeInDuration: const Duration(seconds: 1),
imageUrl: "https://cloud.marianum-fulda.de/core/preview?fileId=${file!.id}&x=100&y=-1&a=1",
httpHeaders: {
"Authorization": "Basic ${base64.encode(utf8.encode("${preferences.getString("username")}:${preferences.getString("password")}"))}"
},
);
}
}