From 7291f1656cb04c0602be2606f7b44e976d6123f7 Mon Sep 17 00:00:00 2001 From: otsmr Date: Sat, 8 Feb 2025 17:07:48 +0100 Subject: [PATCH] delete >24h messages & remove messageKind where possible --- README.md | 5 +- lib/src/model/json/message.dart | 2 +- lib/src/model/messages_model.dart | 52 ++++--- .../views/chats/chat_item_details_view.dart | 132 +++++++++--------- 4 files changed, 104 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index 1012e83..b091fb8 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,11 @@ Don't be lonely, get twonly! Send pictures to a friend in real time and be sure ## TODOS bevor first beta -- MessageKind -> Ausbauen? -- Nachrichten nach 24h Stunden löschen - Pro Invitation codes - Push Notification (Android) +- Contact view page +- Verify contact view +- Context Menu - Settings - Notification - Real deployment aufsetzen, direkt auf Netcup? diff --git a/lib/src/model/json/message.dart b/lib/src/model/json/message.dart index 4296b05..17e8e56 100644 --- a/lib/src/model/json/message.dart +++ b/lib/src/model/json/message.dart @@ -134,7 +134,7 @@ class MediaMessageContent extends MessageContent { } class TextMessageContent extends MessageContent { - final String text; + String text; TextMessageContent({required this.text}); static TextMessageContent fromJson(Map json) { diff --git a/lib/src/model/messages_model.dart b/lib/src/model/messages_model.dart index a08e040..b3677c4 100644 --- a/lib/src/model/messages_model.dart +++ b/lib/src/model/messages_model.dart @@ -13,7 +13,6 @@ class DbMessage { required this.messageId, required this.messageOtherId, required this.otherUserId, - required this.messageKind, required this.messageContent, required this.messageOpenedAt, required this.messageAcknowledgeByUser, @@ -26,7 +25,6 @@ class DbMessage { // is this null then the message was sent from the user itself int? messageOtherId; int otherUserId; - MessageKind messageKind; MessageContent messageContent; DateTime? messageOpenedAt; bool messageAcknowledgeByUser; @@ -42,7 +40,8 @@ class DbMessage { bool get messageReceived => messageOtherId != null; bool isMedia() { - return messageKind == MessageKind.image || messageKind == MessageKind.video; + return messageContent is TextMessageContent || + messageContent is MediaMessageContent; } MessageSendState getSendState() { @@ -276,17 +275,19 @@ class DbMessages extends CvModelBase { return messages[0]; } - static Future _updateByMessageId( - int messageId, Map data) async { + static Future _updateByMessageId(int messageId, Map data, + {bool notifyFlutterState = true}) async { await dbProvider.db!.update( tableName, data, where: "$columnMessageId = ?", whereArgs: [messageId], ); - int? fromUserId = await getFromUserIdByMessageId(messageId); - if (fromUserId != null) { - globalCallBackOnMessageChange(fromUserId); + if (notifyFlutterState) { + int? fromUserId = await getFromUserIdByMessageId(messageId); + if (fromUserId != null) { + globalCallBackOnMessageChange(fromUserId); + } } } @@ -354,25 +355,39 @@ class DbMessages extends CvModelBase { List get fields => [messageId, messageKind, messageContentJson, messageOpenedAt, sendAt]; + // TODO: The message meta is needed to maintain the flame. Delete if not. + // This function should calculate if this message is needed for the flame calculation and delete the message complete and not only + // the message content. + static Future deleteTextContent( + int messageId, TextMessageContent oldMessage) async { + oldMessage.text = ""; + Map data = { + columnMessageContentJson: jsonEncode(oldMessage.toJson()), + }; + await _updateByMessageId(messageId, data, notifyFlutterState: false); + } + static Future> convertToDbMessage( List fromDb) async { try { List parsedUsers = []; for (int i = 0; i < fromDb.length; i++) { dynamic messageOpenedAt = fromDb[i][columnMessageOpenedAt]; - if (messageOpenedAt != null) { - messageOpenedAt = DateTime.tryParse(fromDb[i][columnMessageOpenedAt]); - // if (messageOpenedAt != null) { - // if (messageOpenedAt.difference()) { - // } - // } - } - int? messageOtherId = fromDb[i][columnMessageOtherId]; MessageContent content = MessageContent.fromJson( jsonDecode(fromDb[i][columnMessageContentJson])); - MessageKind messageKind = - MessageKindExtension.fromIndex(fromDb[i][columnMessageKind]); + + var tmp = content; + if (tmp is TextMessageContent && messageOpenedAt != null) { + messageOpenedAt = DateTime.tryParse(fromDb[i][columnMessageOpenedAt]); + if (messageOpenedAt != null) { + if ((DateTime.now()).difference(messageOpenedAt).inHours >= 24) { + deleteTextContent(fromDb[i][columnMessageId], tmp); + } + } + } + int? messageOtherId = fromDb[i][columnMessageOtherId]; + bool isDownloaded = true; if (messageOtherId != null) { if (content is MediaMessageContent) { @@ -386,7 +401,6 @@ class DbMessages extends CvModelBase { messageId: fromDb[i][columnMessageId], messageOtherId: messageOtherId, otherUserId: fromDb[i][columnOtherUserId], - messageKind: messageKind, messageContent: content, isDownloaded: isDownloaded, messageOpenedAt: messageOpenedAt, diff --git a/lib/src/views/chats/chat_item_details_view.dart b/lib/src/views/chats/chat_item_details_view.dart index 66931e7..f51dfd0 100644 --- a/lib/src/views/chats/chat_item_details_view.dart +++ b/lib/src/views/chats/chat_item_details_view.dart @@ -38,75 +38,69 @@ class ChatListEntry extends StatelessWidget { Widget child = Container(); - switch (message.messageKind) { - case MessageKind.textMessage: - if (content is TextMessageContent) { - child = Container( - constraints: BoxConstraints( - maxWidth: MediaQuery.of(context).size.width * 0.8, - ), - padding: EdgeInsets.symmetric( - vertical: 4, - horizontal: 10), // Add some padding around the text - decoration: BoxDecoration( - color: right - ? const Color.fromARGB(107, 124, 77, 255) - : const Color.fromARGB( - 83, 68, 137, 255), // Set the background color - borderRadius: BorderRadius.circular(12.0), // Set border radius - ), - child: Text( - content.text, - style: TextStyle( - color: Colors.white, // Set text color for contrast - fontSize: 17, - ), - textAlign: TextAlign.left, // Center the text - ), - ); - } - break; - case MessageKind.image: - Color color = message.messageContent - .getColor(Theme.of(context).colorScheme.primary); - child = GestureDetector( - onTap: () { - if (state == MessageSendState.received && !isDownloading) { - if (message.isDownloaded) { - Navigator.push( - context, - MaterialPageRoute(builder: (context) { - return MediaViewerView(user, message); - }), - ); - } else { - tryDownloadMedia(token, force: true); - } + if (content is TextMessageContent) { + child = Container( + constraints: BoxConstraints( + maxWidth: MediaQuery.of(context).size.width * 0.8, + ), + padding: EdgeInsets.symmetric( + vertical: 4, horizontal: 10), // Add some padding around the text + decoration: BoxDecoration( + color: right + ? const Color.fromARGB(107, 124, 77, 255) + : const Color.fromARGB( + 83, 68, 137, 255), // Set the background color + borderRadius: BorderRadius.circular(12.0), // Set border radius + ), + child: Text( + content.text, + style: TextStyle( + color: Colors.white, // Set text color for contrast + fontSize: 17, + ), + textAlign: TextAlign.left, // Center the text + ), + ); + } else if (content is MediaMessageContent && !content.isVideo) { + Color color = message.messageContent + .getColor(Theme.of(context).colorScheme.primary); + child = GestureDetector( + onTap: () { + if (state == MessageSendState.received && !isDownloading) { + if (message.isDownloaded) { + Navigator.push( + context, + MaterialPageRoute(builder: (context) { + return MediaViewerView(user, message); + }), + ); + } else { + tryDownloadMedia(token, force: true); } - }, - child: Container( - padding: EdgeInsets.all(10), - width: 150, - decoration: BoxDecoration( - border: Border.all( - color: color, // Set the background color - width: 1.0, // Set the border width here - ), - borderRadius: BorderRadius.circular(12.0), // Set border radius + } + }, + child: Container( + padding: EdgeInsets.all(10), + width: 150, + decoration: BoxDecoration( + border: Border.all( + color: color, // Set the background color + width: 1.0, // Set the border width here ), - child: Align( - alignment: Alignment.centerRight, - child: MessageSendStateIcon( - message, - mainAxisAlignment: - right ? MainAxisAlignment.center : MainAxisAlignment.center, - ), + borderRadius: BorderRadius.circular(12.0), // Set border radius + ), + child: Align( + alignment: Alignment.centerRight, + child: MessageSendStateIcon( + message, + mainAxisAlignment: + right ? MainAxisAlignment.center : MainAxisAlignment.center, ), ), - ); - default: - return Container(); + ), + ); } + return Align( alignment: right ? Alignment.centerRight : Alignment.centerLeft, child: Padding( @@ -164,7 +158,7 @@ class _ChatItemDetailsViewState extends State { if (updateOpenStatus) { _messages.where((x) => x.messageOpenedAt == null).forEach((message) { if (message.messageOtherId != null && - message.messageKind == MessageKind.textMessage) { + message.messageContent is TextMessageContent) { if (!alreadyReportedOpened.contains(message.messageOtherId!)) { userOpenedOtherMessage( message.otherUserId, message.messageOtherId!); @@ -210,6 +204,14 @@ class _ChatItemDetailsViewState extends State { (_messages[i - 1].messageOtherId != null && _messages[i].messageOtherId != null); } + if (_messages[i].messageOpenedAt != null) { + if ((DateTime.now()) + .difference(_messages[i].messageOpenedAt!) + .inHours >= + 24) { + return Container(); + } + } return ChatListEntry( _messages[i], widget.user,