Can update comment content.

This commit is contained in:
Pierre 2018-04-07 18:11:01 +02:00
parent 1096b98bd6
commit 20911e9b2a
7 changed files with 148 additions and 5 deletions

View File

@ -102,6 +102,31 @@ public class CommentsHelper {
} }
/**
* Edit the content of the comment
*
* @param commentID The ID of the comment to update
* @param content The new content of the comment
* @return TRUE for a success / FALSE else
*/
public boolean editContent(int commentID, String content){
//Perform a request on the server
APIRequestParameters params = new APIRequestParameters(mContext, "comments/edit");
params.addInt("commentID", commentID);
params.addString("content", content);
//Try to perform the request on the server
try {
APIResponse response = new APIRequest().exec(params);
return response.getResponse_code() == 200;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** /**
* Intend to delete a comment * Intend to delete a comment
* *

View File

@ -320,6 +320,13 @@ public class PostsAdapter extends ArrayAdapter<Post>{
*/ */
void onCommentLikeUpdate(Comment comment, boolean is_liking); void onCommentLikeUpdate(Comment comment, boolean is_liking);
/**
* Handles the update of the content of a comment
*
* @param comment The target comment
*/
void onUpdateCommentContent(Comment comment);
/** /**
* Handles the process of deletion of a comment. * Handles the process of deletion of a comment.
* *

View File

@ -225,7 +225,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
//Check for errors //Check for errors
if(usersInfo == null){ if(usersInfo == null){
Log.e(TAG, "Couldn't get informations about some users !"); Log.e(TAG, "Couldn't get information about some users !");
return; return;
} }

View File

@ -391,8 +391,10 @@ public class PostsListFragment extends Fragment
inflater.inflate(R.menu.menu_comments_actions, menu); inflater.inflate(R.menu.menu_comments_actions, menu);
//Disable moderation actions if required //Disable moderation actions if required
if(comment.getUserID() != AccountUtils.getID(getActivity())) if(comment.getUserID() != AccountUtils.getID(getActivity())) {
menu.findItem(R.id.action_edit_comment).setEnabled(false);
menu.findItem(R.id.action_delete).setEnabled(false); menu.findItem(R.id.action_delete).setEnabled(false);
}
//Save information about the comment in the fragment //Save information about the comment in the fragment
MENU_ACTION = MENU_ACTION_COMMENTS; MENU_ACTION = MENU_ACTION_COMMENTS;
@ -419,6 +421,87 @@ public class PostsListFragment extends Fragment
} }
@Override
public void onUpdateCommentContent(final Comment comment) {
//Inflate the content of the dialog
View content = getActivity().getLayoutInflater().inflate(R.layout.dialog_edit_comment, null);
final EditCommentContentView commentInput = content.findViewById(R.id.input_comment_content);
commentInput.setText(comment.getContent());
//Display a dialog
new AlertDialog.Builder(getActivity())
//Set general information
.setTitle(R.string.popup_editcomment_title)
.setNegativeButton(R.string.popup_editcomment_cancel, null)
.setView(content)
//Set positive action
.setPositiveButton(R.string.popup_editcomment_confirm,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
submitNewComment(comment, commentInput.getText()+"");
}
})
.show();
}
/**
* Submit the new content of the comment back to the server, after having done security check
*
* @param comment The comment to update
* @param newContent The new content for the comment
*/
private void submitNewComment(final Comment comment, final String newContent){
//Check the length of the comment
if(!StringsUtils.isValidForContent(newContent)){
Toast.makeText(getActivity(), R.string.err_invalid_comment_content,
Toast.LENGTH_SHORT).show();
return;
}
//Try to update the content of the comment on the server
new AsyncTask<Void, Void, Comment>(){
@Override
protected Comment doInBackground(Void... params) {
//Try to update the comment
if(!mCommentsHelper.editContent(comment.getId(), newContent))
return null;
//Get a new version of the comment
return mCommentsHelper.getInfosSingle(comment.getId());
}
@Override
protected void onPostExecute(@Nullable Comment newComment) {
//Check if the activity has been destroyed
if(getActivity() == null)
return;
//Check for errors
if(newComment == null){
Toast.makeText(getActivity(), R.string.err_update_comment_content,
Toast.LENGTH_SHORT).show();
return;
}
//Update the comment content
comment.setContent(newComment.getContent());
mPostsAdapter.notifyDataSetChanged();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override @Override
public void deleteComment(final Comment comment) { public void deleteComment(final Comment comment) {
@ -490,7 +573,13 @@ public class PostsListFragment extends Fragment
if(mCurrCommentInContextMenu == null) if(mCurrCommentInContextMenu == null)
return false; return false;
//Check the action to perform //Edit the comment
if(item.getItemId() == R.id.action_edit_comment){
onUpdateCommentContent(mCurrCommentInContextMenu);
return true;
}
//Delete the comment
if(item.getItemId() == R.id.action_delete){ if(item.getItemId() == R.id.action_delete){
deleteComment(mCurrCommentInContextMenu); deleteComment(mCurrCommentInContextMenu);
return true; return true;

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<org.communiquons.android.comunic.client.ui.views.EditCommentContentView
android:id="@+id/input_comment_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/popup_editcomment_edit_placeholder"/>
</LinearLayout>

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto" <menu xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Update comment -->
<item
android:id="@+id/action_edit_comment"
android:title="@string/action_edit_comment"/>
<!-- Delete comment --> <!-- Delete comment -->
<item <item

View File

@ -130,4 +130,10 @@
<string name="like_button_img">Like</string> <string name="like_button_img">Like</string>
<string name="like_view_like">Like</string> <string name="like_view_like">Like</string>
<string name="like_view_liking">Liking</string> <string name="like_view_liking">Liking</string>
<string name="action_edit_comment">Edit</string>
<string name="popup_editcomment_title">Edit the content of the comment</string>
<string name="popup_editcomment_cancel">Cancel</string>
<string name="popup_editcomment_confirm">Edit</string>
<string name="popup_editcomment_edit_placeholder">New content for the comment</string>
<string name="err_update_comment_content">An error occurred while trying to update comment content !</string>
</resources> </resources>