1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 04:48:51 +00:00

Display text messages

This commit is contained in:
Pierre HUBERT 2019-04-26 08:58:18 +02:00
parent 05f7396c98
commit 051654d7d5
4 changed files with 106 additions and 2 deletions

View File

@ -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);

View File

@ -151,6 +151,12 @@ class _ConversationScreenState extends State<ConversationScreen> {
});
}
// 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<ConversationScreen> {
return ConversationMessageTile(
message: _messages.elementAt(i),
userInfo: _usersInfo.getUser(_messages[i].userID),
isLastMessage: _isLastMessage(i),
);
}),
);

View File

@ -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: <Widget>[
Column(
children: <Widget>[
// 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: <Widget>[
Column(
children: <Widget>[
// 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();
}
}

View File

@ -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}";
}