From 8db27d8ccacfc19ea1915f54d33e247ba505340b Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 14 Mar 2018 18:14:53 +0100 Subject: [PATCH] Can create comments. --- .../client/data/comments/CommentsHelper.java | 84 +++++++++++++++++++ .../client/ui/adapters/PostsAdapter.java | 82 +++++++++++++++++- app/src/main/res/layout/post_item.xml | 63 +++++++++----- app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 4 + 5 files changed, 211 insertions(+), 23 deletions(-) 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 95998ad..44a581a 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 @@ -1,8 +1,12 @@ package org.communiquons.android.comunic.client.data.comments; +import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import org.communiquons.android.comunic.client.api.APIRequest; +import org.communiquons.android.comunic.client.api.APIRequestParameters; +import org.communiquons.android.comunic.client.api.APIResponse; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -18,6 +22,86 @@ import java.util.ArrayList; public class CommentsHelper { + /** + * Application context + */ + private Context mContext; + + /** + * Comments helper public constructor + * + * @param context The context of the application + */ + public CommentsHelper(Context context){ + + //Save application context (avoid to leak activities context) + mContext = context.getApplicationContext(); + + } + + + /** + * Intend to submit a new comment to a post + * + * @param postID The target post + * @param comment The content of the comment + * @return The ID of the created comment / -1 in case of failure + */ + public int send_comment(int postID, String comment){ + + //Create and perform an API request + APIRequestParameters params = new APIRequestParameters(mContext, "comments/create"); + params.addInt("postID", postID); + params.addString("content", comment); + + //Perform the request + try { + + //Try to perform the request + APIResponse response = new APIRequest().exec(params); + + //Check and return success + return response.getJSONObject().getInt("commentID"); + + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + + /** + * Get informations about a single comment + * + * @param commentID The ID of the comment to get + * @return Informations about the comment or NULL in case of failure + */ + @Nullable + public Comment getInfosSingle(int commentID){ + + //Prepare API request + APIRequestParameters params = new APIRequestParameters(mContext, "comments/get_single"); + params.addInt("commentID", commentID); + + //Perform the request + try { + + //Perform the request + APIResponse response = new APIRequest().exec(params); + + //Process result (if any) + if(response.getResponse_code() != 200) + return null; + + //Parse and return response + return parse_json_comment(response.getJSONObject()); + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + } + /** * Parse a json array that contains comment and return the list of comments as an 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 f73b570..e4075ce 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 @@ -12,6 +12,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; +import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; @@ -19,9 +20,13 @@ import android.widget.TextView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; +import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; +import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.comments.Comment; +import org.communiquons.android.comunic.client.data.comments.CommentsHelper; 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.PostsList; @@ -56,6 +61,16 @@ public class PostsAdapter extends ArrayAdapter{ */ private Utilities utils; + /** + * Comments helper + */ + private CommentsHelper mCommentsHelper; + + /** + * Get user helper + */ + private GetUsersHelper mUserHelper; + /** * Create the Post Adapter * @@ -71,6 +86,12 @@ public class PostsAdapter extends ArrayAdapter{ //Create utilities object utils = new Utilities(getContext()); + + //Create get user helper object + mUserHelper = new GetUsersHelper(getContext(), DatabaseHelper.getInstance(getContext())); + + //Create comments helper object + mCommentsHelper = new CommentsHelper(context); } @NonNull @@ -221,13 +242,17 @@ public class PostsAdapter extends ArrayAdapter{ private void sendComment(int pos, View container){ //Get post informations - Post post = getItem(pos); + final Post post = getItem(pos); if(post==null) return; + //Get view about the comment + final EditCommentContentView commentInput = container.findViewById(R.id.input_comment_content); + final ImageButton sendButton = container.findViewById(R.id.comment_send_button); + //Get informations about the comment - int postID = post.getId(); - String content = ((EditText) container.findViewById(R.id.input_comment_content)).getText()+""; + final int postID = post.getId(); + final String content = commentInput.getText()+""; //Check the comment's validity if(!StringsUtils.isValidForContent(content)){ @@ -236,6 +261,9 @@ public class PostsAdapter extends ArrayAdapter{ return; } + //Lock send button + sendButton.setClickable(false); + //Submit the comment in a separate thread new AsyncTask>(){ @@ -244,11 +272,57 @@ public class PostsAdapter extends ArrayAdapter{ protected Pair doInBackground(Void... params) { //Try to create the comment + int commentID = mCommentsHelper.send_comment(postID, content); + //Check for errors + if(commentID < 1) + return null; - return null; + //Get information about the newly created comment + Comment comment = mCommentsHelper.getInfosSingle(commentID); + + //Check for errors + if(comment == null) + return null; + + //Get informations about the related user + UserInfo user = mUserHelper.getSingle(comment.getUserID(), false); + + //Check for errors + if(user == null) + return null; + + //Return result + return Pair.create(user, comment); } + @Override + protected void onPostExecute(@Nullable Pair userInfoCommentPair) { + + //Unlock send button + sendButton.setClickable(true); + + //Check for errors + if(userInfoCommentPair == null){ + Toast.makeText(getContext(), R.string.err_create_comment, + Toast.LENGTH_SHORT).show(); + return; + } + + //Empty comment input + commentInput.setText(""); + + //Add the comment to the list + ArrayList comments = post.getComments_list(); + assert comments != null; + comments.add(userInfoCommentPair.second); + + //Add the user to the list + mUsersInfos.put(userInfoCommentPair.first.getId(), userInfoCommentPair.first); + + //Update data set + notifyDataSetChanged(); + } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } diff --git a/app/src/main/res/layout/post_item.xml b/app/src/main/res/layout/post_item.xml index b0a4b55..fbf1261 100644 --- a/app/src/main/res/layout/post_item.xml +++ b/app/src/main/res/layout/post_item.xml @@ -1,29 +1,29 @@ + android:orientation="vertical"> + android:layout_height="wrap_content"> + android:layout_height="@dimen/account_image_default_height" + android:contentDescription="@string/user_image_description" + android:src="@drawable/default_account_image" /> + android:layout_gravity="center" + android:orientation="vertical"> + tools:text="15 days ago" /> @@ -48,15 +48,15 @@ + android:layout_weight="1" /> + tools:text="Public" /> @@ -66,23 +66,48 @@ android:id="@+id/post_image" android:layout_width="match_parent" android:layout_height="wrap_content" - android:scaleType="fitCenter" - android:contentDescription="@string/post_image_description"/> + android:contentDescription="@string/post_image_description" + android:scaleType="fitCenter" /> + tools:text="Post content" /> + android:layout_height="wrap_content" + android:orientation="vertical" /> + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 6a5dd69..7405669 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -93,4 +93,5 @@ Modification de conversation Supprimer Mettre à jour + Le commentaire saisi semble être invalide… \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d9b10da..48fd616 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -101,4 +101,8 @@ friends private Post image (if any) + New comment + Specified comment content seems to be invalid… + An error occurred while trying to create comment! + Send