mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Send message
This commit is contained in:
parent
e2202a4794
commit
3f75c565ca
@ -6,12 +6,15 @@ import 'package:comunic/lists/users_list.dart';
|
|||||||
import 'package:comunic/models/api_request.dart';
|
import 'package:comunic/models/api_request.dart';
|
||||||
import 'package:comunic/models/conversation.dart';
|
import 'package:comunic/models/conversation.dart';
|
||||||
import 'package:comunic/models/conversation_message.dart';
|
import 'package:comunic/models/conversation_message.dart';
|
||||||
|
import 'package:comunic/models/new_conversation_message.dart';
|
||||||
import 'package:comunic/utils/account_utils.dart';
|
import 'package:comunic/utils/account_utils.dart';
|
||||||
|
|
||||||
/// Conversation helper
|
/// Conversation helper
|
||||||
///
|
///
|
||||||
/// @author Pierre HUBERT
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
|
enum SendMessageResult { SUCCESS, MESSAGE_REJECTED, FAILED }
|
||||||
|
|
||||||
class ConversationsHelper {
|
class ConversationsHelper {
|
||||||
final ConversationsDatabaseHelper _conversationsDatabaseHelper =
|
final ConversationsDatabaseHelper _conversationsDatabaseHelper =
|
||||||
ConversationsDatabaseHelper();
|
ConversationsDatabaseHelper();
|
||||||
@ -156,4 +159,23 @@ class ConversationsHelper {
|
|||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send a new message to the server
|
||||||
|
Future<SendMessageResult> sendMessage(NewConversationMessage message) async {
|
||||||
|
final response = await APIRequest(
|
||||||
|
uri: "conversations/sendMessage",
|
||||||
|
needLogin: true,
|
||||||
|
args: {
|
||||||
|
"conversationID": message.conversationID.toString(),
|
||||||
|
"message": message.message
|
||||||
|
},
|
||||||
|
).exec();
|
||||||
|
|
||||||
|
if(response.code == 401)
|
||||||
|
return SendMessageResult.MESSAGE_REJECTED;
|
||||||
|
else if(response.code != 200)
|
||||||
|
return SendMessageResult.FAILED;
|
||||||
|
|
||||||
|
return SendMessageResult.SUCCESS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
17
lib/models/new_conversation_message.dart
Normal file
17
lib/models/new_conversation_message.dart
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
/// New conversation message model
|
||||||
|
///
|
||||||
|
/// This model is used to transfer a conversation message to send in the application
|
||||||
|
///
|
||||||
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
|
class NewConversationMessage {
|
||||||
|
final int conversationID;
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
NewConversationMessage({
|
||||||
|
@required this.conversationID,
|
||||||
|
@required this.message,
|
||||||
|
}) : assert(conversationID != null);
|
||||||
|
}
|
@ -2,6 +2,7 @@ import 'package:comunic/helpers/conversations_helper.dart';
|
|||||||
import 'package:comunic/helpers/users_helper.dart';
|
import 'package:comunic/helpers/users_helper.dart';
|
||||||
import 'package:comunic/lists/conversation_messages_list.dart';
|
import 'package:comunic/lists/conversation_messages_list.dart';
|
||||||
import 'package:comunic/lists/users_list.dart';
|
import 'package:comunic/lists/users_list.dart';
|
||||||
|
import 'package:comunic/models/new_conversation_message.dart';
|
||||||
import 'package:comunic/ui/tiles/conversation_message_tile.dart';
|
import 'package:comunic/ui/tiles/conversation_message_tile.dart';
|
||||||
import 'package:comunic/utils/intl_utils.dart';
|
import 'package:comunic/utils/intl_utils.dart';
|
||||||
import 'package:comunic/utils/list_utils.dart';
|
import 'package:comunic/utils/list_utils.dart';
|
||||||
@ -26,11 +27,17 @@ class ConversationScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _ConversationScreenState extends State<ConversationScreen> {
|
class _ConversationScreenState extends State<ConversationScreen> {
|
||||||
|
//Helpers
|
||||||
final ConversationsHelper _conversationsHelper = ConversationsHelper();
|
final ConversationsHelper _conversationsHelper = ConversationsHelper();
|
||||||
final UsersHelper _usersHelper = UsersHelper();
|
final UsersHelper _usersHelper = UsersHelper();
|
||||||
|
|
||||||
|
// Class members
|
||||||
ConversationMessagesList _messages;
|
ConversationMessagesList _messages;
|
||||||
UsersList _usersInfo = UsersList();
|
UsersList _usersInfo = UsersList();
|
||||||
ErrorLevel _error = ErrorLevel.NONE;
|
ErrorLevel _error = ErrorLevel.NONE;
|
||||||
|
bool _isMessageValid = false;
|
||||||
|
bool _isSendingMessage = false;
|
||||||
|
TextEditingController _textEditingController = TextEditingController();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeDependencies() {
|
void didChangeDependencies() {
|
||||||
@ -40,6 +47,8 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
|
|
||||||
void _setError(ErrorLevel err) => setState(() => _error = err);
|
void _setError(ErrorLevel err) => setState(() => _error = err);
|
||||||
|
|
||||||
|
void _setSending(bool sending) => setState(() => _isSendingMessage = sending);
|
||||||
|
|
||||||
/// Method called when an error occurred while loading messages
|
/// Method called when an error occurred while loading messages
|
||||||
void _errorLoading() {
|
void _errorLoading() {
|
||||||
_setError(_messages == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR);
|
_setError(_messages == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR);
|
||||||
@ -68,6 +77,41 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
_messages = messages;
|
_messages = messages;
|
||||||
else
|
else
|
||||||
_messages.addAll(messages);
|
_messages.addAll(messages);
|
||||||
|
|
||||||
|
_messages.sort();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a new message
|
||||||
|
Future<void> _submitMessage(BuildContext context, String content) async {
|
||||||
|
//Send the message
|
||||||
|
_setSending(true);
|
||||||
|
final result = await _conversationsHelper.sendMessage(
|
||||||
|
NewConversationMessage(
|
||||||
|
conversationID: widget.conversationID, message: content));
|
||||||
|
_setSending(false);
|
||||||
|
|
||||||
|
//Check the result of the operation
|
||||||
|
if (result == SendMessageResult.SUCCESS)
|
||||||
|
_clearSendMessageForm();
|
||||||
|
else
|
||||||
|
Scaffold.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
result == SendMessageResult.MESSAGE_REJECTED
|
||||||
|
? tr("Message rejected by the server!")
|
||||||
|
: tr("Could not send message!"),
|
||||||
|
),
|
||||||
|
duration: Duration(milliseconds: 500),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear send message form
|
||||||
|
void _clearSendMessageForm() {
|
||||||
|
setState(() {
|
||||||
|
_textEditingController = TextEditingController();
|
||||||
|
_isMessageValid = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,19 +120,85 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
return buildErrorCard(tr("Could not load the list of messages!"));
|
return buildErrorCard(tr("Could not load the list of messages!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
/// Messages list
|
||||||
Widget build(BuildContext context) {
|
Widget _buildMessagesList() {
|
||||||
if (_error == ErrorLevel.MAJOR) return _buildError();
|
return Expanded(
|
||||||
|
child: ListView.builder(
|
||||||
if (_messages == null) return buildCenteredProgressBar();
|
|
||||||
|
|
||||||
return ListView.builder(
|
|
||||||
itemCount: _messages.length,
|
itemCount: _messages.length,
|
||||||
itemBuilder: (c, i) {
|
itemBuilder: (c, i) {
|
||||||
return ConversationMessageTile(
|
return ConversationMessageTile(
|
||||||
message: _messages.elementAt(i),
|
message: _messages.elementAt(i),
|
||||||
userInfo: _usersInfo.getUser(_messages[i].userID),
|
userInfo: _usersInfo.getUser(_messages[i].userID),
|
||||||
);
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send message from
|
||||||
|
Widget _buildSendMessageForm() {
|
||||||
|
return new Container(
|
||||||
|
margin: const EdgeInsets.symmetric(horizontal: 8.0),
|
||||||
|
child: new Row(
|
||||||
|
children: <Widget>[
|
||||||
|
// Image area
|
||||||
|
/*new Container(
|
||||||
|
margin: new EdgeInsets.symmetric(horizontal: 4.0),
|
||||||
|
child: new IconButton(
|
||||||
|
icon: new Icon(
|
||||||
|
Icons.photo_camera,
|
||||||
|
color: Theme.of(context).accentColor,
|
||||||
|
),
|
||||||
|
onPressed: () async {}),
|
||||||
|
),*/
|
||||||
|
|
||||||
|
// Message area
|
||||||
|
new Flexible(
|
||||||
|
child: new TextField(
|
||||||
|
controller: _textEditingController,
|
||||||
|
onChanged: (String messageText) {
|
||||||
|
setState(() {
|
||||||
|
_isMessageValid = messageText.length > 4;
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
onSubmitted:
|
||||||
|
_isMessageValid ? (s) => _submitMessage(context, s) : null,
|
||||||
|
decoration:
|
||||||
|
new InputDecoration.collapsed(hintText: tr("Send a message")),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Send button
|
||||||
|
new Container(
|
||||||
|
margin: const EdgeInsets.symmetric(horizontal: 4.0),
|
||||||
|
child: new IconButton(
|
||||||
|
icon: new Icon(
|
||||||
|
Icons.send,
|
||||||
|
color: !_isSendingMessage && _isMessageValid
|
||||||
|
? Theme.of(context).accentColor
|
||||||
|
: Theme.of(context).disabledColor,
|
||||||
|
),
|
||||||
|
onPressed: !_isSendingMessage && _isMessageValid
|
||||||
|
? () => _submitMessage(context, _textEditingController.text)
|
||||||
|
: null,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
if (_error == ErrorLevel.MAJOR) return _buildError();
|
||||||
|
|
||||||
|
if (_messages == null) return buildCenteredProgressBar();
|
||||||
|
|
||||||
|
return Column(
|
||||||
|
children: <Widget>[
|
||||||
|
_buildMessagesList(),
|
||||||
|
Divider(),
|
||||||
|
_buildSendMessageForm()
|
||||||
|
],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user