Can get older posts in latest posts fragment.

This commit is contained in:
Pierre HUBERT 2018-07-04 09:47:52 +02:00
parent 128b05e3df
commit 3f858b790b
5 changed files with 160 additions and 11 deletions

View File

@ -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 {

View File

@ -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<Integer, UserInfo> 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<Integer, Void, PostsList> {
@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<Integer, UserInfo> 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();
}
}
}

View File

@ -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<Integer, UserInfo> 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();
}
}

View File

@ -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();
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="@+id/posts_list"
<org.communiquons.android.comunic.client.ui.views.ScrollListView android:id="@+id/posts_list"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"