From 3e15064e4483685e965983a03a03d68b89962a7e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 2 Nov 2019 09:43:09 +0100 Subject: [PATCH] Display the list of notifications --- lib/ui/screens/notifications_screen.dart | 137 ++++++++++++++++++++++- 1 file changed, 131 insertions(+), 6 deletions(-) diff --git a/lib/ui/screens/notifications_screen.dart b/lib/ui/screens/notifications_screen.dart index dbcf8c1..0eb2567 100644 --- a/lib/ui/screens/notifications_screen.dart +++ b/lib/ui/screens/notifications_screen.dart @@ -4,8 +4,10 @@ import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/groups_list.dart'; import 'package:comunic/lists/notifications_list.dart'; import 'package:comunic/lists/users_list.dart'; +import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; +import 'package:comunic/models/notification.dart' as n; import 'package:flutter/material.dart'; /// Notifications screen @@ -70,12 +72,7 @@ class _NotificationsScreenState extends State { return Column( children: [ Expanded( - child: RefreshIndicator( - child: SingleChildScrollView( - physics: AlwaysScrollableScrollPhysics(), - child: _buildBody(), - ), - onRefresh: _loadList), + child: RefreshIndicator(child: _buildBody(), onRefresh: _loadList), ) ], ); @@ -83,6 +80,24 @@ class _NotificationsScreenState extends State { /// Build body Widget _buildBody() { + if (_status == _Status.ERROR || _list.length == 0) + return SingleChildScrollView( + physics: AlwaysScrollableScrollPhysics(), + child: _buildSingleChildCases(), + ); + + return ListView( + children: _list + .map((f) => _NotificationTile( + notification: f, + usersList: _users, + groupsList: _groups, + )) + .toList(), + ); + } + + Widget _buildSingleChildCases() { // In case of error if (_status == _Status.ERROR) return buildErrorCard(tr("Could not get the list of notifications!"), @@ -103,3 +118,113 @@ class _NotificationsScreenState extends State { ); } } + +class _NotificationTile extends StatelessWidget { + final n.Notification notification; + final UsersList usersList; + final GroupsList groupsList; + + const _NotificationTile({ + Key key, + @required this.notification, + @required this.usersList, + @required this.groupsList, + }) : assert(notification != null), + super(key: key); + + @override + Widget build(BuildContext context) { + final srcUser = usersList.getUser(notification.fromUser); + + String message = "${srcUser.fullName} "; + + switch (notification.type) { + // Comment + case n.NotificationType.COMMENT_CREATED: + message += tr("posted a comment"); + break; + + // Friendship requests + case n.NotificationType.SENT_FRIEND_REQUEST: + message += tr("sent you a friendship request."); + break; + + case n.NotificationType.ACCEPTED_FRIEND_REQUEST: + message += tr("accepted your friendship request."); + break; + + case n.NotificationType.REJECTED_FRIEND_REQUEST: + message += tr("rejected your friendship request."); + break; + + // Groups membership + case n.NotificationType.SENT_GROUP_MEMBERSHIP_INVITATION: + message += tr("invited you to join the group"); + break; + + case n.NotificationType.ACCEPTED_GROUP_MEMBERSHIP_INVITATION: + message += tr("accepted his invitation to join the group"); + break; + + case n.NotificationType.REJECTED_GROUP_MEMBERSHIP_INVITATION: + message += tr("rejected his invitation to join the group"); + break; + + case n.NotificationType.SENT_GROUP_MEMBERSHIP_REQUEST: + message += tr("sent a request to join the group"); + break; + + case n.NotificationType.ACCEPTED_GROUP_MEMBERSHIP_REQUEST: + message += tr("accepted you request to join the group"); + break; + + case n.NotificationType.REJECTED_GROUP_MEMBERSHIP_REQUEST: + message += tr("rejected your request to join the group"); + break; + + // Generic element creation + case n.NotificationType.ELEM_CREATED: + if (notification.onElemType == n.NotificationElementType.POST) + message += tr("created a new post"); + break; + + case n.NotificationType.ELEM_UPDATED: + // Operation not implemented + break; + } + + // Separator + message += " "; + + // Notification target + + // User page + if (notification.fromContainerType == n.NotificationElementType.USER_PAGE) { + if (notification.fromUser == notification.fromContainerId) + message += tr("on his / her page"); + else + message += tr("on %user_name%'s page", args: { + "user_name": usersList.getUser(notification.fromContainerId).fullName + }); + } + + // Group page + if (notification.fromContainerType == + n.NotificationElementType.GROUP_PAGE) { + message += tr("on the group %group%.", args: { + "group": groupsList[notification.fromContainerId].displayName + }); + } + + // Group membership + if (notification.onElemType == n.NotificationElementType.GROUP_MEMBERSHIP) + message += groupsList[notification.onElemId].displayName; + + return ListTile( + leading: AccountImageWidget( + user: srcUser, + ), + title: Text(message), + ); + } +}