Created onPostUpdate listener and implemented onPostComment.

This commit is contained in:
Pierre 2018-03-21 20:22:27 +01:00
parent 06f5b7962b
commit d04158b7e5
2 changed files with 149 additions and 91 deletions

View File

@ -62,14 +62,9 @@ public class PostsAdapter extends ArrayAdapter<Post>{
private Utilities utils;
/**
* Comments helper
* Actions update listener
*/
private CommentsHelper mCommentsHelper;
/**
* Get user helper
*/
private GetUsersHelper mUserHelper;
private onPostUpdate mListener;
/**
* Create the Post Adapter
@ -77,8 +72,11 @@ public class PostsAdapter extends ArrayAdapter<Post>{
* @param context The context of execution of the application
* @param list The list of posts
* @param usersInfos Informations about the user
* @param listener Specify the listener to perform callback actions such as create a comment
* for example
*/
public PostsAdapter(Context context, PostsList list, ArrayMap<Integer, UserInfo> usersInfos){
public PostsAdapter(Context context, PostsList list, ArrayMap<Integer, UserInfo> usersInfos,
onPostUpdate listener){
super(context, 0, list);
//Save the users info object
@ -87,11 +85,7 @@ 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);
mListener = listener;
}
@NonNull
@ -104,24 +98,24 @@ public class PostsAdapter extends ArrayAdapter<Post>{
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.post_item, parent, false);
//Get informations about the post and the user
//Get information about the post and the user
Post post = getItem(position);
assert post != null;
UserInfo userInfo = null;
if(mUsersInfos.containsKey(post.getUserID()))
userInfo = mUsersInfos.get(post.getUserID());
//Get the views related to user Informations
//Get the views related to user Information
ImageView userAccountImage = convertView.findViewById(R.id.user_account_image);
TextView userAccountName = convertView.findViewById(R.id.user_account_name);
//Reset user informations
//Reset user information
userAccountName.setText("");
ImageLoadManager.remove(userAccountImage);
userAccountImage.setImageDrawable(UiUtils.getDrawable(getContext(),
R.drawable.default_account_image));
//Set user informations if available
//Set user information if available
if(userInfo != null){
userAccountName.setText(userInfo.getDisplayFullName());
ImageLoadManager.load(getContext(), userInfo.getAcountImageURL(), userAccountImage);
@ -245,7 +239,7 @@ public class PostsAdapter extends ArrayAdapter<Post>{
*/
private void sendComment(int pos, View container){
//Get post informations
//Get post information
final Post post = getItem(pos);
if(post==null)
return;
@ -254,79 +248,26 @@ public class PostsAdapter extends ArrayAdapter<Post>{
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
final int postID = post.getId();
final String content = commentInput.getText()+"";
//Call interface
mListener.onCreateComment(pos, sendButton, post, commentInput);
//Check the comment's validity
if(!StringsUtils.isValidForContent(content)){
Toast.makeText(getContext(), R.string.err_invalid_comment_content,
Toast.LENGTH_SHORT).show();
return;
}
//Lock send button
sendButton.setClickable(false);
/**
* This interface is used to redirect post update events to the fragment managing the
* rendering of the posts
*/
public interface onPostUpdate {
//Submit the comment in a separate thread
new AsyncTask<Void, Void, Pair<UserInfo, Comment>>(){
/**
* This method is called when a comment creation request is made
*
* @param pos The position of the post in the list
* @param button The button triggered to submit comment creation
* @param post Informations about the target post
* @param input The input where the comment comment was typed
*/
void onCreateComment(int pos, View button, Post post, EditCommentContentView input);
@Override
@Nullable
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);
}
}

View File

@ -1,18 +1,30 @@
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.util.ArrayMap;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
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.UsersInfo.GetUsersHelper;
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.PostsList;
import org.communiquons.android.comunic.client.data.utils.StringsUtils;
import org.communiquons.android.comunic.client.ui.adapters.PostsAdapter;
import org.communiquons.android.comunic.client.ui.views.EditCommentContentView;
import java.util.ArrayList;
/**
* Posts list fragment
@ -23,7 +35,8 @@ import org.communiquons.android.comunic.client.ui.adapters.PostsAdapter;
* Created by pierre on 3/18/18.
*/
public class PostsListFragment extends Fragment {
public class PostsListFragment extends Fragment
implements PostsAdapter.onPostUpdate {
/**
* The list of posts
@ -45,6 +58,16 @@ public class PostsListFragment extends Fragment {
*/
ListView mListView;
/**
* Comments helper
*/
CommentsHelper mCommentsHelper;
/**
* Users helper
*/
GetUsersHelper mUserHelper;
/**
* Set the list of posts of the fragment
*
@ -80,21 +103,115 @@ public class PostsListFragment extends Fragment {
show();
}
/**
* Display the list of posts
*/
public void show(){
//Create comment helper
mCommentsHelper = new CommentsHelper(getActivity());
//Create user helper
mUserHelper = new GetUsersHelper(getActivity(), DatabaseHelper.getInstance(getActivity()));
//Check if the list of posts is not null
if(mPostsList == null && mUsersInfo == null)
return;
//Create posts adapter (if required)
if(mPostsAdapter == null) {
mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, mUsersInfo);
mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, mUsersInfo, this);
//Connect the adapter to the view
mListView.setAdapter(mPostsAdapter);
}
//Notify dataset update
//Notify data set update
mPostsAdapter.notifyDataSetChanged();
}
@Override
public void onCreateComment(int pos, final View button, final Post post,
final EditCommentContentView input) {
//Get information about the comment
final int postID = post.getId();
final String content = input.getText()+"";
//Check the comment's validity
if(!StringsUtils.isValidForContent(content)){
Toast.makeText(getActivity(), R.string.err_invalid_comment_content,
Toast.LENGTH_SHORT).show();
return;
}
//Lock send button
button.setClickable(false);
//Submit the comment in a separate thread
new AsyncTask<Void, Void, Pair<UserInfo, Comment>>(){
@Override
@Nullable
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 information 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) {
//Check if the activity has been destroyed
if(getActivity() == null)
return;
//Unlock send button
button.setClickable(true);
//Check for errors
if(userInfoCommentPair == null){
Toast.makeText(getActivity(), R.string.err_create_comment,
Toast.LENGTH_SHORT).show();
return;
}
//Empty comment input
input.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
mUsersInfo.put(userInfoCommentPair.first.getId(), userInfoCommentPair.first);
//Update data set
mPostsAdapter.notifyDataSetChanged();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}