diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java index 9d3358b..02673af 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java @@ -167,6 +167,15 @@ public class Post { getUser_access_level() == PostUserAccess.FULL_ACCESS; } + /** + * Check whether the current user can update the content of the current post or not + * + * @return TRUE if the post can be updated by the current user / FALSE else + */ + public boolean canUpdate(){ + return getUser_access_level() == PostUserAccess.FULL_ACCESS; + } + //Set and get file path url void setFile_path_url(String file_path_url) { this.file_path_url = file_path_url; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java index f878385..cb4fd31 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java @@ -210,6 +210,30 @@ public class PostsHelper { } } + /** + * Intend to update the content of a post + * + * @param postId The ID of the post to update + * @param content The new content for the post + * @return TRUE in case of success / FALSE else + */ + public boolean update_content(int postId, String content) { + + //Perform a request on the API + APIRequestParameters params = new APIRequestParameters(mContext, "posts/update_content"); + params.addString("new_content", content); + params.addInt("postID", postId); + + //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 post information into POST object * 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 599af65..2c1770d 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 @@ -297,6 +297,13 @@ public class PostsAdapter extends ArrayAdapter{ */ void onPostLikeUpdate(Post post, boolean is_liking); + /** + * Handles the update request of the content of a post + * + * @param post Target post + */ + void onPostContentUpdate(Post post); + /** * Handles the deletion process of a post * 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 b90217f..bfd2183 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 @@ -14,6 +14,7 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; @@ -300,6 +301,10 @@ public class PostsListFragment extends Fragment menu.findItem(R.id.action_delete).setEnabled(false); } + + //Check if the current user can update the post or not + if(!post.canUpdate()) + menu.findItem(R.id.action_edit_post).setEnabled(false); } }); @@ -325,6 +330,73 @@ public class PostsListFragment extends Fragment } + @Override + public void onPostContentUpdate(final Post post) { + + //Inflate a view + final View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_edit_post, + null); + ((EditText)view.findViewById(R.id.post_content_input)).setText(post.getContent()); + + //Create a dialog + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.popup_editpost_title) + .setNegativeButton(R.string.popup_editpost_cancel, null) + .setView(view) + + .setPositiveButton(R.string.popup_editpost_confirm, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + processPostUpdate(post, view); + } + }) + + .show(); + + } + + /** + * Process the update of a post content + * + * @param post The target post + * @param editForm The form that contains the updated post + */ + private void processPostUpdate(final Post post, View editForm){ + + //Get the content of the post + final String newContent = ((EditText)editForm.findViewById(R.id.post_content_input)).getText()+""; + + //Update the content of the post + new AsyncTask(){ + + @Override + protected Post doInBackground(Void... params) { + if(!mPostsHelper.update_content(post.getId(), newContent)) + return null; + + return mPostsHelper.getSingle(post.getId()); + } + + @Override + protected void onPostExecute(@Nullable Post newPost) { + + if(getActivity() == null) + return; + + if(newPost == null){ + Toast.makeText(getActivity(), R.string.err_update_post_content, + Toast.LENGTH_SHORT).show(); + return; + } + + //Update the content of the post + post.setContent(newPost.getContent()); + mPostsAdapter.notifyDataSetChanged(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + @Override public void deletePost(final int pos) { @@ -429,8 +501,7 @@ public class PostsListFragment extends Fragment public void onUpdateCommentContent(final Comment comment) { //Inflate the content of the dialog - View content = getActivity().getLayoutInflater().inflate(R.layout.dialog_edit_comment, - mListView); + 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()); @@ -564,6 +635,11 @@ public class PostsListFragment extends Fragment if(!(mPostsList.size() > mNumCurrPostInContextMenu)) return false; + //Edit the content of the post if required + if(item.getItemId() == R.id.action_edit_post){ + onPostContentUpdate(mPostsList.get(mNumCurrPostInContextMenu)); + } + //Check if the request is to delete the comment if(item.getItemId() == R.id.action_delete) { deletePost(mNumCurrPostInContextMenu); diff --git a/app/src/main/res/layout/dialog_edit_post.xml b/app/src/main/res/layout/dialog_edit_post.xml new file mode 100644 index 0000000..d4f475f --- /dev/null +++ b/app/src/main/res/layout/dialog_edit_post.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_post_actions.xml b/app/src/main/res/menu/menu_post_actions.xml index 127eca2..abc0340 100644 --- a/app/src/main/res/menu/menu_post_actions.xml +++ b/app/src/main/res/menu/menu_post_actions.xml @@ -1,6 +1,11 @@ + + + New content for the comment An error occurred while trying to update comment content ! Comment image + Edit + Edit the post + Cancel + Edit + New content for the post + An error occurred while trying to update the content of the post!