Improved PostsList object

This commit is contained in:
Pierre HUBERT 2018-08-30 16:45:51 +02:00
parent 0e1be1f43d
commit a1b2c8d7d8
9 changed files with 169 additions and 98 deletions

View File

@ -1,7 +1,11 @@
package org.communiquons.android.comunic.client.data.arrays; package org.communiquons.android.comunic.client.data.arrays;
import android.support.annotation.Nullable;
import android.util.ArrayMap;
import org.communiquons.android.comunic.client.data.models.Comment; import org.communiquons.android.comunic.client.data.models.Comment;
import org.communiquons.android.comunic.client.data.models.Post; import org.communiquons.android.comunic.client.data.models.Post;
import org.communiquons.android.comunic.client.data.models.UserInfo;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,6 +23,11 @@ public class PostsList extends ArrayList<Post> {
*/ */
private static final String TAG = "PostsList"; private static final String TAG = "PostsList";
/**
* Associated users information
*/
private ArrayMap<Integer, UserInfo> mUsersInfo = new ArrayMap<>();
/** /**
* Get the IDs of the users who created the posts and their comments * Get the IDs of the users who created the posts and their comments
* *
@ -52,4 +61,29 @@ public class PostsList extends ArrayList<Post> {
} }
/**
* Get associated user information
*
* @return Associated user information
*/
@Nullable
public ArrayMap<Integer, UserInfo> getUsersInfo() {
return mUsersInfo;
}
/**
* Set post associated users information
*
* @param usersInfo User list to set
*/
public void setUsersInfo(@Nullable ArrayMap<Integer, UserInfo> usersInfo) {
this.mUsersInfo = usersInfo;
}
/**
* Check if information about related users are present or not
*/
public boolean hasUsersInfo(){
return this.mUsersInfo != null;
}
} }

View File

@ -82,16 +82,14 @@ public class PostsAdapter extends BaseRecyclerViewAdapter {
* *
* @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 usersInfo Information about the user
* @param listener Specify the listener to perform callback actions such as create a comment * @param listener Specify the listener to perform callback actions such as create a comment
* for example * for example
*/ */
public PostsAdapter(Context context, PostsList list, ArrayMap<Integer, UserInfo> usersInfo, public PostsAdapter(Context context, PostsList list, onPostUpdateListener listener){
onPostUpdateListener listener){
super(context); super(context);
mList = list; mList = list;
mUsersInfo = usersInfo; mUsersInfo = list.getUsersInfo();
//Utilities //Utilities
mUtils = new Utilities(getContext()); mUtils = new Utilities(getContext());

View File

@ -0,0 +1,41 @@
package org.communiquons.android.comunic.client.ui.asynctasks;
import android.content.Context;
import org.communiquons.android.comunic.client.data.arrays.PostsList;
import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask;
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
import org.communiquons.android.comunic.client.data.helpers.PostsHelper;
/**
* Load user posts task
*
* @author Pierre HUBERT
*/
public class LoadUserPostsTask extends SafeAsyncTask<Void, Void, PostsList> {
private int mUserID;
/**
* Constructor
*
* @param userID The ID of the target user
* @param context Application context
*/
public LoadUserPostsTask(int userID, Context context) {
super(context);
this.mUserID = userID;
}
@Override
protected PostsList doInBackground(Void... voids) {
PostsList list = new PostsHelper(getContext()).get_user(mUserID);
//Get associated user information, if possible
if(list != null)
list.setUsersInfo(new GetUsersHelper(getContext()).getMultiple(list.getUsersId()));
return list;
}
}

View File

@ -1,11 +1,11 @@
package org.communiquons.android.comunic.client.ui.fragments; package org.communiquons.android.comunic.client.ui.fragments;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -52,10 +52,6 @@ public class LatestPostsFragment extends Fragment
*/ */
PostsList mPostsList; PostsList mPostsList;
/**
* Information about the related users
*/
ArrayMap<Integer, UserInfo> mUserInfo;
/** /**
* Fragment that displays the list of posts * Fragment that displays the list of posts
@ -133,7 +129,7 @@ public class LatestPostsFragment extends Fragment
//Get user information, if possible //Get user information, if possible
if(postsList != null) if(postsList != null)
mUserInfo = mUserHelper.getMultiple(postsList.getUsersId()); postsList.setUsersInfo(mUserHelper.getMultiple(postsList.getUsersId()));
return postsList; return postsList;
} }
@ -166,7 +162,7 @@ public class LatestPostsFragment extends Fragment
Toast.makeText(getActivity(), R.string.err_get_latest_posts, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.err_get_latest_posts, Toast.LENGTH_SHORT).show();
return; return;
} }
if(mUserInfo == null){ if(!list.hasUsersInfo()){
Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show();
return; return;
} }
@ -183,7 +179,6 @@ public class LatestPostsFragment extends Fragment
//Apply the post fragment //Apply the post fragment
mPostsListFragment = new PostsListFragment(); mPostsListFragment = new PostsListFragment();
mPostsListFragment.setPostsList(mPostsList); mPostsListFragment.setPostsList(mPostsList);
mPostsListFragment.setUsersInfos(mUserInfo);
mPostsListFragment.setOnPostListFragmentsUpdateListener(this); mPostsListFragment.setOnPostListFragmentsUpdateListener(this);
//Create and commit a transaction //Create and commit a transaction
@ -249,19 +244,16 @@ public class LatestPostsFragment extends Fragment
if(postsList == null) if(postsList == null)
return null; return null;
//Merge posts list
mPostsList.addAll(postsList);
//Get information about the users //Get information about the users
ArrayMap<Integer, UserInfo> usersInfo ArrayMap<Integer, UserInfo> usersInfo
= mUserHelper.getMultiple(mPostsList.getUsersId()); = mUserHelper.getMultiple(postsList.getUsersId());
//Check for errors //Check for errors
if(usersInfo == null) if(usersInfo == null)
return null; return null;
//Save new user information assert postsList.getUsersInfo() != null;
mUserInfo = usersInfo; postsList.getUsersInfo().putAll(usersInfo);
return postsList; return postsList;
} }
@ -273,13 +265,22 @@ public class LatestPostsFragment extends Fragment
if(getActivity() == null) if(getActivity() == null)
return; return;
//Unlock post loading //Unlock post loading
mLoadPostsLock = false; mLoadPostsLock = false;
toggleLoadingBarVisibility(false); toggleLoadingBarVisibility(false);
if(posts == null){
Toast.makeText(getActivity(), R.string.err_get_older_posts,
Toast.LENGTH_SHORT).show();
return;
}
mPostsList.addAll(posts);
assert mPostsList.getUsersInfo() != null;
mPostsList.getUsersInfo().putAll(posts.getUsersInfo());
//Apply new posts list //Apply new posts list
mPostsListFragment.setPostsList(mPostsList);
mPostsListFragment.setUsersInfos(mUserInfo);
mPostsListFragment.show(); mPostsListFragment.show();
} }

View File

@ -1,15 +1,14 @@
package org.communiquons.android.comunic.client.ui.fragments; package org.communiquons.android.comunic.client.ui.fragments;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.util.ArrayMap;
import android.util.Pair; import android.util.Pair;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -21,17 +20,17 @@ import android.widget.EditText;
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.utils.AccountUtils; import org.communiquons.android.comunic.client.data.arrays.PostsList;
import org.communiquons.android.comunic.client.data.enums.LikesType;
import org.communiquons.android.comunic.client.data.helpers.CommentsHelper;
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
import org.communiquons.android.comunic.client.data.models.UserInfo;
import org.communiquons.android.comunic.client.data.models.Comment;
import org.communiquons.android.comunic.client.data.helpers.CommentsHelper;
import org.communiquons.android.comunic.client.data.helpers.LikesHelper; import org.communiquons.android.comunic.client.data.helpers.LikesHelper;
import org.communiquons.android.comunic.client.data.enums.LikesType;
import org.communiquons.android.comunic.client.data.models.Post;
import org.communiquons.android.comunic.client.data.helpers.PostsHelper; import org.communiquons.android.comunic.client.data.helpers.PostsHelper;
import org.communiquons.android.comunic.client.data.arrays.PostsList; import org.communiquons.android.comunic.client.data.models.Comment;
import org.communiquons.android.comunic.client.data.models.Post;
import org.communiquons.android.comunic.client.data.models.UserInfo;
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
import org.communiquons.android.comunic.client.data.utils.StringsUtils; 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.listeners.OnPostListFragmentsUpdateListener; import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener;
@ -89,11 +88,6 @@ public class PostsListFragment extends Fragment
*/ */
PostsList mPostsList; PostsList mPostsList;
/**
* Information about the related users
*/
ArrayMap<Integer, UserInfo> mUsersInfo;
/** /**
* Events listener * Events listener
*/ */
@ -138,15 +132,6 @@ public class PostsListFragment extends Fragment
this.mPostsList = list; this.mPostsList = list;
} }
/**
* Set the list of users information
*
* @param list The list
*/
public void setUsersInfos(ArrayMap<Integer, UserInfo> list){
this.mUsersInfo = list;
}
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
@ -189,13 +174,17 @@ public class PostsListFragment extends Fragment
*/ */
public void show(){ public void show(){
//Check if the view has not been created yet...
if(getView() == null)
return;
//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)
return; return;
//Create posts adapter (if required) //Create posts adapter (if required)
if(mPostsAdapter == null) { if(mPostsAdapter == null) {
mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, mUsersInfo, this); mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, this);
//Connect the adapter to the view //Connect the adapter to the view
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
@ -283,7 +272,7 @@ public class PostsListFragment extends Fragment
comments.add(userInfoCommentPair.second); comments.add(userInfoCommentPair.second);
//Add the user to the list //Add the user to the list
mUsersInfo.put(userInfoCommentPair.first.getId(), userInfoCommentPair.first); mPostsList.getUsersInfo().put(userInfoCommentPair.first.getId(), userInfoCommentPair.first);
//Update data set //Update data set
mPostsAdapter.notifyDataSetChanged(); mPostsAdapter.notifyDataSetChanged();

View File

@ -1,11 +1,11 @@
package org.communiquons.android.comunic.client.ui.fragments; package org.communiquons.android.comunic.client.ui.fragments;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -115,6 +115,7 @@ public class SinglePostFragment extends Fragment {
mPostsList = new PostsList(); mPostsList = new PostsList();
mPostsList.add(mPost); mPostsList.add(mPost);
mUserInfo = mGetUserHelper.getMultiple(mPostsList.getUsersId()); mUserInfo = mGetUserHelper.getMultiple(mPostsList.getUsersId());
mPostsList.setUsersInfo(mUserInfo);
} }
return null; return null;
@ -152,7 +153,6 @@ public class SinglePostFragment extends Fragment {
//Apply the post fragment //Apply the post fragment
PostsListFragment postsListFragment = new PostsListFragment(); PostsListFragment postsListFragment = new PostsListFragment();
postsListFragment.setPostsList(mPostsList); postsListFragment.setPostsList(mPostsList);
postsListFragment.setUsersInfos(mUserInfo);
//Create and commit a transaction //Create and commit a transaction
FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

View File

@ -1,12 +1,12 @@
package org.communiquons.android.comunic.client.ui.fragments; package org.communiquons.android.comunic.client.ui.fragments;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.UiThread; import android.support.annotation.UiThread;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -17,10 +17,11 @@ 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.arrays.PostsList; import org.communiquons.android.comunic.client.data.arrays.PostsList;
import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask;
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
import org.communiquons.android.comunic.client.data.helpers.PostsHelper; import org.communiquons.android.comunic.client.data.helpers.PostsHelper;
import org.communiquons.android.comunic.client.data.models.Post; import org.communiquons.android.comunic.client.data.models.Post;
import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.ui.asynctasks.LoadUserPostsTask;
/** /**
* User posts fragment * User posts fragment
@ -51,16 +52,6 @@ public class UserPostsFragment extends Fragment
*/ */
private PostsList mPostsList; private PostsList mPostsList;
/**
* Information about the related users
*/
private ArrayMap<Integer, UserInfo> mUsersInfo;
/**
* Post loading thread
*/
private Thread mLoadThread;
/** /**
* Posts helper * Posts helper
*/ */
@ -86,6 +77,11 @@ public class UserPostsFragment extends Fragment
*/ */
private FrameLayout mCreatePostLayout; private FrameLayout mCreatePostLayout;
/**
* Load user posts task
*/
private LoadUserPostsTask mLoadUserPostsTask;
/** /**
* Posts list fragment * Posts list fragment
*/ */
@ -145,70 +141,80 @@ public class UserPostsFragment extends Fragment
load_posts(); load_posts();
} }
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
cancel_load_task();
}
/** /**
* Load user posts * Load user posts
*/ */
private void load_posts(){ private void load_posts(){
mLoadThread = new Thread(new Runnable() { cancel_load_task();
mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity());
mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@Override @Override
public void run() { public void OnPostExecute(PostsList posts) {
if(getActivity() == null)
if(mPostsList == null)
return; return;
//Get the list of posts of the user apply_posts(posts);
PostsList new_posts = mPostsHelper.get_user(mUserID);
if(mPostsList == null)
return;
mPostsList.addAll(new_posts);
if(mPostsList != null)
mUsersInfo = mUserHelper.getMultiple(mPostsList.getUsersId());
if(getActivity() != null && getView() != null)
getView().post(new Runnable() {
@Override
public void run() {
display_posts();
}
});
} }
}); });
mLoadThread.start(); mLoadUserPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
private void cancel_load_task(){
if(mLoadUserPostsTask != null)
mLoadUserPostsTask.setOnPostExecuteListener(null);
}
/** /**
* Display the list of posts * Apply the list of posts
*/ */
@UiThread @UiThread
private void display_posts(){ private void apply_posts(@Nullable PostsList posts){
if(mPostsList == null)
return;
if(isStateSaved()) if(isStateSaved())
return; return;
//Check for errors //Check for errors
if(mPostsList == null){ if(posts == null){
Toast.makeText(getActivity(), R.string.err_get_user_posts, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.err_get_user_posts, Toast.LENGTH_SHORT).show();
return; return;
} }
//Check we didn't get user information //Check we didn't get user information
if(mUsersInfo == null){ if(!posts.hasUsersInfo()){
Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show();
return; return;
} }
mPostsListFragment = new PostsListFragment(); //Merge post information with existing one
mPostsListFragment.setPostsList(mPostsList); mPostsList.addAll(posts);
mPostsListFragment.setUsersInfos(mUsersInfo); assert mPostsList.getUsersInfo() != null;
mPostsList.getUsersInfo().putAll(posts.getUsersInfo());
//Create fragment if required
if(mPostsListFragment == null){
mPostsListFragment = new PostsListFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.posts_list_target, mPostsListFragment);
transaction.commit();
}
mPostsListFragment.setPostsList(mPostsList);
mPostsListFragment.show();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.posts_list_target, mPostsListFragment);
transaction.commit();
setNoPostNoticeVisibility(mPostsList.size() < 1); setNoPostNoticeVisibility(mPostsList.size() < 1);
} }

View File

@ -265,4 +265,5 @@
<string name="err_invalid_conversation_message_content">Le contenu du message est invalide !</string> <string name="err_invalid_conversation_message_content">Le contenu du message est invalide !</string>
<string name="err_update_conversation_message_content">Une erreur a survenue lors de la mise à jour du contenu du message ! Veuillez réessayer…</string> <string name="err_update_conversation_message_content">Une erreur a survenue lors de la mise à jour du contenu du message ! Veuillez réessayer…</string>
<string name="success_update_conversation_message_content">Le contenu du message a bien été mis à jour !</string> <string name="success_update_conversation_message_content">Le contenu du message a bien été mis à jour !</string>
<string name="err_get_older_posts">Une erreur a survenue lors de la récupération de posts plus anciens !</string>
</resources> </resources>

View File

@ -264,4 +264,5 @@
<string name="err_invalid_conversation_message_content">The content of the conversation message is invalid!</string> <string name="err_invalid_conversation_message_content">The content of the conversation message is invalid!</string>
<string name="err_update_conversation_message_content">Could not update conversation message content!</string> <string name="err_update_conversation_message_content">Could not update conversation message content!</string>
<string name="success_update_conversation_message_content">The content of the conversation message has been successfully updated!</string> <string name="success_update_conversation_message_content">The content of the conversation message has been successfully updated!</string>
<string name="err_get_older_posts">Could not get older posts!</string>
</resources> </resources>