From e111bec5f0a71ca6a4559c78b395c677cdc70a4e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 8 Dec 2018 21:05:05 +0100 Subject: [PATCH] Display groups posts --- .../client/data/helpers/PostsHelper.java | 48 ++++++++++ .../comunic/client/data/models/GroupInfo.java | 29 ++++++ .../ui/asynctasks/GetGroupPostsTask.java | 41 +++++++++ .../ui/fragments/PostsCreateFormFragment.java | 8 ++ .../groups/GroupPageMainFragment.java | 19 ++++ .../fragments/groups/GroupsPostsFragment.java | 92 +++++++++++++++++++ .../res/layout/fragment_group_main_page.xml | 4 +- 7 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupPostsTask.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupsPostsFragment.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java index a836e90..1cd8e27 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java @@ -125,6 +125,50 @@ public class PostsHelper { } } + /** + * Get the list of the posts of a group + * + * @param groupID The ID of the group to get the post from + * @return The list of posts / null in case of failure + */ + @Nullable + public PostsList get_group(int groupID){ + return get_group(groupID, -1); + } + + /** + * Get the list of the posts of a group + * + * @param groupID The ID of the group to get the post from + * @param from The post to start from (-1 not to specify) + * @return The list of posts / null in case of failure + */ + @Nullable + public PostsList get_group(int groupID, int from){ + + //Perform a request on the API + APIRequest params = new APIRequest(mContext, "posts/get_group"); + params.addInt("groupID", groupID); + + if(from > -1) + params.addInt("startFrom", from); + + //Perform the request + try { + + //Make the request on the API + APIResponse response = new APIRequestHelper().exec(params); + + //Get the list of posts and process it + JSONArray posts = response.getJSONArray(); + return parse_json_posts_list(posts); + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + /** * Get the list of latest posts of a user * @@ -255,6 +299,10 @@ public class PostsHelper { req.addString("kind-page", "user"); break; + case GROUP_PAGE: + req.addString("kind-page", "group"); + break; + default: throw new RuntimeException("Unsupported kind of page !"); } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java index bce03a8..067f3f8 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java @@ -135,4 +135,33 @@ public class GroupInfo { || getMembershipLevel() == GroupsMembershipLevels.MODERATOR || getMembershipLevel() == GroupsMembershipLevels.MEMBER; } + + /** + * Check if a user is at least the moderator of a group + * + * @return TRUE if the user is at least a moderator of the group / FALSE else + */ + public boolean isAtLeastModerator(){ + return getMembershipLevel() == GroupsMembershipLevels.ADMINISTRATOR + || getMembershipLevel() == GroupsMembershipLevels.MODERATOR; + } + + /** + * Check if the current user can post texts on this group or not + * + * @return TRUE if the user can posts texts / FALSE else + */ + public boolean canPostTexts(){ + + //Posting texts on a group requires to be at least a member of it + if(!isAtLeastMember()) + return false; + + if(getPostCreationLevel() == GroupPostsCreationLevel.MODERATORS && !isAtLeastModerator()) + return false; + + //If we get there, the user can certainly create posts + //on the group + return true; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupPostsTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupPostsTask.java new file mode 100644 index 0000000..a6691fd --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupPostsTask.java @@ -0,0 +1,41 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.arrays.PostsList; +import org.communiquons.android.comunic.client.data.helpers.PostsHelper; + +/** + * Get group posts safe async task + * + * @author Pierre HUBERT + */ +public class GetGroupPostsTask extends SafeAsyncTask { + + public GetGroupPostsTask(Context context) { + super(context); + } + + @Override + protected PostsList doInBackground(Integer... integers) { + PostsHelper helper = new PostsHelper(getContext()); + PostsList list; + + //Check if a start point has been specified + if(integers.length == 1) + list = helper.get_group(integers[0]); + else + list = helper.get_group(integers[0], integers[1]); + + //Check if the list of posts could not be retrieved + if(list == null) + return null; + + //Try to load related information + if(!helper.load_related_information(list)) + return null; + + //Success. Now return information + return list; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java index 407f17a..c7a611c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java @@ -56,6 +56,11 @@ public class PostsCreateFormFragment extends Fragment { */ public static final int PAGE_TYPE_USER = 1; + /** + * Page type : group page + */ + public static final int PAGE_TYPE_GROUP = 2; + /** * On post created interface */ @@ -249,6 +254,9 @@ public class PostsCreateFormFragment extends Fragment { switch (getArguments().getInt(PAGE_TYPE_ARG)){ case PAGE_TYPE_USER: post.setPage_type(PageType.USER_PAGE); + + case PAGE_TYPE_GROUP: + post.setPage_type(PageType.GROUP_PAGE); } post.setPage_id(getArguments().getInt(PAGE_ID_ARG)); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java index 8a92cf2..22adf26 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java @@ -5,6 +5,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -140,9 +141,27 @@ public class GroupPageMainFragment extends AbstractGroupFragment { */ private void applyGroupInfo(){ + + //Apply main group information mGroupName.setText(mAdvancedGroupInfo.getDisplayName()); mGroupImage.setGroup(mAdvancedGroupInfo); + + + //Show posts list + Bundle args = new Bundle(); + args.putInt(GroupsPostsFragment.ARGUMENT_GROUP_ID, mGroupID); + args.putBoolean(GroupsPostsFragment.ARGUMENT_CAN_CREATE_GROUPS, + mAdvancedGroupInfo.canPostTexts()); + + GroupsPostsFragment fragment = new GroupsPostsFragment(); + fragment.setArguments(args); + + FragmentTransaction transaction + = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction(); + transaction.replace(R.id.postsListTarget, fragment); + transaction.commit(); + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupsPostsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupsPostsFragment.java new file mode 100644 index 0000000..374aeb5 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupsPostsFragment.java @@ -0,0 +1,92 @@ +package org.communiquons.android.comunic.client.ui.fragments.groups; + +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.View; + +import org.communiquons.android.comunic.client.data.arrays.PostsList; +import org.communiquons.android.comunic.client.ui.asynctasks.GetGroupPostsTask; +import org.communiquons.android.comunic.client.ui.asynctasks.GetLatestPostsTask; +import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.ui.fragments.AbstractPostsListFragment; +import org.communiquons.android.comunic.client.ui.fragments.PostsCreateFormFragment; + +/** + * Group posts fragment + * + * @author Pierre HUBERT + */ +public class GroupsPostsFragment extends AbstractPostsListFragment { + + /** + * Debug tag + */ + private static final String TAG = GroupsPostsFragment.class.getSimpleName(); + + /** + * Bundle arguments + */ + public static final String ARGUMENT_GROUP_ID = "group_id"; + public static final String ARGUMENT_CAN_CREATE_GROUPS = "can_create_posts"; + + /** + * The ID of the current group + */ + private int mGroupID; + + /** + * Whether posts can be created by the user + */ + private boolean mCanCreatePosts; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Get target group ID + assert getArguments() != null; + mGroupID = getArguments().getInt(ARGUMENT_GROUP_ID); + mCanCreatePosts = getArguments().getBoolean(ARGUMENT_CAN_CREATE_GROUPS); + + setDisplayPostsTarget(false); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + enablePostFormFragment(mCanCreatePosts); + if(mCanCreatePosts) + init_create_post_fragment(PostsCreateFormFragment.PAGE_TYPE_GROUP, mGroupID); + } + + @Override + public void onLoadPosts() { + loadPosts(-1); + } + + @Override + public void onLoadMorePosts(int last_post_id) { + loadPosts(last_post_id-1); + } + + /** + * Load a list of posts + * + * @param last_post_id The id of latest post to start from. Set -1 to get the latest posts + */ + private void loadPosts(int last_post_id){ + getTasksManager().unsetSpecificTasks(GetLatestPostsTask.class); + GetGroupPostsTask task = new GetGroupPostsTask(getActivity()); + task.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { + @Override + public void OnPostExecute(PostsList posts) { + onGotNewPosts(posts); + } + }); + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mGroupID, last_post_id); + getTasksManager().addTask(task); + } +} diff --git a/app/src/main/res/layout/fragment_group_main_page.xml b/app/src/main/res/layout/fragment_group_main_page.xml index 89977f0..e381d11 100644 --- a/app/src/main/res/layout/fragment_group_main_page.xml +++ b/app/src/main/res/layout/fragment_group_main_page.xml @@ -5,8 +5,8 @@ android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools"> -