From d9b8c4d98e5494a2a916f4828feb299bfc0ef9af Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 1 Apr 2018 09:51:10 +0200 Subject: [PATCH] Text posts can be created. --- .../comunic/client/data/posts/Post.java | 8 +- .../client/data/posts/PostsHelper.java | 108 ++++++++++++++++++ .../ui/fragments/PostsCreateFormFragment.java | 55 +++++++++ .../client/ui/fragments/UserPageFragment.java | 16 ++- app/src/main/res/values/strings.xml | 3 +- 5 files changed, 176 insertions(+), 14 deletions(-) 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 88a4413..29e2f2b 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 @@ -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; } 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 0c8bad5..3c90c3e 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 @@ -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 * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java index 5f79617..d1f01b0 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsCreateFormFragment.java @@ -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(){ + + @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); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java index aa53fe5..c390313 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java @@ -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(); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9ec8e31..b6aa244 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -122,5 +122,6 @@ New post… Send Show / Hide create post form. - The content of the post is too short ! + The content of the post is too short! + An error occurred while trying to create the post!