2020-05-08 13:40:13 +00:00
|
|
|
import 'package:comunic/helpers/conversations_helper.dart';
|
2020-05-08 13:42:44 +00:00
|
|
|
import 'package:comunic/helpers/events_helper.dart';
|
2020-05-08 13:40:13 +00:00
|
|
|
import 'package:comunic/helpers/users_helper.dart';
|
|
|
|
import 'package:comunic/lists/unread_conversations_list.dart';
|
|
|
|
import 'package:comunic/lists/users_list.dart';
|
2021-03-11 17:50:36 +00:00
|
|
|
import 'package:comunic/ui/routes/main_route/main_route.dart';
|
2020-05-08 13:40:13 +00:00
|
|
|
import 'package:comunic/ui/widgets/async_screen_widget.dart';
|
2021-03-11 17:50:36 +00:00
|
|
|
import 'package:comunic/ui/widgets/conversation_image_widget.dart';
|
2020-05-08 13:42:44 +00:00
|
|
|
import 'package:comunic/ui/widgets/safe_state.dart';
|
2021-03-11 17:50:36 +00:00
|
|
|
import 'package:comunic/utils/date_utils.dart';
|
2020-05-08 13:40:13 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Unread conversations screen
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
class UnreadConversationsScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_UnreadConversationsScreenState createState() =>
|
|
|
|
_UnreadConversationsScreenState();
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:42:44 +00:00
|
|
|
class _UnreadConversationsScreenState
|
|
|
|
extends SafeState<UnreadConversationsScreen> {
|
2020-05-08 13:40:13 +00:00
|
|
|
UnreadConversationsList _list;
|
|
|
|
UsersList _users;
|
|
|
|
|
2020-05-08 13:42:44 +00:00
|
|
|
final _key = GlobalKey<AsyncScreenWidgetState>();
|
|
|
|
|
2020-05-08 13:40:13 +00:00
|
|
|
Future<void> _refresh() async {
|
|
|
|
_list = await ConversationsHelper.getListUnread();
|
|
|
|
_users = await UsersHelper().getListWithThrow(_list.usersID);
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:42:44 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
listen<NewNumberUnreadConversations>((e) => _key.currentState.refresh());
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:40:13 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AsyncScreenWidget(
|
2020-05-08 13:42:44 +00:00
|
|
|
key: _key,
|
2020-05-08 13:40:13 +00:00
|
|
|
onReload: _refresh,
|
|
|
|
onBuild: _buildList,
|
|
|
|
errorMessage: tr("Could not load the list of unread conversations!"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildList() {
|
|
|
|
// Check for no unread conversation
|
|
|
|
if (_list.isEmpty)
|
|
|
|
return Center(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(
|
|
|
|
tr("You do not have any unread conversation yet..."),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return ListView.builder(
|
|
|
|
itemBuilder: _tileBuilder,
|
|
|
|
itemCount: _list.length,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _tileBuilder(BuildContext context, int index) {
|
2021-03-11 17:50:36 +00:00
|
|
|
final conv = _list[index];
|
|
|
|
|
|
|
|
final message = _list[index].message;
|
|
|
|
|
|
|
|
final singleUserConv = conv.conv.members.length < 3;
|
|
|
|
|
|
|
|
String messageStr;
|
|
|
|
if (message.hasFile)
|
|
|
|
messageStr = tr("New file");
|
|
|
|
else if (message.hasMessage)
|
|
|
|
messageStr = singleUserConv
|
|
|
|
? message.message.content
|
|
|
|
: tr("%1% : %2%", args: {
|
|
|
|
"1": _users.getUser(message.userID).fullName,
|
|
|
|
"2": message.message.content,
|
|
|
|
});
|
|
|
|
else
|
|
|
|
message.serverMessage.getText(_users);
|
|
|
|
|
2020-05-08 13:40:13 +00:00
|
|
|
return ListTile(
|
2021-03-11 17:50:36 +00:00
|
|
|
leading: ConversationImageWidget(
|
|
|
|
conversation: conv.conv,
|
|
|
|
users: _users,
|
|
|
|
),
|
|
|
|
title: Text(ConversationsHelper.getConversationName(conv.conv, _users)),
|
2020-05-08 13:40:13 +00:00
|
|
|
subtitle: RichText(
|
2021-02-07 16:09:08 +00:00
|
|
|
text: TextSpan(style: Theme.of(context).textTheme.bodyText2, children: [
|
2020-05-08 13:40:13 +00:00
|
|
|
TextSpan(
|
2021-03-11 17:50:36 +00:00
|
|
|
text: messageStr,
|
2020-05-08 13:40:13 +00:00
|
|
|
style: TextStyle(fontStyle: FontStyle.italic),
|
|
|
|
),
|
|
|
|
]),
|
2021-03-11 17:50:36 +00:00
|
|
|
),
|
|
|
|
trailing: Text(diffTimeFromNowToStr(conv.message.timeSent)),
|
2021-04-06 16:22:45 +00:00
|
|
|
onTap: () => MainController.of(context).openConversationById(conv.conv.id),
|
2021-03-11 17:50:36 +00:00
|
|
|
);
|
2020-05-08 13:40:13 +00:00
|
|
|
}
|
|
|
|
}
|