1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Show who is writing messages

This commit is contained in:
Pierre HUBERT 2021-03-13 09:29:54 +01:00
parent 7ccc7a492e
commit d8b2dd2599
4 changed files with 100 additions and 0 deletions

View File

@ -50,6 +50,14 @@ class DeletedCommentEvent {
DeletedCommentEvent(this.commentID);
}
/// Writing message in conversation event
class WritingMessageInConversationEvent {
final int convID;
final int userID;
WritingMessageInConversationEvent(this.convID, this.userID);
}
/// New conversation message
class NewConversationMessageEvent {
final ConversationMessage msg;

View File

@ -144,6 +144,12 @@ class WebSocketHelper {
EventsHelper.emit(DeletedCommentEvent(msg.data));
break;
// A user is writing a new message
case "writing_message_in_conv":
EventsHelper.emit(WritingMessageInConversationEvent(
msg.data["conv_id"], msg.data["user_id"]));
break;
// Created new conversation message
case "new_conv_message":
EventsHelper.emit(NewConversationMessageEvent(

View File

@ -17,6 +17,7 @@ import 'package:comunic/ui/tiles/server_conversation_message_tile.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/ui/widgets/scroll_watcher.dart';
import 'package:comunic/ui/widgets/user_writing_in_conv_notifier.dart';
import 'package:comunic/utils/date_utils.dart';
import 'package:comunic/utils/files_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
@ -649,6 +650,7 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
: null,
),
_messages.length == 0 ? _buildNoMessagesNotice() : _buildMessagesList(),
UserWritingInConvNotifier(convID: _conversation.id),
_sendCancel != null ? _buildSendingWidget() : _buildSendMessageForm(),
_showEmojiPicker ? _buildEmojiContainer() : Container(),
],

View File

@ -0,0 +1,84 @@
import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:flutter/material.dart';
/// User writing a message in a conversation notifier
///
/// @author Pierre Hubert
class UserWritingInConvNotifier extends StatefulWidget {
final int convID;
const UserWritingInConvNotifier({Key key, @required this.convID})
: assert(convID != null),
super(key: key);
@override
_UserWritingInConvNotifierState createState() =>
_UserWritingInConvNotifierState();
}
class _UserWritingInConvNotifierState
extends SafeState<UserWritingInConvNotifier> {
final _usersInfo = UsersList();
final _list = List();
@override
void initState() {
this.listen<WritingMessageInConversationEvent>((ev) async {
try {
if (ev.convID != widget.convID) return;
if (!_usersInfo.hasUser(ev.userID))
_usersInfo.add(await UsersHelper().getSingleWithThrow(ev.userID));
_handleEvent(ev.userID);
} catch (e, s) {
logError(e, s);
}
});
super.initState();
}
@override
Widget build(BuildContext context) => Padding(
padding: EdgeInsets.only(left: 5, right: 5),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
writingText,
style: TextStyle(fontSize: 10),
textAlign: TextAlign.justify,
),
),
);
String get writingText {
if (_list.isEmpty) return "";
final users = _list.toSet().map((e) => _usersInfo.getUser(e)).toList();
if (users.length == 1)
return tr("%1% is writing...", args: {"1": users.first.fullName});
return tr("%1% and %2% are writing...", args: {
"1": users.first.fullName,
"2": users.sublist(1).map((e) => e.fullName).join(", ")
});
}
void _handleEvent(int userID) async {
setState(() => this._list.add(userID));
await Future.delayed(
Duration(seconds: srvConfig.conversationsPolicy.writingEventLifetime));
setState(() => this._list.removeAt(0));
}
}