mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Improved PostsList object
This commit is contained in:
parent
0e1be1f43d
commit
a1b2c8d7d8
@ -1,7 +1,11 @@
|
||||
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.Post;
|
||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -19,6 +23,11 @@ public class PostsList extends ArrayList<Post> {
|
||||
*/
|
||||
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
|
||||
*
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -82,16 +82,14 @@ public class PostsAdapter extends BaseRecyclerViewAdapter {
|
||||
*
|
||||
* @param context The context of execution of the application
|
||||
* @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
|
||||
* for example
|
||||
*/
|
||||
public PostsAdapter(Context context, PostsList list, ArrayMap<Integer, UserInfo> usersInfo,
|
||||
onPostUpdateListener listener){
|
||||
public PostsAdapter(Context context, PostsList list, onPostUpdateListener listener){
|
||||
super(context);
|
||||
|
||||
mList = list;
|
||||
mUsersInfo = usersInfo;
|
||||
mUsersInfo = list.getUsersInfo();
|
||||
|
||||
//Utilities
|
||||
mUtils = new Utilities(getContext());
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
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.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.ArrayMap;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@ -52,10 +52,6 @@ public class LatestPostsFragment extends Fragment
|
||||
*/
|
||||
PostsList mPostsList;
|
||||
|
||||
/**
|
||||
* Information about the related users
|
||||
*/
|
||||
ArrayMap<Integer, UserInfo> mUserInfo;
|
||||
|
||||
/**
|
||||
* Fragment that displays the list of posts
|
||||
@ -133,7 +129,7 @@ public class LatestPostsFragment extends Fragment
|
||||
|
||||
//Get user information, if possible
|
||||
if(postsList != null)
|
||||
mUserInfo = mUserHelper.getMultiple(postsList.getUsersId());
|
||||
postsList.setUsersInfo(mUserHelper.getMultiple(postsList.getUsersId()));
|
||||
|
||||
return postsList;
|
||||
}
|
||||
@ -166,7 +162,7 @@ public class LatestPostsFragment extends Fragment
|
||||
Toast.makeText(getActivity(), R.string.err_get_latest_posts, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
if(mUserInfo == null){
|
||||
if(!list.hasUsersInfo()){
|
||||
Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
@ -183,7 +179,6 @@ public class LatestPostsFragment extends Fragment
|
||||
//Apply the post fragment
|
||||
mPostsListFragment = new PostsListFragment();
|
||||
mPostsListFragment.setPostsList(mPostsList);
|
||||
mPostsListFragment.setUsersInfos(mUserInfo);
|
||||
mPostsListFragment.setOnPostListFragmentsUpdateListener(this);
|
||||
|
||||
//Create and commit a transaction
|
||||
@ -249,19 +244,16 @@ public class LatestPostsFragment extends Fragment
|
||||
if(postsList == null)
|
||||
return null;
|
||||
|
||||
//Merge posts list
|
||||
mPostsList.addAll(postsList);
|
||||
|
||||
//Get information about the users
|
||||
ArrayMap<Integer, UserInfo> usersInfo
|
||||
= mUserHelper.getMultiple(mPostsList.getUsersId());
|
||||
= mUserHelper.getMultiple(postsList.getUsersId());
|
||||
|
||||
//Check for errors
|
||||
if(usersInfo == null)
|
||||
return null;
|
||||
|
||||
//Save new user information
|
||||
mUserInfo = usersInfo;
|
||||
assert postsList.getUsersInfo() != null;
|
||||
postsList.getUsersInfo().putAll(usersInfo);
|
||||
|
||||
return postsList;
|
||||
}
|
||||
@ -273,13 +265,22 @@ public class LatestPostsFragment extends Fragment
|
||||
if(getActivity() == null)
|
||||
return;
|
||||
|
||||
|
||||
//Unlock post loading
|
||||
mLoadPostsLock = 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
|
||||
mPostsListFragment.setPostsList(mPostsList);
|
||||
mPostsListFragment.setUsersInfos(mUserInfo);
|
||||
mPostsListFragment.show();
|
||||
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
package org.communiquons.android.comunic.client.ui.fragments;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.widget.DividerItemDecoration;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.Pair;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
@ -21,17 +20,17 @@ import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
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.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.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.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.ui.adapters.PostsAdapter;
|
||||
import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener;
|
||||
@ -89,11 +88,6 @@ public class PostsListFragment extends Fragment
|
||||
*/
|
||||
PostsList mPostsList;
|
||||
|
||||
/**
|
||||
* Information about the related users
|
||||
*/
|
||||
ArrayMap<Integer, UserInfo> mUsersInfo;
|
||||
|
||||
/**
|
||||
* Events listener
|
||||
*/
|
||||
@ -138,15 +132,6 @@ public class PostsListFragment extends Fragment
|
||||
this.mPostsList = list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the list of users information
|
||||
*
|
||||
* @param list The list
|
||||
*/
|
||||
public void setUsersInfos(ArrayMap<Integer, UserInfo> list){
|
||||
this.mUsersInfo = list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
@ -189,13 +174,17 @@ public class PostsListFragment extends Fragment
|
||||
*/
|
||||
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
|
||||
if(mPostsList == null && mUsersInfo == null)
|
||||
if(mPostsList == null)
|
||||
return;
|
||||
|
||||
//Create posts adapter (if required)
|
||||
if(mPostsAdapter == null) {
|
||||
mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, mUsersInfo, this);
|
||||
mPostsAdapter = new PostsAdapter(getActivity(), mPostsList, this);
|
||||
|
||||
//Connect the adapter to the view
|
||||
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
@ -283,7 +272,7 @@ public class PostsListFragment extends Fragment
|
||||
comments.add(userInfoCommentPair.second);
|
||||
|
||||
//Add the user to the list
|
||||
mUsersInfo.put(userInfoCommentPair.first.getId(), userInfoCommentPair.first);
|
||||
mPostsList.getUsersInfo().put(userInfoCommentPair.first.getId(), userInfoCommentPair.first);
|
||||
|
||||
//Update data set
|
||||
mPostsAdapter.notifyDataSetChanged();
|
||||
|
@ -1,11 +1,11 @@
|
||||
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.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.ArrayMap;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@ -115,6 +115,7 @@ public class SinglePostFragment extends Fragment {
|
||||
mPostsList = new PostsList();
|
||||
mPostsList.add(mPost);
|
||||
mUserInfo = mGetUserHelper.getMultiple(mPostsList.getUsersId());
|
||||
mPostsList.setUsersInfo(mUserInfo);
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -152,7 +153,6 @@ public class SinglePostFragment extends Fragment {
|
||||
//Apply the post fragment
|
||||
PostsListFragment postsListFragment = new PostsListFragment();
|
||||
postsListFragment.setPostsList(mPostsList);
|
||||
postsListFragment.setUsersInfos(mUserInfo);
|
||||
|
||||
//Create and commit a transaction
|
||||
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
||||
|
@ -1,12 +1,12 @@
|
||||
package org.communiquons.android.comunic.client.ui.fragments;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.ArrayMap;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
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.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;
|
||||
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
|
||||
@ -51,16 +52,6 @@ public class UserPostsFragment extends Fragment
|
||||
*/
|
||||
private PostsList mPostsList;
|
||||
|
||||
/**
|
||||
* Information about the related users
|
||||
*/
|
||||
private ArrayMap<Integer, UserInfo> mUsersInfo;
|
||||
|
||||
/**
|
||||
* Post loading thread
|
||||
*/
|
||||
private Thread mLoadThread;
|
||||
|
||||
/**
|
||||
* Posts helper
|
||||
*/
|
||||
@ -86,6 +77,11 @@ public class UserPostsFragment extends Fragment
|
||||
*/
|
||||
private FrameLayout mCreatePostLayout;
|
||||
|
||||
/**
|
||||
* Load user posts task
|
||||
*/
|
||||
private LoadUserPostsTask mLoadUserPostsTask;
|
||||
|
||||
/**
|
||||
* Posts list fragment
|
||||
*/
|
||||
@ -145,70 +141,80 @@ public class UserPostsFragment extends Fragment
|
||||
load_posts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
cancel_load_task();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load user posts
|
||||
*/
|
||||
private void load_posts(){
|
||||
|
||||
mLoadThread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cancel_load_task();
|
||||
|
||||
if(mPostsList == null)
|
||||
mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity());
|
||||
mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
|
||||
@Override
|
||||
public void OnPostExecute(PostsList posts) {
|
||||
if(getActivity() == null)
|
||||
return;
|
||||
|
||||
//Get the list of posts of the user
|
||||
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();
|
||||
apply_posts(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
|
||||
private void display_posts(){
|
||||
private void apply_posts(@Nullable PostsList posts){
|
||||
|
||||
if(mPostsList == null)
|
||||
return;
|
||||
|
||||
if(isStateSaved())
|
||||
return;
|
||||
|
||||
//Check for errors
|
||||
if(mPostsList == null){
|
||||
if(posts == null){
|
||||
Toast.makeText(getActivity(), R.string.err_get_user_posts, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
//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();
|
||||
return;
|
||||
}
|
||||
|
||||
//Merge post information with existing one
|
||||
mPostsList.addAll(posts);
|
||||
assert mPostsList.getUsersInfo() != null;
|
||||
mPostsList.getUsersInfo().putAll(posts.getUsersInfo());
|
||||
|
||||
//Create fragment if required
|
||||
if(mPostsListFragment == null){
|
||||
mPostsListFragment = new PostsListFragment();
|
||||
mPostsListFragment.setPostsList(mPostsList);
|
||||
mPostsListFragment.setUsersInfos(mUsersInfo);
|
||||
|
||||
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
||||
transaction.replace(R.id.posts_list_target, mPostsListFragment);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
|
||||
mPostsListFragment.setPostsList(mPostsList);
|
||||
mPostsListFragment.show();
|
||||
|
||||
|
||||
setNoPostNoticeVisibility(mPostsList.size() < 1);
|
||||
}
|
||||
|
@ -265,4 +265,5 @@
|
||||
<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="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>
|
@ -264,4 +264,5 @@
|
||||
<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="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>
|
||||
|
Loading…
Reference in New Issue
Block a user