mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-10-31 01:24:43 +00:00 
			
		
		
		
	Can update the content of a post.
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -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 | ||||
|      * | ||||
|   | ||||
| @@ -297,6 +297,13 @@ public class PostsAdapter extends ArrayAdapter<Post>{ | ||||
|          */ | ||||
|         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 | ||||
|          * | ||||
|   | ||||
| @@ -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<Void, Void, Post>(){ | ||||
|  | ||||
|             @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); | ||||
|   | ||||
							
								
								
									
										13
									
								
								app/src/main/res/layout/dialog_edit_post.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								app/src/main/res/layout/dialog_edit_post.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| <?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"> | ||||
|  | ||||
|     <!-- Post content --> | ||||
|     <EditText | ||||
|         android:id="@+id/post_content_input" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:hint="@string/edit_post_content_hint"/> | ||||
|  | ||||
| </LinearLayout> | ||||
| @@ -1,6 +1,11 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||||
|  | ||||
|     <!-- Update the content of the post --> | ||||
|     <item | ||||
|         android:id="@+id/action_edit_post" | ||||
|         android:title="@string/action_edit_post" /> | ||||
|  | ||||
|     <!-- Delete the post --> | ||||
|     <item | ||||
|         android:id="@+id/action_delete" | ||||
|   | ||||
| @@ -137,4 +137,10 @@ | ||||
|     <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> | ||||
|     <string name="comment_image_description">Comment image</string> | ||||
|     <string name="action_edit_post">Edit</string> | ||||
|     <string name="popup_editpost_title">Edit the post</string> | ||||
|     <string name="popup_editpost_cancel">Cancel</string> | ||||
|     <string name="popup_editpost_confirm">Edit</string> | ||||
|     <string name="edit_post_content_hint">New content for the post</string> | ||||
|     <string name="err_update_post_content">An error occurred while trying to update the content of the post!</string> | ||||
| </resources> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Pierre
					Pierre