From 3f858b790b54cd2a8933f88f27acc588791e925c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 4 Jul 2018 09:47:52 +0200 Subject: [PATCH] Can get older posts in latest posts fragment. --- .../client/data/helpers/PostsHelper.java | 15 +++ .../ui/fragments/LatestPostsFragment.java | 98 ++++++++++++++++++- .../ui/fragments/PostsListFragment.java | 40 +++++++- .../OnPostListFragmentsUpdateListener.java | 16 +++ .../main/res/layout/fragment_postslist.xml | 2 +- 5 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnPostListFragmentsUpdateListener.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java index 033aa5b..5052dd4 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java @@ -115,9 +115,24 @@ public class PostsHelper { */ @Nullable public PostsList get_latest() { + return get_latest(-1); + } + + /** + * Get the list of latest posts of a user + * + * @param from The ID of the newest post to start from (-1 to start from the newest post) + * @return The list of posts / null in case of failure + */ + @Nullable + public PostsList get_latest(int from) { //Perform a request on the API APIRequest params = new APIRequest(mContext, "posts/get_latest"); + //Check if we have to start from a precise post + if(from > 0) + params.addInt("startFrom", from); + //Perform the request try { 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 d9b751e..ffc3733 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 @@ -19,6 +19,7 @@ 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.UserInfo; import org.communiquons.android.comunic.client.ui.activities.MainActivity; +import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener; /** * Latest posts fragment @@ -27,7 +28,8 @@ import org.communiquons.android.comunic.client.ui.activities.MainActivity; * Created by pierre on 5/10/18. */ -public class LatestPostsFragment extends Fragment { +public class LatestPostsFragment extends Fragment + implements OnPostListFragmentsUpdateListener { /** * Debug tag @@ -54,6 +56,11 @@ public class LatestPostsFragment extends Fragment { */ ArrayMap mUserInfo; + /** + * Fragment that displays the list of posts + */ + private PostsListFragment mPostsListFragment; + /** * Loading progress bar */ @@ -64,6 +71,11 @@ public class LatestPostsFragment extends Fragment { */ TextView mNoPostNotice; + /** + * Posts load lock + */ + private boolean mLoadPostsLock = false; + @Override public void onStart() { super.onStart(); @@ -170,13 +182,14 @@ public class LatestPostsFragment extends Fragment { //Append the new posts list //Apply the post fragment - PostsListFragment postsListFragment = new PostsListFragment(); - postsListFragment.setPostsList(mPostsList); - postsListFragment.setUsersInfos(mUserInfo); + mPostsListFragment = new PostsListFragment(); + mPostsListFragment.setPostsList(mPostsList); + mPostsListFragment.setUsersInfos(mUserInfo); + mPostsListFragment.setOnPostListFragmentsUpdateListener(this); //Create and commit a transaction FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); - transaction.replace(R.id.posts_list_target, postsListFragment); + transaction.replace(R.id.posts_list_target, mPostsListFragment); transaction.commit(); } @@ -197,4 +210,79 @@ public class LatestPostsFragment extends Fragment { private void toggleNoPostNoticeVisibility(boolean visible){ mNoPostNotice.setVisibility(visible ? View.VISIBLE : View.GONE); } + + @Override + public void onLoadMorePosts() { + + //Check if post loading is already locked + if(mLoadPostsLock) + return; + + if(mPostsList == null) + return; + + if(mPostsList.size() == 0) + return; + + //Display loading bar + mLoadPostsLock = true; + toggleLoadingBarVisibility(true); + + //Get the ID of the oldest post to start from + final int start = mPostsList.get(mPostsList.size()-1).getId() - 1; + + //Get older posts + new GetOlderPosts().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start); + } + + /** + * This class get and apply older posts + */ + private class GetOlderPosts extends AsyncTask { + + @Override + protected PostsList doInBackground(Integer... id) { + + //Get the list of older posts + PostsList postsList = mPostsHelper.get_latest(id[0]); + + //Check for errors + if(postsList == null) + return null; + + //Merge posts list + mPostsList.addAll(postsList); + + //Get information about the users + ArrayMap usersInfo + = mUserHelper.getMultiple(mPostsList.getUsersId()); + + //Check for errors + if(usersInfo == null) + return null; + + //Save new user information + mUserInfo = usersInfo; + + return postsList; + } + + @Override + protected void onPostExecute(PostsList posts) { + + //Check if the activity has been detached + if(getActivity() == null) + return; + + //Unlock post loading + mLoadPostsLock = false; + toggleLoadingBarVisibility(false); + + //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 ffaf468..52de0ce 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 @@ -15,7 +15,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; -import android.widget.ListView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; @@ -32,8 +31,11 @@ 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.utils.StringsUtils; 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.OnScrollChangeDetectListener; import org.communiquons.android.comunic.client.ui.listeners.onPostUpdateListener; import org.communiquons.android.comunic.client.ui.views.EditCommentContentView; +import org.communiquons.android.comunic.client.ui.views.ScrollListView; import java.util.ArrayList; @@ -47,7 +49,7 @@ import java.util.ArrayList; */ public class PostsListFragment extends Fragment - implements onPostUpdateListener { + implements onPostUpdateListener, OnScrollChangeDetectListener { /** * Menu action : no action @@ -85,10 +87,15 @@ public class PostsListFragment extends Fragment PostsList mPostsList; /** - * Informations about the related users + * Information about the related users */ ArrayMap mUsersInfo; + /** + * Events listener + */ + private OnPostListFragmentsUpdateListener onPostListFragmentsUpdateListener = null; + /** * Post adapter */ @@ -97,14 +104,13 @@ public class PostsListFragment extends Fragment /** * The list of posts */ - ListView mListView; + ScrollListView mListView; /** * Posts helper */ PostsHelper mPostsHelper; - /** * Comments helper */ @@ -167,6 +173,7 @@ public class PostsListFragment extends Fragment //Get the list view mListView = view.findViewById(R.id.posts_list); + mListView.setOnScrollChangeDetectListener(this); //Show the posts show(); @@ -670,4 +677,27 @@ public class PostsListFragment extends Fragment return super.onContextItemSelected(item); } + + public OnPostListFragmentsUpdateListener getOnPostListFragmentsUpdateListener() { + return onPostListFragmentsUpdateListener; + } + + public void setOnPostListFragmentsUpdateListener( + OnPostListFragmentsUpdateListener onPostListFragmentsUpdateListener) { + this.onPostListFragmentsUpdateListener = onPostListFragmentsUpdateListener; + } + + @Override + public void onReachTop() { + //Nothing + } + + @Override + public void onReachBottom() { + + //Request more posts + if(onPostListFragmentsUpdateListener != null) + onPostListFragmentsUpdateListener.onLoadMorePosts(); + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnPostListFragmentsUpdateListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnPostListFragmentsUpdateListener.java new file mode 100644 index 0000000..ee9518a --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnPostListFragmentsUpdateListener.java @@ -0,0 +1,16 @@ +package org.communiquons.android.comunic.client.ui.listeners; + +/** + * Posts list fragment update listener + * + * @author Pierre HUBERT + */ + +public interface OnPostListFragmentsUpdateListener { + + /** + * This method is triggered when the user request to load more posts + */ + void onLoadMorePosts(); + +} diff --git a/app/src/main/res/layout/fragment_postslist.xml b/app/src/main/res/layout/fragment_postslist.xml index e29fecf..d48898c 100644 --- a/app/src/main/res/layout/fragment_postslist.xml +++ b/app/src/main/res/layout/fragment_postslist.xml @@ -1,5 +1,5 @@ -