diff --git a/lib/helpers/groups_helper.dart b/lib/helpers/groups_helper.dart index b07c16b..3040e04 100644 --- a/lib/helpers/groups_helper.dart +++ b/lib/helpers/groups_helper.dart @@ -54,6 +54,17 @@ class GroupsHelper { return list; } + /// Get a list of groups from the server. In case of error, this method throws + /// an exception + Future getListOrThrow(Set groups, + {bool force = false}) async { + final list = await getList(groups, force: force); + + if (list == null) throw Exception("Could not get the list of groups!"); + + return list; + } + /// Get a list of groups from the server Future getList(Set groups, {bool force = false}) async { final list = GroupsList(); diff --git a/lib/lists/notifications_list.dart b/lib/lists/notifications_list.dart index f0ef100..159b332 100644 --- a/lib/lists/notifications_list.dart +++ b/lib/lists/notifications_list.dart @@ -8,21 +8,19 @@ import 'package:comunic/models/notification.dart'; class NotificationsList extends AbstractList { /// Get the ID of all the users related to the notifications /// included in the list - List get usersIds { - final list = List(); + Set get usersIds { + final list = Set(); forEach((n) { - if (!list.contains(n.fromUser)) list.add(n.fromUser); + list.add(n.fromUser); if (n.onElemType == NotificationElementType.FRIENDSHIP_REQUEST || - n.onElemType == NotificationElementType.USER_PAGE) { - if (!list.contains(n.onElemId)) list.add(n.onElemId); - } + n.onElemType == NotificationElementType.USER_PAGE) + list.add(n.onElemId); if (n.fromContainerType == NotificationElementType.FRIENDSHIP_REQUEST || - n.fromContainerType == NotificationElementType.USER_PAGE) { - if (!list.contains(n.fromContainerId)) list.add(n.fromContainerId); - } + n.fromContainerType == NotificationElementType.USER_PAGE) + list.add(n.fromContainerId); }); return list; @@ -30,18 +28,18 @@ class NotificationsList extends AbstractList { /// Get the ID of all the groups related to the notifications /// included in the list - List get groupsIds { - final list = List(); + Set get groupsIds { + final list = Set(); forEach((n) { - if (n.onElemType == NotificationElementType.GROUP_PAGE && - !list.contains(n.onElemId)) list.add(n.onElemId); + if (n.onElemType == NotificationElementType.GROUP_PAGE) + list.add(n.onElemId); - if (n.fromContainerType == NotificationElementType.GROUP_PAGE && - !list.contains(n.fromContainerId)) list.add(n.fromContainerId); + if (n.fromContainerType == NotificationElementType.GROUP_PAGE) + list.add(n.fromContainerId); - if (n.onElemType == NotificationElementType.GROUP_MEMBERSHIP && - !list.contains(n.onElemId)) list.add(n.onElemId); + if (n.onElemType == NotificationElementType.GROUP_MEMBERSHIP) + list.add(n.onElemId); }); return list; diff --git a/lib/ui/screens/notifications_screen.dart b/lib/ui/screens/notifications_screen.dart index 40386c0..a2708a3 100644 --- a/lib/ui/screens/notifications_screen.dart +++ b/lib/ui/screens/notifications_screen.dart @@ -1,4 +1,9 @@ +import 'package:comunic/helpers/groups_helper.dart'; +import 'package:comunic/helpers/notifications_helper.dart'; +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/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -16,6 +21,8 @@ class NotificationsScreen extends StatefulWidget { class _NotificationsScreenState extends State { NotificationsList _list; + UsersList _users; + GroupsList _groups; _Status _status = _Status.LOADING; void setStatus(_Status s) => setState(() => _status = s); @@ -23,6 +30,31 @@ class _NotificationsScreenState extends State { Future _loadList() async { setStatus(_Status.LOADING); + try { + final list = await NotificationsHelper().getUnread(); + + final users = await UsersHelper().getListWithThrow(list.usersIds); + + final groups = await GroupsHelper().getListOrThrow(list.groupsIds); + + setState(() { + _list = list; + _users = users; + _groups = groups; + }); + } on Exception catch (e) { + print("Exception while getting the list of notifications!"); + print(e); + } on Error catch (e) { + print("Error while getting the list of notifications!"); + print(e.stackTrace); + } + } + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + _loadList(); } @override