From 20911e9b2abd715c42a4247ec8738dec116312a4 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 7 Apr 2018 18:11:01 +0200 Subject: [PATCH] Can update comment content. --- .../client/data/comments/CommentsHelper.java | 25 +++++ .../client/ui/adapters/PostsAdapter.java | 7 ++ .../fragments/ConversationsListFragment.java | 2 +- .../ui/fragments/PostsListFragment.java | 93 ++++++++++++++++++- .../main/res/layout/dialog_edit_comment.xml | 12 +++ .../main/res/menu/menu_comments_actions.xml | 8 +- app/src/main/res/values/strings.xml | 6 ++ 7 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 app/src/main/res/layout/dialog_edit_comment.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java index e015610..6254b37 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java @@ -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 * 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 6d61203..599af65 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 @@ -320,6 +320,13 @@ public class PostsAdapter extends ArrayAdapter{ */ 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. * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java index 4f50067..eccb971 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java @@ -225,7 +225,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O //Check for errors if(usersInfo == null){ - Log.e(TAG, "Couldn't get informations about some users !"); + Log.e(TAG, "Couldn't get information about some users !"); return; } 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 2066241..fb7f11d 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 @@ -391,8 +391,10 @@ public class PostsListFragment extends Fragment inflater.inflate(R.menu.menu_comments_actions, menu); //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); + } //Save information about the comment in the fragment 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(){ + + @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 public void deleteComment(final Comment comment) { @@ -490,7 +573,13 @@ public class PostsListFragment extends Fragment if(mCurrCommentInContextMenu == null) 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){ deleteComment(mCurrCommentInContextMenu); return true; diff --git a/app/src/main/res/layout/dialog_edit_comment.xml b/app/src/main/res/layout/dialog_edit_comment.xml new file mode 100644 index 0000000..afbcabc --- /dev/null +++ b/app/src/main/res/layout/dialog_edit_comment.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_comments_actions.xml b/app/src/main/res/menu/menu_comments_actions.xml index a186228..2bf7bf6 100644 --- a/app/src/main/res/menu/menu_comments_actions.xml +++ b/app/src/main/res/menu/menu_comments_actions.xml @@ -1,6 +1,10 @@ - + + + + Like Like Liking + Edit + Edit the content of the comment + Cancel + Edit + New content for the comment + An error occurred while trying to update comment content !