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;
|
||||
|
||||
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
|
||||
*
|
||||
|
@ -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<Post>{
|
||||
*/
|
||||
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<Post>{
|
||||
|
||||
//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<Post>{
|
||||
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<Post>{
|
||||
return;
|
||||
}
|
||||
|
||||
//Lock send button
|
||||
sendButton.setClickable(false);
|
||||
|
||||
//Submit the comment in a separate thread
|
||||
new AsyncTask<Void, Void, Pair<UserInfo, Comment>>(){
|
||||
|
||||
@ -244,11 +272,57 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
||||
protected Pair<UserInfo, Comment> doInBackground(Void... params) {
|
||||
|
||||
//Try to create the comment
|
||||
int commentID = mCommentsHelper.send_comment(postID, content);
|
||||
|
||||
|
||||
//Check for errors
|
||||
if(commentID < 1)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
style="@style/PostContener"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
style="@style/PostContener">
|
||||
android:orientation="vertical">
|
||||
|
||||
<!-- Informations about user who created the post -->
|
||||
<LinearLayout
|
||||
style="@style/PostHeader"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/PostHeader">
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ImageView
|
||||
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_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
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_gravity="center">
|
||||
android:layout_gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
<!-- User name -->
|
||||
<TextView
|
||||
@ -53,10 +53,10 @@
|
||||
<!-- Post visibility level -->
|
||||
<TextView
|
||||
android:id="@+id/post_visibility"
|
||||
style="@style/PostVisibility"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="Public"
|
||||
style="@style/PostVisibility"/>
|
||||
tools:text="Public" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
@ -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" />
|
||||
|
||||
<!-- Post content -->
|
||||
<TextView
|
||||
android:id="@+id/post_content"
|
||||
style="@style/PostContent"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="Post content"
|
||||
style="@style/PostContent" />
|
||||
tools:text="Post content" />
|
||||
|
||||
<!-- Post comments -->
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:id="@+id/comments_list"
|
||||
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>
|
@ -93,4 +93,5 @@
|
||||
<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_button_update">Mettre à jour</string>
|
||||
<string name="err_invalid_comment_content">Le commentaire saisi semble être invalide…</string>
|
||||
</resources>
|
@ -101,4 +101,8 @@
|
||||
<string name="post_visibility_friends">friends</string>
|
||||
<string name="post_visibility_private">private</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>
|
||||
|
Loading…
Reference in New Issue
Block a user