1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 14:59:22 +00:00
comunicmobile/lib/ui/screens/conversation_screen.dart

488 lines
15 KiB
Dart
Raw Normal View History

2019-04-27 07:52:16 +00:00
import 'dart:async';
2019-04-25 06:56:16 +00:00
import 'package:comunic/helpers/conversations_helper.dart';
2020-04-19 11:58:24 +00:00
import 'package:comunic/helpers/events_helper.dart';
2019-04-25 06:56:16 +00:00
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversation_messages_list.dart';
import 'package:comunic/lists/users_list.dart';
2021-03-10 23:23:11 +00:00
import 'package:comunic/models/conversation.dart';
2019-05-04 06:58:14 +00:00
import 'package:comunic/models/conversation_message.dart';
2019-04-25 07:48:52 +00:00
import 'package:comunic/models/new_conversation_message.dart';
2021-03-11 16:27:20 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
2019-04-25 06:56:16 +00:00
import 'package:comunic/ui/tiles/conversation_message_tile.dart';
import 'package:comunic/ui/tiles/server_conversation_message_tile.dart';
2020-04-19 11:42:47 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
2019-04-27 16:29:30 +00:00
import 'package:comunic/ui/widgets/scroll_watcher.dart';
2019-04-25 09:13:02 +00:00
import 'package:comunic/utils/files_utils.dart';
2019-04-25 06:56:16 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/list_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Conversation screen
///
/// @author Pierre HUBERT
enum ErrorLevel { NONE, MINOR, MAJOR }
2019-04-27 16:29:30 +00:00
enum _OlderMessagesLevel { NONE, LOADING, NO_MORE_AVAILABLE }
2019-04-25 06:56:16 +00:00
class ConversationScreen extends StatefulWidget {
final int conversationID;
const ConversationScreen({Key key, this.conversationID})
: assert(conversationID != null),
super(key: key);
@override
State<StatefulWidget> createState() => _ConversationScreenState();
}
2020-04-19 11:42:47 +00:00
class _ConversationScreenState extends SafeState<ConversationScreen> {
2019-04-25 07:48:52 +00:00
//Helpers
2019-04-25 06:56:16 +00:00
final ConversationsHelper _conversationsHelper = ConversationsHelper();
final UsersHelper _usersHelper = UsersHelper();
2019-04-25 07:48:52 +00:00
// Class members
2021-03-10 23:23:11 +00:00
Conversation _conversation;
2019-04-25 06:56:16 +00:00
ConversationMessagesList _messages;
UsersList _usersInfo = UsersList();
ErrorLevel _error = ErrorLevel.NONE;
2019-04-25 07:48:52 +00:00
bool _isMessageValid = false;
bool _isSendingMessage = false;
TextEditingController _textEditingController = TextEditingController();
2019-04-27 16:29:30 +00:00
ScrollWatcher _scrollController;
_OlderMessagesLevel _loadingOlderMessages = _OlderMessagesLevel.NONE;
@override
void initState() {
super.initState();
2020-04-19 11:42:47 +00:00
_init();
2019-04-27 16:29:30 +00:00
}
2019-04-25 06:56:16 +00:00
@override
2020-04-19 11:42:47 +00:00
void dispose() {
super.dispose();
_deallocate();
2019-04-27 07:52:16 +00:00
}
2019-04-25 06:56:16 +00:00
void _setError(ErrorLevel err) => setState(() => _error = err);
2019-04-25 07:48:52 +00:00
void _setSending(bool sending) => setState(() => _isSendingMessage = sending);
2019-04-27 16:29:30 +00:00
void _setLoadingOlderMessagesState(_OlderMessagesLevel state) => setState(() {
_loadingOlderMessages = state;
});
2019-04-25 06:56:16 +00:00
/// Method called when an error occurred while loading messages
2021-03-10 16:54:41 +00:00
void _errorLoading() =>
_setError(_messages == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR);
2019-04-25 06:56:16 +00:00
/// Load the first conversations
2020-04-19 11:42:47 +00:00
Future<void> _init() async {
_scrollController = ScrollWatcher(onReachBottom: _loadOlderMessages);
2021-03-10 23:23:11 +00:00
_conversation =
await ConversationsHelper().getSingle(widget.conversationID);
2020-04-19 11:42:47 +00:00
// Fetch latest messages
await _loadMessages(false);
await _loadMessages(true);
2019-04-27 07:52:16 +00:00
2020-04-19 11:42:47 +00:00
await _conversationsHelper
.registerConversationEvents(widget.conversationID);
2020-04-19 11:58:24 +00:00
this.listen<NewConversationMessageEvent>((ev) async {
2021-03-10 16:54:41 +00:00
if (ev.msg.convID == widget.conversationID) {
try {
await _conversationsHelper.saveMessage(ev.msg);
await _applyNewMessages(ConversationMessagesList()..add(ev.msg));
} catch (e, s) {
print("Failed to show new message! $e => $s");
_errorLoading();
}
2020-04-19 12:16:35 +00:00
}
});
this.listen<UpdatedConversationMessageEvent>((ev) async {
2021-03-10 16:54:41 +00:00
if (ev.msg.convID == widget.conversationID) {
2020-04-19 12:16:35 +00:00
await _conversationsHelper.saveMessage(ev.msg);
setState(() => _messages.replace(ev.msg));
}
2020-04-19 11:58:24 +00:00
});
2020-04-19 12:29:01 +00:00
this.listen<DeletedConversationMessageEvent>((ev) async {
2021-03-10 16:54:41 +00:00
if (ev.msg.convID == widget.conversationID) {
await _conversationsHelper.removeMessage(ev.msg);
2020-04-19 12:29:01 +00:00
setState(() => _messages.removeMsg(ev.msg.id));
}
});
2020-04-19 11:42:47 +00:00
}
/// Free resources when this conversation widget is no longer required
void _deallocate() {
_conversationsHelper.unregisterConversationEvents(widget.conversationID);
}
2019-04-25 06:56:16 +00:00
/// Load a list of messages
Future<void> _loadMessages(bool online) async {
if (!mounted) return;
2021-03-10 16:54:41 +00:00
try {
//First, get the messages
final messages = await _conversationsHelper.getNewMessages(
2019-04-27 06:51:58 +00:00
conversationID: widget.conversationID,
lastMessageID: _messages == null ? 0 : _messages.lastMessageID,
2021-03-10 16:54:41 +00:00
online: online,
);
2019-04-25 06:56:16 +00:00
2021-03-10 16:54:41 +00:00
// In case we are offline and we did not get any message we do not do
// anything (we wait for the online request)
if (messages.length == 0 && !online) return;
2021-03-10 16:54:41 +00:00
await _applyNewMessages(messages);
} catch (e, s) {
debugPrint("Failed to load messages! $e => $s", wrapWidth: 4096);
2021-03-10 16:54:41 +00:00
_errorLoading();
}
2019-04-27 16:29:30 +00:00
}
/// Get older messages
Future<void> _loadOlderMessages() async {
if (_loadingOlderMessages != _OlderMessagesLevel.NONE ||
_messages == null ||
_messages.length == 0) return;
2021-03-10 16:54:41 +00:00
try {
// Let's start to load older messages
_setLoadingOlderMessagesState(_OlderMessagesLevel.LOADING);
2019-04-27 16:29:30 +00:00
2021-03-10 16:54:41 +00:00
final messages = await _conversationsHelper.getOlderMessages(
conversationID: widget.conversationID,
oldestMessagesID: _messages.firstMessageID);
2019-04-27 16:29:30 +00:00
2021-03-10 16:54:41 +00:00
// Mark as not loading anymore
_setLoadingOlderMessagesState(_OlderMessagesLevel.NONE);
2019-04-27 16:29:30 +00:00
2021-03-10 16:54:41 +00:00
// Check if there is no more unread messages
if (messages.length == 0) {
_setLoadingOlderMessagesState(_OlderMessagesLevel.NO_MORE_AVAILABLE);
return;
}
2019-04-27 16:29:30 +00:00
2021-03-10 16:54:41 +00:00
// Apply the messages
_applyNewMessages(messages);
} catch (e, s) {
print("Failed to load older messages! $e => $s");
2019-04-27 16:29:30 +00:00
_errorLoading();
}
}
/// Apply new messages [messages] must not be null
2021-03-10 16:54:41 +00:00
///
/// Throws in case of failure
2019-04-27 16:29:30 +00:00
Future<void> _applyNewMessages(ConversationMessagesList messages) async {
// We ignore new messages once the area is no longer visible
if (!this.mounted) return;
2019-04-27 16:29:30 +00:00
2019-04-25 06:56:16 +00:00
//Then get information about users
final usersToGet =
findMissingFromSet(_usersInfo.usersID.toSet(), messages.getUsersID());
2019-04-25 06:56:16 +00:00
2021-03-10 16:54:41 +00:00
final users = await _usersHelper.getList(usersToGet);
2019-04-25 06:56:16 +00:00
// Save the new list of messages
setState(() {
_usersInfo.addAll(users);
if (_messages == null)
_messages = messages;
else
_messages.addAll(messages);
2019-04-25 07:48:52 +00:00
2019-04-27 07:52:16 +00:00
//Reverse the order of the messages (if required)
if (messages.length > 0) {
_messages.sort();
final reverse = _messages.reversed;
_messages = ConversationMessagesList();
_messages.addAll(reverse);
}
2019-04-25 07:48:52 +00:00
});
2019-04-27 07:52:16 +00:00
// Remove previous errors
_setError(ErrorLevel.NONE);
2019-04-25 07:48:52 +00:00
}
2019-04-25 09:13:02 +00:00
/// Pick and send an image
Future<void> _sendImage(BuildContext context) async {
final image = await pickImage(context);
2019-04-25 18:14:19 +00:00
if (image == null) return null;
_submitMessage(
context,
NewConversationMessage(
conversationID: widget.conversationID,
message: null,
image: image,
),
);
2019-04-25 18:22:53 +00:00
// In case a message was already written in the input
_updatedText(_textEditingController.text);
2019-04-25 18:14:19 +00:00
}
/// Send a new text message
Future<void> _submitTextMessage(BuildContext context, String content) async {
2019-04-25 18:22:53 +00:00
if (await _submitMessage(
context,
NewConversationMessage(
conversationID: widget.conversationID,
message: content,
)) ==
SendMessageResult.SUCCESS) _clearSendMessageForm();
2019-04-25 09:13:02 +00:00
}
2019-04-25 18:14:19 +00:00
/// Submit a new message
2019-04-25 18:22:53 +00:00
Future<SendMessageResult> _submitMessage(
2019-04-25 18:14:19 +00:00
BuildContext context, NewConversationMessage message) async {
2019-04-25 07:48:52 +00:00
//Send the message
_setSending(true);
2019-04-25 18:14:19 +00:00
final result = await _conversationsHelper.sendMessage(message);
2019-04-25 07:48:52 +00:00
_setSending(false);
//Check the result of the operation
2019-04-25 18:22:53 +00:00
if (result != SendMessageResult.SUCCESS)
2019-04-25 07:48:52 +00:00
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),
),
);
2019-04-25 18:22:53 +00:00
return result;
}
void _updatedText(String text) {
setState(() {
2020-05-09 12:57:59 +00:00
_isMessageValid = text.length > 2;
2019-04-25 18:22:53 +00:00
});
2019-04-25 07:48:52 +00:00
}
/// Clear send message form
void _clearSendMessageForm() {
setState(() {
_textEditingController = TextEditingController();
_isMessageValid = false;
2019-04-25 06:56:16 +00:00
});
}
2019-04-26 09:04:06 +00:00
/// Check if a message is the last message of a user or not
2019-04-26 06:58:18 +00:00
bool _isLastMessage(int index) {
return index == 0 ||
2019-04-26 09:04:06 +00:00
(index > 0 && _messages[index - 1].userID != _messages[index].userID);
}
/// Check if a message is the first message of a user or not
bool _isFirstMessage(int index) {
return index == _messages.length - 1 ||
2019-04-27 06:51:58 +00:00
(index < _messages.length - 1 &&
_messages[index + 1].userID != _messages[index].userID);
2019-04-26 06:58:18 +00:00
}
2019-04-25 06:56:16 +00:00
/// Error handling
Widget _buildError() {
return buildErrorCard(tr("Could not load the list of messages!"));
}
2019-04-27 16:29:30 +00:00
/// Widget shown when loading older messages
Widget _buildLoadingOlderMessage() {
return Container(
padding: EdgeInsets.all(8.0),
child: CircularProgressIndicator(),
);
}
/// Notice shown when there is no messages to show
Widget _buildNoMessagesNotice() {
return Expanded(
child: Center(
child: Text(tr("There is no message yet in this converation.")),
),
);
}
2019-04-25 07:48:52 +00:00
/// Messages list
Widget _buildMessagesList() {
return Expanded(
child: ListView.builder(
2019-04-27 16:29:30 +00:00
controller: _scrollController,
2019-04-27 06:51:58 +00:00
reverse: true,
2019-04-25 07:48:52 +00:00
itemCount: _messages.length,
itemBuilder: (c, i) {
return _messages[i].isServerMessage
? ServerConversationMessageTile(
message: _messages[i].serverMessage,
users: _usersInfo,
)
: ConversationMessageTile(
2021-03-10 23:23:11 +00:00
conversation: _conversation,
message: _messages.elementAt(i),
userInfo: _usersInfo.getUser(_messages[i].userID),
isLastMessage: _isLastMessage(i),
isFirstMessage: _isFirstMessage(i),
2021-03-11 16:27:20 +00:00
onRequestMessageStats: _requestMessageStats,
onRequestMessageUpdate: _updateMessage,
onRequestMessageDelete: _deleteMessage,
);
2019-04-25 07:48:52 +00:00
}),
);
}
2019-04-27 16:29:30 +00:00
/// Send message form
2019-04-25 07:48:52 +00:00
Widget _buildSendMessageForm() {
return new Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: new Row(
children: <Widget>[
// Image area
2019-04-25 09:13:02 +00:00
new Container(
2019-04-25 18:14:19 +00:00
margin: new EdgeInsets.symmetric(horizontal: 4.0),
child: new IconButton(
2019-04-25 18:22:53 +00:00
icon: new Icon(
Icons.photo_camera,
color: _isSendingMessage
? Theme.of(context).disabledColor
: Theme.of(context).accentColor,
),
onPressed: () => _sendImage(context),
),
2019-04-25 18:14:19 +00:00
),
2019-04-25 07:48:52 +00:00
// Message area
new Flexible(
child: new TextField(
2019-04-27 16:29:30 +00:00
keyboardType: TextInputType.text,
2019-04-27 08:13:29 +00:00
maxLines: null,
maxLength: 200,
maxLengthEnforced: true,
// Show max length only when there is some text already typed
2019-05-20 07:20:11 +00:00
buildCounter: smartInputCounterWidgetBuilder,
2019-04-25 18:22:53 +00:00
enabled: !_isSendingMessage,
2019-04-25 07:48:52 +00:00
controller: _textEditingController,
2019-04-25 18:22:53 +00:00
onChanged: _updatedText,
2019-04-25 18:14:19 +00:00
onSubmitted: _isMessageValid
? (s) => _submitTextMessage(context, s)
: null,
2019-04-27 08:13:29 +00:00
decoration: new InputDecoration.collapsed(
hintText: tr("Send a message"),
),
2019-04-25 07:48:52 +00:00
),
),
// 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
2019-04-25 18:14:19 +00:00
? () =>
_submitTextMessage(context, _textEditingController.text)
2019-04-25 07:48:52 +00:00
: null,
),
),
],
),
);
}
2019-04-25 06:56:16 +00:00
@override
Widget build(BuildContext context) {
if (_error == ErrorLevel.MAJOR) return _buildError();
if (_messages == null) return buildCenteredProgressBar();
2019-04-25 07:48:52 +00:00
return Column(
children: <Widget>[
Container(
child: _error == ErrorLevel.MINOR ? _buildError() : null,
),
2019-04-27 16:29:30 +00:00
Container(
child: _loadingOlderMessages == _OlderMessagesLevel.LOADING
? _buildLoadingOlderMessage()
: null,
),
_messages.length == 0 ? _buildNoMessagesNotice() : _buildMessagesList(),
2019-04-25 07:48:52 +00:00
Divider(),
_buildSendMessageForm()
],
);
2019-04-25 06:56:16 +00:00
}
2019-05-04 06:58:14 +00:00
2021-03-11 16:27:20 +00:00
/// Request message statistics
void _requestMessageStats(ConversationMessage message) async {
MainController.of(context)
.openConversationMessageStats(_conversation, message);
}
2019-05-04 08:24:38 +00:00
/// Request message content update
Future<void> _updateMessage(ConversationMessage message) async {
final newContent = await askUserString(
context: context,
title: tr("Update message"),
message: tr("Please enter new message content:"),
2020-04-16 13:10:47 +00:00
defaultValue: message.message.content,
2019-05-04 08:24:38 +00:00
hint: tr("New message"));
if (newContent == null) return;
if (!await _conversationsHelper.updateMessage(message.id, newContent)) {
showSimpleSnack(context, tr("Could not update message content!"));
return;
}
}
2019-05-04 06:58:14 +00:00
/// Request message deletion
Future<void> _deleteMessage(ConversationMessage message) async {
final choice = await showDialog<bool>(
context: context,
builder: (c) => AlertDialog(
title: Text(tr("Confirm deletion")),
content: Text(
tr("Do you really want to delete this message ? The operation can not be cancelled !"),
textAlign: TextAlign.justify,
),
actions: <Widget>[
FlatButton(
child: Text(
tr("Cancel").toUpperCase(),
2019-05-04 06:58:14 +00:00
),
onPressed: () => Navigator.pop(c, false),
2019-05-04 06:58:14 +00:00
),
FlatButton(
child: Text(
tr("Confirm").toUpperCase(),
style: TextStyle(color: Colors.red),
),
onPressed: () => Navigator.pop(c, true),
),
],
),
2019-05-04 06:58:14 +00:00
);
2019-05-04 08:24:38 +00:00
if (choice == null || !choice) return;
2019-05-04 06:58:14 +00:00
// Execute the request
2019-05-04 08:24:38 +00:00
if (!await _conversationsHelper.deleteMessage(message.id))
2019-05-04 06:58:14 +00:00
showSimpleSnack(context, tr("Could not delete conversation message!"));
}
2019-04-25 06:56:16 +00:00
}