mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 22:09:30 +00:00
Can create comments.
This commit is contained in:
parent
88298e7517
commit
8db27d8cca
@ -1,8 +1,12 @@
|
|||||||
package org.communiquons.android.comunic.client.data.comments;
|
package org.communiquons.android.comunic.client.data.comments;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
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.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@ -18,6 +22,86 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class CommentsHelper {
|
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
|
* Parse a json array that contains comment and return the list of comments as an object
|
||||||
*
|
*
|
||||||
|
@ -12,6 +12,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
import android.widget.ImageButton;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
@ -19,9 +20,13 @@ import android.widget.TextView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.communiquons.android.comunic.client.R;
|
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.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.UsersInfo.UserInfo;
|
||||||
import org.communiquons.android.comunic.client.data.comments.Comment;
|
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.Post;
|
||||||
import org.communiquons.android.comunic.client.data.posts.PostTypes;
|
import org.communiquons.android.comunic.client.data.posts.PostTypes;
|
||||||
import org.communiquons.android.comunic.client.data.posts.PostsList;
|
import org.communiquons.android.comunic.client.data.posts.PostsList;
|
||||||
@ -56,6 +61,16 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
*/
|
*/
|
||||||
private Utilities utils;
|
private Utilities utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comments helper
|
||||||
|
*/
|
||||||
|
private CommentsHelper mCommentsHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get user helper
|
||||||
|
*/
|
||||||
|
private GetUsersHelper mUserHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the Post Adapter
|
* Create the Post Adapter
|
||||||
*
|
*
|
||||||
@ -71,6 +86,12 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
|
|
||||||
//Create utilities object
|
//Create utilities object
|
||||||
utils = new Utilities(getContext());
|
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
|
@NonNull
|
||||||
@ -221,13 +242,17 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
private void sendComment(int pos, View container){
|
private void sendComment(int pos, View container){
|
||||||
|
|
||||||
//Get post informations
|
//Get post informations
|
||||||
Post post = getItem(pos);
|
final Post post = getItem(pos);
|
||||||
if(post==null)
|
if(post==null)
|
||||||
return;
|
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
|
//Get informations about the comment
|
||||||
int postID = post.getId();
|
final int postID = post.getId();
|
||||||
String content = ((EditText) container.findViewById(R.id.input_comment_content)).getText()+"";
|
final String content = commentInput.getText()+"";
|
||||||
|
|
||||||
//Check the comment's validity
|
//Check the comment's validity
|
||||||
if(!StringsUtils.isValidForContent(content)){
|
if(!StringsUtils.isValidForContent(content)){
|
||||||
@ -236,6 +261,9 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Lock send button
|
||||||
|
sendButton.setClickable(false);
|
||||||
|
|
||||||
//Submit the comment in a separate thread
|
//Submit the comment in a separate thread
|
||||||
new AsyncTask<Void, Void, Pair<UserInfo, Comment>>(){
|
new AsyncTask<Void, Void, Pair<UserInfo, Comment>>(){
|
||||||
|
|
||||||
@ -244,11 +272,57 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
protected Pair<UserInfo, Comment> doInBackground(Void... params) {
|
protected Pair<UserInfo, Comment> doInBackground(Void... params) {
|
||||||
|
|
||||||
//Try to create the comment
|
//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<UserInfo, Comment> 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<Comment> 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);
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,29 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:orientation="vertical"
|
style="@style/PostContener"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
style="@style/PostContener">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<!-- Informations about user who created the post -->
|
<!-- Informations about user who created the post -->
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
style="@style/PostHeader"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content">
|
||||||
style="@style/PostHeader">
|
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/user_account_image"
|
android:id="@+id/user_account_image"
|
||||||
android:src="@drawable/default_account_image"
|
|
||||||
android:contentDescription="@string/user_image_description"
|
|
||||||
android:layout_width="@dimen/account_image_default_width"
|
android:layout_width="@dimen/account_image_default_width"
|
||||||
android:layout_height="@dimen/account_image_default_height" />
|
android:layout_height="@dimen/account_image_default_height"
|
||||||
|
android:contentDescription="@string/user_image_description"
|
||||||
|
android:src="@drawable/default_account_image" />
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical"
|
android:layout_gravity="center"
|
||||||
android:layout_gravity="center">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<!-- User name -->
|
<!-- User name -->
|
||||||
<TextView
|
<TextView
|
||||||
@ -53,10 +53,10 @@
|
|||||||
<!-- Post visibility level -->
|
<!-- Post visibility level -->
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/post_visibility"
|
android:id="@+id/post_visibility"
|
||||||
|
style="@style/PostVisibility"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
tools:text="Public"
|
tools:text="Public" />
|
||||||
style="@style/PostVisibility"/>
|
|
||||||
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@ -66,23 +66,48 @@
|
|||||||
android:id="@+id/post_image"
|
android:id="@+id/post_image"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:scaleType="fitCenter"
|
android:contentDescription="@string/post_image_description"
|
||||||
android:contentDescription="@string/post_image_description"/>
|
android:scaleType="fitCenter" />
|
||||||
|
|
||||||
<!-- Post content -->
|
<!-- Post content -->
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/post_content"
|
android:id="@+id/post_content"
|
||||||
|
style="@style/PostContent"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
tools:text="Post content"
|
tools:text="Post content" />
|
||||||
style="@style/PostContent" />
|
|
||||||
|
|
||||||
<!-- Post comments -->
|
<!-- Post comments -->
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:orientation="vertical"
|
|
||||||
android:id="@+id/comments_list"
|
android:id="@+id/comments_list"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical" />
|
||||||
|
|
||||||
|
<!-- Comments form -->
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/create_comment_form"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<!-- Comment hint -->
|
||||||
|
<org.communiquons.android.comunic.client.ui.views.EditCommentContentView
|
||||||
|
android:id="@+id/input_comment_content"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:hint="@string/new_comment_hint"
|
||||||
|
android:inputType="textMultiLine" />
|
||||||
|
|
||||||
|
<ImageButton
|
||||||
|
android:id="@+id/comment_send_button"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:contentDescription="@string/action_send"
|
||||||
|
android:src="@android:drawable/ic_menu_send" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
@ -93,4 +93,5 @@
|
|||||||
<string name="fragment_update_conversation_title_update">Modification de conversation</string>
|
<string name="fragment_update_conversation_title_update">Modification de conversation</string>
|
||||||
<string name="fragment_update_conversation_members_menu_delete">Supprimer</string>
|
<string name="fragment_update_conversation_members_menu_delete">Supprimer</string>
|
||||||
<string name="fragment_update_conversation_button_update">Mettre à jour</string>
|
<string name="fragment_update_conversation_button_update">Mettre à jour</string>
|
||||||
|
<string name="err_invalid_comment_content">Le commentaire saisi semble être invalide…</string>
|
||||||
</resources>
|
</resources>
|
@ -101,4 +101,8 @@
|
|||||||
<string name="post_visibility_friends">friends</string>
|
<string name="post_visibility_friends">friends</string>
|
||||||
<string name="post_visibility_private">private</string>
|
<string name="post_visibility_private">private</string>
|
||||||
<string name="post_image_description">Post image (if any)</string>
|
<string name="post_image_description">Post image (if any)</string>
|
||||||
|
<string name="new_comment_hint">New comment</string>
|
||||||
|
<string name="err_invalid_comment_content">Specified comment content seems to be invalid…</string>
|
||||||
|
<string name="err_create_comment">An error occurred while trying to create comment!</string>
|
||||||
|
<string name="action_send">Send</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user