mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 07:49:28 +00:00
Can delete comments.
This commit is contained in:
parent
d04158b7e5
commit
679cfaa208
@ -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
|
||||
*
|
||||
|
@ -29,12 +29,7 @@ import java.util.ArrayList;
|
||||
* Created by pierre on 3/11/18.
|
||||
*/
|
||||
|
||||
public class CommentsAdapter extends ArrayAdapter<Comment> {
|
||||
|
||||
/**
|
||||
* Comment helper
|
||||
*/
|
||||
private static CommentsHelper mCHelper = null;
|
||||
class CommentsAdapter extends ArrayAdapter<Comment> {
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
@ -51,39 +46,37 @@ public class CommentsAdapter extends ArrayAdapter<Comment> {
|
||||
*
|
||||
* @param context The context of the application
|
||||
* @param comment The comment to fill
|
||||
* @param listener The actions update listener
|
||||
* @param user Information about the user (NULL for none)
|
||||
* @param viewGroup Target view group
|
||||
* @return Generated view
|
||||
*/
|
||||
static View getInflatedView(Context context, Comment comment, @Nullable UserInfo user,
|
||||
ViewGroup viewGroup){
|
||||
|
||||
//Create comment helper if required
|
||||
if(mCHelper == null){
|
||||
mCHelper = new CommentsHelper(context.getApplicationContext());
|
||||
}
|
||||
static View getInflatedView(Context context, Comment comment,
|
||||
PostsAdapter.onPostUpdate listener,
|
||||
@Nullable UserInfo user, ViewGroup viewGroup){
|
||||
|
||||
//Inflate a view
|
||||
View v = LayoutInflater.from(context).inflate(R.layout.comment_item, viewGroup, false);
|
||||
|
||||
//Return filled view
|
||||
return fillView(context, v, comment, user);
|
||||
return fillView(context, v, comment, user, listener);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 comment The comment to update
|
||||
* @param user Information about the user (NULL for none)
|
||||
* @param listener Posts updates listener
|
||||
* @return Updated view
|
||||
*/
|
||||
private static View fillView(final Context context, View view, Comment comment,
|
||||
@Nullable UserInfo user) {
|
||||
private static View fillView(final Context context, final View view, final Comment comment,
|
||||
@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();
|
||||
|
||||
//Update user name and account image
|
||||
@ -105,21 +98,13 @@ public class CommentsAdapter extends ArrayAdapter<Comment> {
|
||||
//Update comment actions
|
||||
ImageButton actions = view.findViewById(R.id.comment_actions_btn);
|
||||
|
||||
//Create context menu
|
||||
actions.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
|
||||
actions.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
||||
|
||||
//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);
|
||||
|
||||
public void onClick(View v) {
|
||||
listener.showCommentActions(view, comment);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//Return view
|
||||
return view;
|
||||
|
||||
|
@ -1,36 +1,25 @@
|
||||
package org.communiquons.android.comunic.client.ui.adapters;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
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.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.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.PostTypes;
|
||||
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.Utilities;
|
||||
import org.communiquons.android.comunic.client.ui.views.EditCommentContentView;
|
||||
@ -184,7 +173,7 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
||||
|
||||
//Inflate the view
|
||||
View commentView = CommentsAdapter.getInflatedView(getContext(), comment,
|
||||
commentUser, commentsView);
|
||||
mListener, commentUser, commentsView);
|
||||
commentsView.addView(commentView);
|
||||
}
|
||||
}
|
||||
@ -269,5 +258,20 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
||||
*/
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,24 @@
|
||||
package org.communiquons.android.comunic.client.ui.fragments;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Fragment;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.Pair;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
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.UsersInfo.GetUsersHelper;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
|
||||
@ -38,6 +44,26 @@ import java.util.ArrayList;
|
||||
public class PostsListFragment extends Fragment
|
||||
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
|
||||
*/
|
||||
@ -214,4 +240,99 @@ public class PostsListFragment extends Fragment
|
||||
}
|
||||
}.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);
|
||||
}
|
||||
}
|
||||
|
@ -106,4 +106,9 @@
|
||||
<string name="err_create_comment">An error occurred while trying to create comment!</string>
|
||||
<string name="action_send">Send</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>
|
||||
|
Loading…
Reference in New Issue
Block a user