From 54bb812e5c0f6831a6ede22e5f4feb8f7650b159 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 25 Mar 2018 14:28:39 +0200 Subject: [PATCH] User can delete posts. --- .../comunic/client/data/posts/Post.java | 10 +++ .../client/data/posts/PostUserAccess.java | 34 ++++++++ .../client/data/posts/PostsHelper.java | 48 ++++++++++- .../client/ui/adapters/PostsAdapter.java | 11 ++- .../ui/fragments/PostsListFragment.java | 86 ++++++++++++++++++- app/src/main/res/values/strings.xml | 5 ++ 6 files changed, 189 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostUserAccess.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java index b3bcb8a..4776110 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java @@ -25,6 +25,7 @@ public class Post { private PostTypes type; private PostVisibilityLevels visibilityLevel; private ArrayList comments_list; + private PostUserAccess user_access_level = PostUserAccess.NO_ACCESS; //Files specific private String file_path_url; @@ -98,6 +99,15 @@ public class Post { return comments_list; } + //Set and post user access level + void setUser_access_level(PostUserAccess user_access_level) { + this.user_access_level = user_access_level; + } + + public PostUserAccess getUser_access_level() { + return user_access_level; + } + //Set and get file path url void setFile_path_url(String file_path_url) { this.file_path_url = file_path_url; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostUserAccess.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostUserAccess.java new file mode 100644 index 0000000..8c2c999 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostUserAccess.java @@ -0,0 +1,34 @@ +package org.communiquons.android.comunic.client.data.posts; + +/** + * Post users access enum + * + * @author Pierre HUBERT + * Created by pierre on 3/25/18. + */ + +public enum PostUserAccess { + + /** + * No access to the post + */ + NO_ACCESS, + + /** + * Basic access to the post + * > Can see the post, put comments within it, like it... + */ + BASIC_ACCESS, + + /** + * Intermediate access to the post + * > Can delete the post + */ + INTERMEDIATE_ACCESS, + + /** + * Full access to the post + */ + FULL_ACCESS + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java index 3e12f24..3642ce9 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java @@ -72,9 +72,34 @@ public class PostsHelper { } /** - * Parse a JSON post informations into POST object + * Intend to delete a post specified by its ID * - * @param json Source JSON post informations + * @param postID The ID of the post to delete + * @return TRUE in case of SUCCESS / FALSE else + */ + public boolean delete(int postID){ + + //Perform the request on the server + APIRequestParameters params = new APIRequestParameters(mContext, "posts/delete"); + params.addInt("postID", postID); + + //Intend to perform the request + try { + + APIResponse response = new APIRequest().exec(params); + + return response.getResponse_code() == 200; + + } catch (Exception e){ + e.printStackTrace(); + return false; + } + } + + /** + * Parse a JSON post information into POST object + * + * @param json Source JSON post information * @return The created post element */ private Post parse_json_post(JSONObject json) throws JSONException { @@ -126,6 +151,25 @@ public class PostsHelper { } + //Determine the user access level to the post + switch (json.getString("user_access")){ + + case "basic": + post.setUser_access_level(PostUserAccess.BASIC_ACCESS); + break; + + case "intermediate": + post.setUser_access_level(PostUserAccess.INTERMEDIATE_ACCESS); + break; + + case "full": + post.setUser_access_level(PostUserAccess.FULL_ACCESS); + break; + + default: + post.setUser_access_level(PostUserAccess.NO_ACCESS); + } + //Get file path url (if any) if(json.getString("file_path_url") != null){ post.setFile_path_url(json.getString("file_path_url")); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java index 933f8f9..658e404 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java @@ -262,7 +262,7 @@ public class PostsAdapter extends ArrayAdapter{ * * @param pos The position of the post in the list * @param button The button triggered to submit comment creation - * @param post Informations about the target post + * @param post Information about the target post * @param input The input where the comment comment was typed */ void onCreateComment(int pos, View button, Post post, EditCommentContentView input); @@ -276,10 +276,17 @@ public class PostsAdapter extends ArrayAdapter{ */ void showPostActions(View button, int pos, Post post); + /** + * Handles the deletion process of a post + * + * @param pos The position of the post to delete + */ + void deletePost(int pos); + /** * Show the available actions for a comment * - * @param button The button that provoqued the event + * @param button The button that provoked the event * @param comment Target comment for the actions */ void showCommentActions(View button, Comment comment); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java index 8934fe7..c710c3b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java @@ -25,6 +25,8 @@ import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.comments.Comment; import org.communiquons.android.comunic.client.data.comments.CommentsHelper; import org.communiquons.android.comunic.client.data.posts.Post; +import org.communiquons.android.comunic.client.data.posts.PostUserAccess; +import org.communiquons.android.comunic.client.data.posts.PostsHelper; import org.communiquons.android.comunic.client.data.posts.PostsList; import org.communiquons.android.comunic.client.data.utils.StringsUtils; import org.communiquons.android.comunic.client.ui.adapters.PostsAdapter; @@ -94,6 +96,11 @@ public class PostsListFragment extends Fragment */ ListView mListView; + /** + * Posts helper + */ + PostsHelper mPostsHelper; + /** * Comments helper */ @@ -144,6 +151,9 @@ public class PostsListFragment extends Fragment */ public void show(){ + //Create post helper + mPostsHelper = new PostsHelper(getActivity()); + //Create comment helper mCommentsHelper = new CommentsHelper(getActivity()); @@ -252,7 +262,7 @@ public class PostsListFragment extends Fragment } @Override - public void showPostActions(View button, final int pos, Post post) { + public void showPostActions(View button, final int pos, final Post post) { button.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override @@ -267,6 +277,14 @@ public class PostsListFragment extends Fragment MENU_ACTION = MENU_ACTIONS_POST; mNumCurrPostInContextMenu = pos; + //Disable some options if the user is not the post owner + if(post.getUser_access_level() != PostUserAccess.INTERMEDIATE_ACCESS && + post.getUser_access_level() != PostUserAccess.FULL_ACCESS){ + + //Disable delete action + menu.findItem(R.id.action_delete).setEnabled(false); + + } } }); @@ -274,6 +292,58 @@ public class PostsListFragment extends Fragment button.showContextMenu(); } + @Override + public void deletePost(final int pos) { + + //Get information about the related post + final Post post = mPostsList.get(pos); + + //Ask user confirmation + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.popup_deletepost_title) + .setMessage(R.string.popup_deletepost_message) + .setNegativeButton(R.string.popup_deletepost_cancel, null) + + + .setPositiveButton(R.string.popup_deletepost_confirm, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + //Remove the post from the list + mPostsList.remove(pos); + mPostsAdapter.notifyDataSetChanged(); + + //Perform post deletion + new AsyncTask(){ + + @Override + protected Boolean doInBackground(Integer... params) { + return mPostsHelper.delete(params[0]); + } + + @Override + protected void onPostExecute(Boolean result) { + + if(getActivity() == null) + return; + + if(!result){ + Toast.makeText(getActivity(), R.string.err_delete_post, + Toast.LENGTH_SHORT).show(); + } + + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, post.getId()); + + } + }) + + + .show(); + + } + @Override public void showCommentActions(View button, final Comment comment) { @@ -352,6 +422,20 @@ public class PostsListFragment extends Fragment if(MENU_ACTION == MENU_ACTION_NONE) return false; + //Check if the action is related to a post + if(MENU_ACTION == MENU_ACTIONS_POST){ + + //Check whether the related post exists or not + if(!(mPostsList.size() > mNumCurrPostInContextMenu)) + return false; + + //Check if the request is to delete the comment + if(item.getItemId() == R.id.action_delete) { + deletePost(mNumCurrPostInContextMenu); + return true; + } + } + //Check if a comment action context menu has been created if(MENU_ACTION == MENU_ACTION_COMMENTS){ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4c152e2..f45b8ac 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -114,4 +114,9 @@ Actions on comment Actions on post Delete + Delete post + Are you sure do you want to delete this post! Will will not be able to undo this operation ! + No + Yes + An error occurred while trying to delete post! Please try again later…