diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java index 34859c1..cef8984 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java @@ -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 { */ private static final String TAG = "PostsList"; + /** + * Associated users information + */ + private ArrayMap 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 { } + /** + * Get associated user information + * + * @return Associated user information + */ + @Nullable + public ArrayMap getUsersInfo() { + return mUsersInfo; + } + + /** + * Set post associated users information + * + * @param usersInfo User list to set + */ + public void setUsersInfo(@Nullable ArrayMap usersInfo) { + this.mUsersInfo = usersInfo; + } + + /** + * Check if information about related users are present or not + */ + public boolean hasUsersInfo(){ + return this.mUsersInfo != null; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java index ab13d95..7d2f966 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java @@ -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 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()); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/LoadUserPostsTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/LoadUserPostsTask.java new file mode 100644 index 0000000..73b20b1 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/LoadUserPostsTask.java @@ -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 { + + 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; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java index ab302dc..d3f6246 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java @@ -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 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 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(); } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java index c3b88a2..e1fe370 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/PostsListFragment.java @@ -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 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 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(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java index 2cc3795..e50637d 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java @@ -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(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java index 322d856..841b675 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java @@ -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 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() { + cancel_load_task(); + + mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity()); + mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { @Override - public void run() { - - if(mPostsList == null) + 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; } - mPostsListFragment = new PostsListFragment(); - mPostsListFragment.setPostsList(mPostsList); - mPostsListFragment.setUsersInfos(mUsersInfo); + //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(); + + 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); } diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 76efe00..12ec2d2 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -265,4 +265,5 @@ Le contenu du message est invalide ! Une erreur a survenue lors de la mise à jour du contenu du message ! Veuillez réessayer… Le contenu du message a bien été mis à jour ! + Une erreur a survenue lors de la récupération de posts plus anciens ! \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3529ebb..edb0ab2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -264,4 +264,5 @@ The content of the conversation message is invalid! Could not update conversation message content! The content of the conversation message has been successfully updated! + Could not get older posts!