diff --git a/lib/models/conversation_message.dart b/lib/models/conversation_message.dart index 26c54d1..7f3f6e1 100644 --- a/lib/models/conversation_message.dart +++ b/lib/models/conversation_message.dart @@ -22,6 +22,8 @@ class ConversationMessage implements Comparable { assert(timeInsert != null), assert(message != null); + DateTime get date => DateTime.fromMillisecondsSinceEpoch(timeInsert * 1000); + @override int compareTo(other) { return id.compareTo(other.id); diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index b98c97a..c61989c 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -151,6 +151,12 @@ class _ConversationScreenState extends State { }); } + // Check if a message is the last message of a user or not + bool _isLastMessage(int index) { + return index == 0 || + (index > 0 && _messages[index - 1].userID == _messages[index].userID); + } + /// Error handling Widget _buildError() { return buildErrorCard(tr("Could not load the list of messages!")); @@ -165,6 +171,7 @@ class _ConversationScreenState extends State { return ConversationMessageTile( message: _messages.elementAt(i), userInfo: _usersInfo.getUser(_messages[i].userID), + isLastMessage: _isLastMessage(i), ); }), ); diff --git a/lib/ui/tiles/conversation_message_tile.dart b/lib/ui/tiles/conversation_message_tile.dart index dc60871..7550c57 100644 --- a/lib/ui/tiles/conversation_message_tile.dart +++ b/lib/ui/tiles/conversation_message_tile.dart @@ -1,5 +1,7 @@ import 'package:comunic/models/conversation_message.dart'; import 'package:comunic/models/user.dart'; +import 'package:comunic/utils/account_utils.dart'; +import 'package:comunic/utils/date_utils.dart'; import 'package:flutter/material.dart'; /// Conversation message tile @@ -9,14 +11,101 @@ import 'package:flutter/material.dart'; class ConversationMessageTile extends StatelessWidget { final ConversationMessage message; final User userInfo; + final bool isLastMessage; - const ConversationMessageTile({Key key, this.message, this.userInfo}) + const ConversationMessageTile( + {Key key, + @required this.message, + @required this.userInfo, + @required this.isLastMessage}) : assert(message != null), assert(userInfo != null), + assert(isLastMessage != null), super(key: key); + /// Build message date + Widget _buildMessageDate() { + return isLastMessage + ? Container( + margin: EdgeInsets.only(top: 5.0), + child: Text( + dateTimeToString(message.date), + style: TextStyle(color: Colors.black54, fontSize: 12.0), + ), + ) + : Container(); + } + + /// Build a message of the current user + Widget _buildRightMessage() { + return Container( + margin: EdgeInsets.only(right: 10.0, bottom: isLastMessage ? 20.0 : 10.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Column( + children: [ + // Text message + Container( + child: Text( + message.message, + textAlign: TextAlign.justify, + style: TextStyle(color: Colors.white), + ), + padding: EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0), + width: 200.0, + decoration: BoxDecoration( + color: Colors.blueAccent, + borderRadius: BorderRadius.circular(8.0), + ), + ), + + // Date + _buildMessageDate() + ], + ), + ], + ), + ); + // Text + } + + /// Build a message of a peer user + Widget _buildLeftMessage() { + return Container( + margin: EdgeInsets.only(left: 10.0, bottom: isLastMessage ? 20.0 : 10.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Column( + children: [ + // Text message + Container( + child: Text( + message.message, + textAlign: TextAlign.justify, + ), + padding: EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0), + width: 200.0, + decoration: BoxDecoration( + color: Colors.black12, + borderRadius: BorderRadius.circular(8.0), + ), + ), + + // Date + _buildMessageDate() + ], + ), + ], + ), + ); + } + @override Widget build(BuildContext context) { - return Text(message.message); + return userID() == message.userID + ? _buildRightMessage() + : _buildLeftMessage(); } } diff --git a/lib/utils/date_utils.dart b/lib/utils/date_utils.dart index e2de772..d08b38f 100644 --- a/lib/utils/date_utils.dart +++ b/lib/utils/date_utils.dart @@ -46,3 +46,9 @@ String diffTimeToStr(int amount) { String diffTimeFromNowToStr(int date) { return diffTimeToStr(time() - date); } + + +/// Return properly formatted date and time +String dateTimeToString(DateTime time) { + return "${time.day}:${time.month}:${time.year} ${time.hour}:${time.minute}"; +} \ No newline at end of file