1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 04:48:51 +00:00

Display the list of members of a group

This commit is contained in:
Pierre HUBERT 2020-05-02 11:18:03 +02:00
parent bff73dbb21
commit e2bf5e73dd
4 changed files with 99 additions and 0 deletions

View File

@ -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<GroupMembersList> 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<String, dynamic> 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<String, dynamic> row) =>
GroupMembership(
userID: row["user_id"],
groupID: row["group_id"],
timeCreate: row["time_create"],
level: _APIGroupsMembershipLevelsMap[row["level"]],
);
}

View File

@ -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<GroupMembership> {
/// Get the list of users in this set
Set<int> get usersID => map((f) => f.userID).toSet();
}

View File

@ -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);
}

View File

@ -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<GroupMembersScreen> {
GroupMembersList _members;
UsersList _users;
Future<void> _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<GroupMembersScreen> {
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),
);
}
}