Can delete comments.

This commit is contained in:
Pierre 2018-03-24 09:25:43 +01:00
parent d04158b7e5
commit 679cfaa208
5 changed files with 182 additions and 41 deletions

View File

@ -102,6 +102,32 @@ public class CommentsHelper {
} }
/**
* Intend to delete a comment
*
* @param commentID The ID of the comment to delete
* @return TRUE in case of success / FALSE else
*/
public boolean delete(int commentID){
//Prepare an API request
APIRequestParameters params = new APIRequestParameters(mContext, "comments/delete");
params.addInt("commentID", commentID);
//Try 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 array that contains comment and return the list of comments as an object * Parse a json array that contains comment and return the list of comments as an object
* *

View File

@ -29,12 +29,7 @@ import java.util.ArrayList;
* Created by pierre on 3/11/18. * Created by pierre on 3/11/18.
*/ */
public class CommentsAdapter extends ArrayAdapter<Comment> { class CommentsAdapter extends ArrayAdapter<Comment> {
/**
* Comment helper
*/
private static CommentsHelper mCHelper = null;
/** /**
* Class constructor * Class constructor
@ -51,39 +46,37 @@ public class CommentsAdapter extends ArrayAdapter<Comment> {
* *
* @param context The context of the application * @param context The context of the application
* @param comment The comment to fill * @param comment The comment to fill
* @param listener The actions update listener
* @param user Information about the user (NULL for none) * @param user Information about the user (NULL for none)
* @param viewGroup Target view group * @param viewGroup Target view group
* @return Generated view * @return Generated view
*/ */
static View getInflatedView(Context context, Comment comment, @Nullable UserInfo user, static View getInflatedView(Context context, Comment comment,
ViewGroup viewGroup){ PostsAdapter.onPostUpdate listener,
@Nullable UserInfo user, ViewGroup viewGroup){
//Create comment helper if required
if(mCHelper == null){
mCHelper = new CommentsHelper(context.getApplicationContext());
}
//Inflate a view //Inflate a view
View v = LayoutInflater.from(context).inflate(R.layout.comment_item, viewGroup, false); View v = LayoutInflater.from(context).inflate(R.layout.comment_item, viewGroup, false);
//Return filled view //Return filled view
return fillView(context, v, comment, user); return fillView(context, v, comment, user, listener);
} }
/** /**
* Fill a view with a specified comments informations * Fill a view with a specified comments informations
* *
* @param context The context of the acivitiy / application * @param context The context of the activity / application
* @param view The view to update * @param view The view to update
* @param comment The comment to update * @param comment The comment to update
* @param user Information about the user (NULL for none) * @param user Information about the user (NULL for none)
* @param listener Posts updates listener
* @return Updated view * @return Updated view
*/ */
private static View fillView(final Context context, View view, Comment comment, private static View fillView(final Context context, final View view, final Comment comment,
@Nullable UserInfo user) { @Nullable UserInfo user, final PostsAdapter.onPostUpdate listener) {
//Check wether the current user is the owner of the comment or not //Check whether the current user is the owner of the comment or not
final boolean isOwner = AccountUtils.getID(context) == comment.getUserID(); final boolean isOwner = AccountUtils.getID(context) == comment.getUserID();
//Update user name and account image //Update user name and account image
@ -105,21 +98,13 @@ public class CommentsAdapter extends ArrayAdapter<Comment> {
//Update comment actions //Update comment actions
ImageButton actions = view.findViewById(R.id.comment_actions_btn); ImageButton actions = view.findViewById(R.id.comment_actions_btn);
//Create context menu actions.setOnClickListener(new View.OnClickListener() {
actions.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { public void onClick(View v) {
listener.showCommentActions(view, comment);
//Inflate the menu
new MenuInflater(context).inflate(R.menu.menu_comments_actions, menu);
//Disable some entries accordingly to ownership status
menu.findItem(R.id.action_delete).setEnabled(isOwner);
} }
}); });
//Return view //Return view
return view; return view;

View File

@ -1,36 +1,25 @@
package org.communiquons.android.comunic.client.ui.adapters; package org.communiquons.android.comunic.client.ui.adapters;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; 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.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.Post;
import org.communiquons.android.comunic.client.data.posts.PostTypes; import org.communiquons.android.comunic.client.data.posts.PostTypes;
import org.communiquons.android.comunic.client.data.posts.PostsList; import org.communiquons.android.comunic.client.data.posts.PostsList;
import org.communiquons.android.comunic.client.data.utils.StringsUtils;
import org.communiquons.android.comunic.client.data.utils.UiUtils; import org.communiquons.android.comunic.client.data.utils.UiUtils;
import org.communiquons.android.comunic.client.data.utils.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import org.communiquons.android.comunic.client.ui.views.EditCommentContentView; import org.communiquons.android.comunic.client.ui.views.EditCommentContentView;
@ -184,7 +173,7 @@ public class PostsAdapter extends ArrayAdapter<Post>{
//Inflate the view //Inflate the view
View commentView = CommentsAdapter.getInflatedView(getContext(), comment, View commentView = CommentsAdapter.getInflatedView(getContext(), comment,
commentUser, commentsView); mListener, commentUser, commentsView);
commentsView.addView(commentView); commentsView.addView(commentView);
} }
} }
@ -269,5 +258,20 @@ public class PostsAdapter extends ArrayAdapter<Post>{
*/ */
void onCreateComment(int pos, View button, Post post, EditCommentContentView input); void onCreateComment(int pos, View button, Post post, EditCommentContentView input);
/**
* Show the available actions for a comment
*
* @param button The button that provoqued the event
* @param comment Target comment for the actions
*/
void showCommentActions(View button, Comment comment);
/**
* Handles the process of deletion of a comment.
*
* @param comment The comment to delete
*/
void deleteComment(Comment comment);
} }
} }

View File

@ -1,18 +1,24 @@
package org.communiquons.android.comunic.client.ui.fragments; package org.communiquons.android.comunic.client.ui.fragments;
import android.app.AlertDialog;
import android.app.Fragment; import android.app.Fragment;
import android.content.DialogInterface;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.Pair; import android.util.Pair;
import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.Account.AccountUtils;
import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
@ -38,6 +44,26 @@ import java.util.ArrayList;
public class PostsListFragment extends Fragment public class PostsListFragment extends Fragment
implements PostsAdapter.onPostUpdate { implements PostsAdapter.onPostUpdate {
/**
* Menu action : no action
*/
private int MENU_ACTION_NONE = 0;
/**
* Menu action : comment actions
*/
private int MENU_ACTION_COMMENTS = 1;
/**
* The current menu action
*/
private int MENU_ACTION = MENU_ACTION_NONE;
/**
* Current processed comment that context menu display actions for
*/
private Comment mCurrCommentInContextMenu;
/** /**
* The list of posts * The list of posts
*/ */
@ -214,4 +240,99 @@ public class PostsListFragment extends Fragment
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
@Override
public void showCommentActions(View button, final Comment comment) {
//Prepare context menu button rendering
button.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
//Inflate the menu
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_comments_actions, menu);
//Disable moderation actions if required
if(comment.getUserID() != AccountUtils.getID(getActivity()))
menu.findItem(R.id.action_delete).setEnabled(false);
//Save information about the comment in the fragment
MENU_ACTION = MENU_ACTION_COMMENTS;
mCurrCommentInContextMenu = comment;
}
});
//Show the context menu of the button
button.showContextMenu();
}
@Override
public void deleteComment(final Comment comment) {
//Show a confirmation dialog
new AlertDialog.Builder(getActivity())
.setTitle(R.string.popup_deletecomment_title)
.setMessage(R.string.popup_deletecomment_message)
.setNegativeButton(R.string.popup_deletecomment_cancel, null)
//Set confirmation action
.setPositiveButton(R.string.popup_deletecomment_confirm,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Mark the comment as deleted and refresh the list of comments
comment.setDeleted(true);
mPostsAdapter.notifyDataSetChanged();
//Perform a deletion request on the server
new AsyncTask<Integer, Void, Boolean>(){
@Override
protected Boolean doInBackground(Integer... params) {
return mCommentsHelper.delete(params[0]);
}
@Override
protected void onPostExecute(Boolean res) {
if(getActivity() == null)
return;
if(!res)
Toast.makeText(getActivity(), R.string.err_delete_comment,
Toast.LENGTH_SHORT).show();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, comment.getId());
}
})
.show();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
//Check if this fragment has recently created a menu or not
if(MENU_ACTION == MENU_ACTION_NONE)
return false;
//Check if a comment action context menu has been created
if(MENU_ACTION == MENU_ACTION_COMMENTS){
//Check for comment information
if(mCurrCommentInContextMenu == null)
return false;
//Check the action to perform
if(item.getItemId() == R.id.action_delete){
deleteComment(mCurrCommentInContextMenu);
return true;
}
}
return super.onContextItemSelected(item);
}
} }

View File

@ -106,4 +106,9 @@
<string name="err_create_comment">An error occurred while trying to create comment!</string> <string name="err_create_comment">An error occurred while trying to create comment!</string>
<string name="action_send">Send</string> <string name="action_send">Send</string>
<string name="action_delete_comment">Delete</string> <string name="action_delete_comment">Delete</string>
<string name="popup_deletecomment_title">Delete comment</string>
<string name="popup_deletecomment_message">Are you sure do you want to delete this comment? The operation is unrecoverable !</string>
<string name="popup_deletecomment_cancel">No</string>
<string name="popup_deletecomment_confirm">Yes</string>
<string name="err_delete_comment">An error occurred while trying to delete the comment !</string>
</resources> </resources>