From e2bf5e73dd8de8f9c8eb1acc387abdf038c6f891 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 2 May 2020 11:18:03 +0200 Subject: [PATCH] Display the list of members of a group --- lib/helpers/groups_helper.dart | 23 +++++++++++++ lib/lists/group_members_list.dart | 11 +++++++ lib/models/group_membership.dart | 23 +++++++++++++ lib/ui/screens/group_members_screen.dart | 42 ++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 lib/lists/group_members_list.dart create mode 100644 lib/models/group_membership.dart diff --git a/lib/helpers/groups_helper.dart b/lib/helpers/groups_helper.dart index f289e3a..204744e 100644 --- a/lib/helpers/groups_helper.dart +++ b/lib/helpers/groups_helper.dart @@ -1,9 +1,11 @@ import 'dart:typed_data'; +import 'package:comunic/lists/group_members_list.dart'; import 'package:comunic/lists/groups_list.dart'; import 'package:comunic/models/advanced_group_info.dart'; import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/group.dart'; +import 'package:comunic/models/group_membership.dart'; import 'package:comunic/utils/api_utils.dart'; import 'package:comunic/utils/map_utils.dart'; @@ -254,6 +256,18 @@ class GroupsHelper { .addString("password", password) .execWithThrow(); + /// Get the list of members of the group + /// + /// Throws in case of failure + static Future getMembersList(int groupID) async => + GroupMembersList() + ..addAll((await APIRequest(uri: "groups/get_members", needLogin: true) + .addInt("id", groupID) + .execWithThrow()) + .getArray() + .map((f) => _apiToGroupMembership(f)) + .toList()); + /// Turn an API entry into a group object Group _getGroupFromAPI(Map map) { return Group( @@ -290,4 +304,13 @@ class GroupsHelper { likes: map["number_likes"], userLike: map["is_liking"], ); + + /// Create [GroupMembership] object from API entry + static GroupMembership _apiToGroupMembership(Map row) => + GroupMembership( + userID: row["user_id"], + groupID: row["group_id"], + timeCreate: row["time_create"], + level: _APIGroupsMembershipLevelsMap[row["level"]], + ); } diff --git a/lib/lists/group_members_list.dart b/lib/lists/group_members_list.dart new file mode 100644 index 0000000..040414d --- /dev/null +++ b/lib/lists/group_members_list.dart @@ -0,0 +1,11 @@ +import 'package:comunic/lists/abstract_list.dart'; +import 'package:comunic/models/group_membership.dart'; + +/// Group members list +/// +/// @author Pierre Hubert + +class GroupMembersList extends AbstractList { + /// Get the list of users in this set + Set get usersID => map((f) => f.userID).toSet(); +} diff --git a/lib/models/group_membership.dart b/lib/models/group_membership.dart new file mode 100644 index 0000000..58abe46 --- /dev/null +++ b/lib/models/group_membership.dart @@ -0,0 +1,23 @@ +import 'package:comunic/models/group.dart'; +import 'package:flutter/material.dart'; + +/// Group membership information +/// +/// @author Pierre Hubert + +class GroupMembership { + final int userID; + final int groupID; + final int timeCreate; + final GroupMembershipLevel level; + + const GroupMembership({ + @required this.userID, + @required this.groupID, + @required this.timeCreate, + @required this.level, + }) : assert(userID != null), + assert(groupID != null), + assert(timeCreate != null), + assert(level != null); +} diff --git a/lib/ui/screens/group_members_screen.dart b/lib/ui/screens/group_members_screen.dart index 89a7076..b83bfc1 100644 --- a/lib/ui/screens/group_members_screen.dart +++ b/lib/ui/screens/group_members_screen.dart @@ -1,3 +1,10 @@ +import 'package:comunic/helpers/groups_helper.dart'; +import 'package:comunic/helpers/users_helper.dart'; +import 'package:comunic/lists/group_members_list.dart'; +import 'package:comunic/lists/users_list.dart'; +import 'package:comunic/models/group_membership.dart'; +import 'package:comunic/ui/widgets/account_image_widget.dart'; +import 'package:comunic/ui/widgets/async_screen_widget.dart'; import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:flutter/material.dart'; @@ -18,6 +25,17 @@ class GroupMembersScreen extends StatefulWidget { } class _GroupMembersScreenState extends State { + GroupMembersList _members; + UsersList _users; + + Future _refresh() async { + final members = await GroupsHelper.getMembersList(widget.groupID); + final users = await UsersHelper().getListWithThrow(members.usersID); + + _members = members; + _users = users; + } + @override Widget build(BuildContext context) { return Scaffold( @@ -25,6 +43,30 @@ class _GroupMembersScreenState extends State { leading: ComunicBackButton(), title: Text(tr("Group members")), ), + body: _buildBody(), + ); + } + + Widget _buildBody() => AsyncScreenWidget( + onReload: _refresh, + onBuild: _buildBodyContent, + errorMessage: tr("Could not load the list of members of this group!"), + ); + + Widget _buildBodyContent() { + return ListView( + children: _members.map(_buildGroupMemberTile).toList(), + ); + } + + ListTile _buildGroupMemberTile(GroupMembership membership) { + final user = _users.getUser(membership.userID); + + return ListTile( + leading: AccountImageWidget( + user: user, + ), + title: Text(user.displayName), ); } }