mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-10-30 17:14:43 +00:00 
			
		
		
		
	Text posts can be created.
This commit is contained in:
		| @@ -64,7 +64,7 @@ public class Post { | ||||
|  | ||||
|  | ||||
|     //Set and get the type of the page | ||||
|     void setPage_type(PageType page_type) { | ||||
|     public void setPage_type(PageType page_type) { | ||||
|         this.page_type = page_type; | ||||
|     } | ||||
|  | ||||
| @@ -74,7 +74,7 @@ public class Post { | ||||
|  | ||||
|  | ||||
|     //Get and set page id | ||||
|     void setPage_id(int page_id) { | ||||
|     public void setPage_id(int page_id) { | ||||
|         this.page_id = page_id; | ||||
|     } | ||||
|  | ||||
| @@ -92,7 +92,7 @@ public class Post { | ||||
|     } | ||||
|  | ||||
|     //Set and get the type of the post | ||||
|     void setType(PostTypes type) { | ||||
|     public void setType(PostTypes type) { | ||||
|         this.type = type; | ||||
|     } | ||||
|  | ||||
| @@ -102,7 +102,7 @@ public class Post { | ||||
|  | ||||
|  | ||||
|     //Set and get post visibility level | ||||
|     void setVisibilityLevel(PostVisibilityLevels visibilityLevel) { | ||||
|     public void setVisibilityLevel(PostVisibilityLevels visibilityLevel) { | ||||
|         this.visibilityLevel = visibilityLevel; | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -36,6 +36,37 @@ public class PostsHelper { | ||||
|         mContext = context; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get information about a single post | ||||
|      * | ||||
|      * @param id The ID of the target post | ||||
|      * @return Information about the post / null in case of failure | ||||
|      */ | ||||
|     @Nullable | ||||
|     public Post getSingle(int id){ | ||||
|  | ||||
|         //Perform an API request | ||||
|         APIRequestParameters params = new APIRequestParameters(mContext, "posts/get_single"); | ||||
|         params.addInt("postID", id); | ||||
|  | ||||
|         try { | ||||
|  | ||||
|             //Send the request to the server | ||||
|             APIResponse response = new APIRequest().exec(params); | ||||
|  | ||||
|             //Check for errors | ||||
|             if(response.getResponse_code() != 200) | ||||
|                 return null; | ||||
|  | ||||
|             //Parse and return information about the server | ||||
|             return parse_json_post(response.getJSONObject()); | ||||
|  | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|             return null; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the list of the posts of a user | ||||
|      * | ||||
| @@ -96,6 +127,83 @@ public class PostsHelper { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Intend to create a post | ||||
|      * | ||||
|      * @param post The post to create | ||||
|      * @return The ID of the create post / -1 in case of failure | ||||
|      */ | ||||
|     public int create(CreatePost post){ | ||||
|  | ||||
|         //Prepare the request on the server | ||||
|         APIRequestParameters params = new APIRequestParameters(mContext, "posts/create"); | ||||
|  | ||||
|         //Put basic information about the post | ||||
|         params.addString("content", post.getContent()); | ||||
|  | ||||
|         //Determine the kind of post | ||||
|         switch (post.getType()){ | ||||
|             case TEXT: | ||||
|                 params.addString("kind", "text"); | ||||
|                 break; | ||||
|  | ||||
|             default: | ||||
|                 //Throw an exception | ||||
|                 throw new RuntimeException("The kind of post is unsupported!"); | ||||
|         } | ||||
|  | ||||
|         //Determine the visibility level of the post | ||||
|         switch (post.getVisibilityLevel()){ | ||||
|  | ||||
|             case PUBLIC: | ||||
|                 params.addString("visibility", "public"); | ||||
|                 break; | ||||
|  | ||||
|             case FRIENDS: | ||||
|                 params.addString("visibility", "friends"); | ||||
|                 break; | ||||
|  | ||||
|             case PRIVATE: | ||||
|                 params.addString("visibility", "private"); | ||||
|                 break; | ||||
|  | ||||
|             default: | ||||
|                 throw new RuntimeException("Unsupported kind of Visibility level!"); | ||||
|         } | ||||
|  | ||||
|         //Set the kind of target page | ||||
|         switch (post.getPage_type()){ | ||||
|  | ||||
|             case USER_PAGE: | ||||
|                 params.addString("kind-page", "user"); | ||||
|                 break; | ||||
|  | ||||
|             default: | ||||
|                 throw new RuntimeException("Unsupported kind of page !"); | ||||
|         } | ||||
|  | ||||
|         //Set the ID of the target page | ||||
|         params.addInt("kind-id", post.getPage_id()); | ||||
|  | ||||
|         //Perform the request on the server | ||||
|         try { | ||||
|  | ||||
|             //Perform the request | ||||
|             APIResponse response = new APIRequest().exec(params); | ||||
|  | ||||
|             //Check for errors | ||||
|             if(response.getResponse_code() != 200) | ||||
|                 return -1; | ||||
|  | ||||
|             //Get and return the ID of the created post | ||||
|             return response.getJSONObject().getInt("postID"); | ||||
|  | ||||
|         } catch (Exception e){ | ||||
|             e.printStackTrace(); | ||||
|             return -1; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Parse a JSON post information into POST object | ||||
|      * | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| package org.communiquons.android.comunic.client.ui.fragments; | ||||
|  | ||||
| import android.app.Fragment; | ||||
| import android.os.AsyncTask; | ||||
| import android.os.Bundle; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.view.LayoutInflater; | ||||
| @@ -13,7 +14,10 @@ import android.widget.Toast; | ||||
|  | ||||
| import org.communiquons.android.comunic.client.R; | ||||
| import org.communiquons.android.comunic.client.data.posts.CreatePost; | ||||
| import org.communiquons.android.comunic.client.data.posts.PageType; | ||||
| 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.PostVisibilityLevels; | ||||
| import org.communiquons.android.comunic.client.data.posts.PostsHelper; | ||||
|  | ||||
| /** | ||||
| @@ -116,6 +120,57 @@ public class PostsCreateFormFragment extends Fragment { | ||||
|         //Create a post object and fill it with the required information | ||||
|         CreatePost post = new CreatePost(); | ||||
|  | ||||
|         //Determine the type and the ID of the page | ||||
|         switch (getArguments().getInt(PAGE_TYPE_ARG)){ | ||||
|             case PAGE_TYPE_USER: | ||||
|                 post.setPage_type(PageType.USER_PAGE); | ||||
|         } | ||||
|         post.setPage_id(getArguments().getInt(PAGE_ID_ARG)); | ||||
|  | ||||
|         //Set post content | ||||
|         post.setContent(""+mPostContent.getText()); | ||||
|  | ||||
|         //Default value, will be updated in a new version | ||||
|         post.setType(PostTypes.TEXT); | ||||
|         post.setVisibilityLevel(PostVisibilityLevels.FRIENDS); | ||||
|  | ||||
|         //Try to create post | ||||
|         mSendButton.setEnabled(false); | ||||
|         new AsyncTask<CreatePost, Void, Post>(){ | ||||
|  | ||||
|             @Override | ||||
|             protected Post doInBackground(CreatePost... params) { | ||||
|                 int postID = mPostHelper.create(params[0]); | ||||
|                 return postID == -1 ? null : mPostHelper.getSingle(postID); | ||||
|             } | ||||
|  | ||||
|             @Override | ||||
|             protected void onPostExecute(Post post) { | ||||
|                 if(getActivity() == null) | ||||
|                     return; | ||||
|  | ||||
|                 postCreationCallback(post); | ||||
|             } | ||||
|         }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, post); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Post creation callback | ||||
|      * | ||||
|      * @param post The created post | ||||
|      */ | ||||
|     private void postCreationCallback(@Nullable Post post){ | ||||
|  | ||||
|         //Check for errors | ||||
|         if(post == null){ | ||||
|             mSendButton.setEnabled(true); | ||||
|             Toast.makeText(getActivity(), R.string.err_submit_post, Toast.LENGTH_SHORT).show(); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         //Success - call callback (if any) | ||||
|         if(mOnPostCreated != null) | ||||
|             mOnPostCreated.onPostCreated(post); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -299,14 +299,10 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen | ||||
|         if(mPostsList == null || mUsersInfo == null) | ||||
|             return; | ||||
|  | ||||
|         if(mPostsListFragment == null) { | ||||
|  | ||||
|             //Create post fragment if requiredand display it | ||||
|             mPostsListFragment = new PostsListFragment(); | ||||
|             mPostsListFragment.setPostsList(mPostsList); | ||||
|             mPostsListFragment.setUsersInfos(mUsersInfo); | ||||
|  | ||||
|         } | ||||
|         //Create the fragment | ||||
|         mPostsListFragment = new PostsListFragment(); | ||||
|         mPostsListFragment.setPostsList(mPostsList); | ||||
|         mPostsListFragment.setUsersInfos(mUsersInfo); | ||||
|  | ||||
|         //Set the fragment | ||||
|         FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); | ||||
| @@ -325,8 +321,9 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen | ||||
|         args.putInt(PostsCreateFormFragment.PAGE_ID_ARG, mUserID); | ||||
|  | ||||
|         //Create fragment | ||||
|         Fragment fragment = new PostsCreateFormFragment(); | ||||
|         PostsCreateFormFragment fragment = new PostsCreateFormFragment(); | ||||
|         fragment.setArguments(args); | ||||
|         fragment.setOnPostCreatedListener(this); | ||||
|  | ||||
|         //Perform transaction | ||||
|         FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); | ||||
| @@ -339,6 +336,7 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen | ||||
|     public void onPostCreated(Post post) { | ||||
|         //Reload the list of post | ||||
|         mPostsList = null; | ||||
|         init_create_post_fragment(); | ||||
|         load_posts(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -122,5 +122,6 @@ | ||||
|     <string name="new_post_hint">New post…</string> | ||||
|     <string name="post_action_send">Send</string> | ||||
|     <string name="btn_show_create_form_description">Show / Hide create post form.</string> | ||||
|     <string name="err_post_content_too_short">The content of the post is too short !</string> | ||||
|     <string name="err_post_content_too_short">The content of the post is too short!</string> | ||||
|     <string name="err_submit_post">An error occurred while trying to create the post!</string> | ||||
| </resources> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Pierre
					Pierre