User can delete posts.

This commit is contained in:
Pierre 2018-03-25 14:28:39 +02:00
parent 7a2177a45c
commit 54bb812e5c
6 changed files with 189 additions and 5 deletions

View File

@ -25,6 +25,7 @@ public class Post {
private PostTypes type;
private PostVisibilityLevels visibilityLevel;
private ArrayList<Comment> 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;

View File

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

View File

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

View File

@ -262,7 +262,7 @@ public class PostsAdapter extends ArrayAdapter<Post>{
*
* @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<Post>{
*/
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);

View File

@ -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<Integer, Void, Boolean>(){
@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){

View File

@ -114,4 +114,9 @@
<string name="comment_action_btn_description">Actions on comment</string>
<string name="post_action_btn_description">Actions on post</string>
<string name="action_delete_post">Delete</string>
<string name="popup_deletepost_title">Delete post</string>
<string name="popup_deletepost_message">Are you sure do you want to delete this post! Will will not be able to undo this operation !</string>
<string name="popup_deletepost_cancel">No</string>
<string name="popup_deletepost_confirm">Yes</string>
<string name="err_delete_post">An error occurred while trying to delete post! Please try again later…</string>
</resources>