2021-03-13 08:29:54 +00:00
|
|
|
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;
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
const UserWritingInConvNotifier({Key? key, required this.convID})
|
2022-03-11 16:16:46 +00:00
|
|
|
: super(key: key);
|
2021-03-13 08:29:54 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_UserWritingInConvNotifierState createState() =>
|
|
|
|
_UserWritingInConvNotifierState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UserWritingInConvNotifierState
|
|
|
|
extends SafeState<UserWritingInConvNotifier> {
|
|
|
|
final _usersInfo = UsersList();
|
|
|
|
|
2021-03-13 14:14:54 +00:00
|
|
|
final _list = [];
|
2021-03-13 08:29:54 +00:00
|
|
|
|
|
|
|
@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(
|
2022-03-10 18:39:57 +00:00
|
|
|
writingText!,
|
2021-03-13 08:29:54 +00:00
|
|
|
style: TextStyle(fontSize: 10),
|
|
|
|
textAlign: TextAlign.justify,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
String? get writingText {
|
2021-03-13 08:29:54 +00:00
|
|
|
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(", ")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
void _handleEvent(int? userID) async {
|
2021-03-13 08:29:54 +00:00
|
|
|
setState(() => this._list.add(userID));
|
|
|
|
|
|
|
|
await Future.delayed(
|
2022-03-10 18:39:57 +00:00
|
|
|
Duration(seconds: srvConfig!.conversationsPolicy.writingEventLifetime));
|
2021-03-13 08:29:54 +00:00
|
|
|
|
|
|
|
setState(() => this._list.removeAt(0));
|
|
|
|
}
|
|
|
|
}
|