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

Show groups posts

This commit is contained in:
Pierre HUBERT 2019-06-10 09:47:02 +02:00
parent d62d23bd44
commit ee864d3d98
7 changed files with 186 additions and 7 deletions

View File

@ -0,0 +1,73 @@
import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/group.dart';
/// Groups helper
///
/// @author Pierre HUBERT
const _APIGroupsMembershipLevelsMap = {
"administrator": GroupMembershipLevel.ADMINISTRATOR,
"moderator": GroupMembershipLevel.MODERATOR,
"member": GroupMembershipLevel.MEMBER,
"invited": GroupMembershipLevel.INVITED,
"pending": GroupMembershipLevel.PENDING,
"visitor": GroupMembershipLevel.VISITOR
};
const _APIGroupsVisibilityLevelsMap = {
"open": GroupVisibilityLevel.OPEN,
"private": GroupVisibilityLevel.PRIVATE,
"secrete": GroupVisibilityLevel.SECRETE
};
const _APIGroupsRegistrationLevelsMap = {
"open": GroupRegistrationLevel.OPEN,
"moderated": GroupRegistrationLevel.MODERATED,
"closed": GroupRegistrationLevel.CLOSED
};
const _APIGroupsPostsCreationLevelsMap = {
"moderators": GroupPostCreationLevel.MODERATORS,
"members": GroupPostCreationLevel.MEMBERS
};
class GroupsHelper {
/// Get a list of groups from the server
Future<GroupsList> getList(Set<int> groups) async {
if(groups.length == 0)
return GroupsList();
final response = await APIRequest(
uri: "groups/get_multiple_info",
needLogin: true,
args: {"list": groups.join(",")},
).exec();
if (response.code != 200) return null;
final list = GroupsList();
response
.getObject()
.forEach((k, d) => list[int.parse(k)] = _getGroupFromAPI(d));
return list;
}
/// Turn an API entry into a group object
Group _getGroupFromAPI(Map<String, dynamic> map) {
return Group(
id: map["id"],
name: map["name"],
numberMembers: map["number_members"],
membershipLevel: _APIGroupsMembershipLevelsMap[map["membership"]],
visibilityLevel: _APIGroupsVisibilityLevelsMap[map["visibility"]],
registrationLevel:
_APIGroupsRegistrationLevelsMap[map["registration_level"]],
postCreationLevel: _APIGroupsPostsCreationLevelsMap[map["posts_level"]],
virtualDirectory: map["virtual_directory"],
following: map["following"]);
}
}

View File

@ -43,6 +43,9 @@ class PostsHelper {
final response = await APIRequest(
uri: "posts/get_latest",
needLogin: true,
args: {
"include_groups": true.toString()
}
).exec();
if (response.code != 200) return null;

View File

@ -0,0 +1,29 @@
import 'dart:collection';
import 'package:comunic/models/group.dart';
/// Groups list
///
/// @author Pierre HUBERT
class GroupsList extends MapBase<int, Group> {
final Map<int, Group> _groups = Map();
@override
Group operator [](Object key) => _groups[key];
@override
void operator []=(int key, Group value) => _groups[key] = value;
@override
void clear() => _groups.clear();
@override
Iterable<int> get keys => _groups.keys;
@override
Group remove(Object key) => _groups.remove(key);
}

View File

@ -9,7 +9,6 @@ import 'package:comunic/models/post.dart';
/// @author Pierre HUBERT
class PostsList extends ListBase<Post> {
List<Post> _list = List();
int get length => _list.length;
@ -29,14 +28,22 @@ class PostsList extends ListBase<Post> {
forEach((p) {
set.add(p.userID);
if(p.userPageID != null && p.userPageID > 0)
set.add(p.userPageID);
if (p.userPageID != null && p.userPageID > 0) set.add(p.userPageID);
if(p.comments != null)
set.addAll(p.comments.usersID);
if (p.comments != null) set.addAll(p.comments.usersID);
});
return set;
}
}
/// Get the list of groups in this list of posts
Set<int> get groupsID {
Set<int> set = Set();
forEach((p) {
if (p.isGroupPost) set.add(p.groupID);
});
return set;
}
}

53
lib/models/group.dart Normal file
View File

@ -0,0 +1,53 @@
import 'package:meta/meta.dart';
/// Group information
///
/// @author Pierre HUBERT
enum GroupMembershipLevel {
ADMINISTRATOR,
MODERATOR,
MEMBER,
INVITED,
PENDING,
VISITOR
}
enum GroupVisibilityLevel { OPEN, PRIVATE, SECRETE }
enum GroupRegistrationLevel { OPEN, MODERATED, CLOSED }
enum GroupPostCreationLevel { MODERATORS, MEMBERS }
class Group {
final int id;
final String name;
final int numberMembers;
final GroupMembershipLevel membershipLevel;
final GroupVisibilityLevel visibilityLevel;
final GroupRegistrationLevel registrationLevel;
final GroupPostCreationLevel postCreationLevel;
final String virtualDirectory;
final bool following;
Group({
@required this.id,
@required this.name,
@required this.numberMembers,
@required this.membershipLevel,
@required this.visibilityLevel,
@required this.registrationLevel,
@required this.postCreationLevel,
@required this.virtualDirectory,
@required this.following,
}) : assert(id != null),
assert(name != null),
assert(numberMembers != null),
assert(membershipLevel != null),
assert(visibilityLevel != null),
assert(registrationLevel != null),
assert(postCreationLevel != null),
assert(following != null);
get displayName => this.name;
}

View File

@ -6,6 +6,7 @@ import 'package:comunic/enums/post_visibility_level.dart';
import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/helpers/likes_helper.dart';
import 'package:comunic/helpers/posts_helper.dart';
import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/comment.dart';
import 'package:comunic/models/like_element.dart';
@ -38,6 +39,7 @@ enum _PostActions { DELETE, UPDATE_CONTENT }
class PostTile extends StatefulWidget {
final Post post;
final UsersList usersInfo;
final GroupsList groupsInfo;
final void Function(Post) onDeletedPost;
final bool showPostTarget;
@ -47,10 +49,12 @@ class PostTile extends StatefulWidget {
@required this.usersInfo,
@required this.onDeletedPost,
@required this.showPostTarget,
@required this.groupsInfo,
}) : assert(post != null),
assert(usersInfo != null),
assert(onDeletedPost != null),
assert(showPostTarget != null),
assert(groupsInfo != null),
super(key: key);
@override
@ -85,7 +89,7 @@ class _PostTileState extends State<PostTile> {
return " > " +
(widget.post.isGroupPost
? "Group"
? widget.groupsInfo[widget.post.groupID].displayName
: widget.usersInfo.getUser(widget.post.userPageID).displayName);
}

View File

@ -1,4 +1,6 @@
import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/post.dart';
@ -33,10 +35,12 @@ class PostsListWidget extends StatefulWidget {
class _PostsListWidgetState extends State<PostsListWidget> {
// Helpers
final UsersHelper _usersHelper = UsersHelper();
final GroupsHelper _groupsHelper = GroupsHelper();
// Class members
PostsList _list;
UsersList _users;
GroupsList _groups;
set error(ErrorLevel err) => setState(() => _error = err);
@ -61,9 +65,14 @@ class _PostsListWidgetState extends State<PostsListWidget> {
if (users == null) return _loadError();
final groups = await _groupsHelper.getList(list.groupsID);
if(groups == null) return _loadError();
setState(() {
_list = list;
_users = users;
_groups = groups;
});
}
@ -77,6 +86,7 @@ class _PostsListWidgetState extends State<PostsListWidget> {
itemBuilder: (c, i) => PostTile(
post: _list[i],
usersInfo: _users,
groupsInfo: _groups,
onDeletedPost: _removePost,
showPostTarget: widget.showPostsTarget,
),