mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 22:09:30 +00:00
Created onPostUpdate listener and implemented onPostComment.
This commit is contained in:
parent
06f5b7962b
commit
d04158b7e5
@ -62,14 +62,9 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
private Utilities utils;
|
private Utilities utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comments helper
|
* Actions update listener
|
||||||
*/
|
*/
|
||||||
private CommentsHelper mCommentsHelper;
|
private onPostUpdate mListener;
|
||||||
|
|
||||||
/**
|
|
||||||
* Get user helper
|
|
||||||
*/
|
|
||||||
private GetUsersHelper mUserHelper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the Post Adapter
|
* Create the Post Adapter
|
||||||
@ -77,8 +72,11 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
* @param context The context of execution of the application
|
* @param context The context of execution of the application
|
||||||
* @param list The list of posts
|
* @param list The list of posts
|
||||||
* @param usersInfos Informations about the user
|
* @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);
|
super(context, 0, list);
|
||||||
|
|
||||||
//Save the users info object
|
//Save the users info object
|
||||||
@ -87,11 +85,7 @@ 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
|
mListener = listener;
|
||||||
mUserHelper = new GetUsersHelper(getContext(), DatabaseHelper.getInstance(getContext()));
|
|
||||||
|
|
||||||
//Create comments helper object
|
|
||||||
mCommentsHelper = new CommentsHelper(context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -104,24 +98,24 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
convertView = LayoutInflater.from(getContext())
|
convertView = LayoutInflater.from(getContext())
|
||||||
.inflate(R.layout.post_item, parent, false);
|
.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);
|
Post post = getItem(position);
|
||||||
assert post != null;
|
assert post != null;
|
||||||
UserInfo userInfo = null;
|
UserInfo userInfo = null;
|
||||||
if(mUsersInfos.containsKey(post.getUserID()))
|
if(mUsersInfos.containsKey(post.getUserID()))
|
||||||
userInfo = mUsersInfos.get(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);
|
ImageView userAccountImage = convertView.findViewById(R.id.user_account_image);
|
||||||
TextView userAccountName = convertView.findViewById(R.id.user_account_name);
|
TextView userAccountName = convertView.findViewById(R.id.user_account_name);
|
||||||
|
|
||||||
//Reset user informations
|
//Reset user information
|
||||||
userAccountName.setText("");
|
userAccountName.setText("");
|
||||||
ImageLoadManager.remove(userAccountImage);
|
ImageLoadManager.remove(userAccountImage);
|
||||||
userAccountImage.setImageDrawable(UiUtils.getDrawable(getContext(),
|
userAccountImage.setImageDrawable(UiUtils.getDrawable(getContext(),
|
||||||
R.drawable.default_account_image));
|
R.drawable.default_account_image));
|
||||||
|
|
||||||
//Set user informations if available
|
//Set user information if available
|
||||||
if(userInfo != null){
|
if(userInfo != null){
|
||||||
userAccountName.setText(userInfo.getDisplayFullName());
|
userAccountName.setText(userInfo.getDisplayFullName());
|
||||||
ImageLoadManager.load(getContext(), userInfo.getAcountImageURL(), userAccountImage);
|
ImageLoadManager.load(getContext(), userInfo.getAcountImageURL(), userAccountImage);
|
||||||
@ -245,7 +239,7 @@ 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 information
|
||||||
final Post post = getItem(pos);
|
final Post post = getItem(pos);
|
||||||
if(post==null)
|
if(post==null)
|
||||||
return;
|
return;
|
||||||
@ -254,79 +248,26 @@ public class PostsAdapter extends ArrayAdapter<Post>{
|
|||||||
final EditCommentContentView commentInput = container.findViewById(R.id.input_comment_content);
|
final EditCommentContentView commentInput = container.findViewById(R.id.input_comment_content);
|
||||||
final ImageButton sendButton = container.findViewById(R.id.comment_send_button);
|
final ImageButton sendButton = container.findViewById(R.id.comment_send_button);
|
||||||
|
|
||||||
//Get informations about the comment
|
//Call interface
|
||||||
final int postID = post.getId();
|
mListener.onCreateComment(pos, sendButton, post, commentInput);
|
||||||
final String content = commentInput.getText()+"";
|
|
||||||
|
|
||||||
//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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,30 @@
|
|||||||
package org.communiquons.android.comunic.client.ui.fragments;
|
package org.communiquons.android.comunic.client.ui.fragments;
|
||||||
|
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.util.ArrayMap;
|
import android.util.ArrayMap;
|
||||||
|
import android.util.Pair;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
|
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.UsersInfo.GetUsersHelper;
|
||||||
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.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.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.adapters.PostsAdapter;
|
||||||
|
import org.communiquons.android.comunic.client.ui.views.EditCommentContentView;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Posts list fragment
|
* Posts list fragment
|
||||||
@ -23,7 +35,8 @@ import org.communiquons.android.comunic.client.ui.adapters.PostsAdapter;
|
|||||||
* Created by pierre on 3/18/18.
|
* Created by pierre on 3/18/18.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class PostsListFragment extends Fragment {
|
public class PostsListFragment extends Fragment
|
||||||
|
implements PostsAdapter.onPostUpdate {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of posts
|
* The list of posts
|
||||||
@ -45,6 +58,16 @@ public class PostsListFragment extends Fragment {
|
|||||||
*/
|
*/
|
||||||
ListView mListView;
|
ListView mListView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comments helper
|
||||||
|
*/
|
||||||
|
CommentsHelper mCommentsHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Users helper
|
||||||
|
*/
|
||||||
|
GetUsersHelper mUserHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the list of posts of the fragment
|
* Set the list of posts of the fragment
|
||||||
*
|
*
|
||||||
@ -80,15 +103,24 @@ public class PostsListFragment extends Fragment {
|
|||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the list of posts
|
||||||
|
*/
|
||||||
public void show(){
|
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
|
//Check if the list of posts is not null
|
||||||
if(mPostsList == null && mUsersInfo == null)
|
if(mPostsList == null && mUsersInfo == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//Create posts adapter (if required)
|
//Create posts adapter (if required)
|
||||||
if(mPostsAdapter == null) {
|
if(mPostsAdapter == null) {
|
||||||
mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, mUsersInfo);
|
mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, mUsersInfo, this);
|
||||||
|
|
||||||
//Connect the adapter to the view
|
//Connect the adapter to the view
|
||||||
mListView.setAdapter(mPostsAdapter);
|
mListView.setAdapter(mPostsAdapter);
|
||||||
@ -97,4 +129,89 @@ public class PostsListFragment extends Fragment {
|
|||||||
//Notify data set update
|
//Notify data set update
|
||||||
mPostsAdapter.notifyDataSetChanged();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user