mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 22:09:30 +00:00
Improved the way posts are retrieved on LatestPostsFragment
This commit is contained in:
parent
5f9d58f811
commit
4c36303ff9
@ -50,6 +50,15 @@ public abstract class SafeAsyncTask<Params, Progress, Result>
|
|||||||
this.onPostExecuteListener = onPostExecuteListener;
|
this.onPostExecuteListener = onPostExecuteListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the a OnPostExecuteListener has been set or not
|
||||||
|
*
|
||||||
|
* @return TRUE if it has been set / FALSE else
|
||||||
|
*/
|
||||||
|
public boolean hasOnPostExecuteListener(){
|
||||||
|
return this.onPostExecuteListener != null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Result result) {
|
protected void onPostExecute(Result result) {
|
||||||
super.onPostExecute(result);
|
super.onPostExecute(result);
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get latest posts task
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
public class GetLatestPostsTask extends SafeAsyncTask<Integer, Void, PostsList> {
|
||||||
|
|
||||||
|
public GetLatestPostsTask(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PostsList doInBackground(Integer... integers) {
|
||||||
|
|
||||||
|
//Get the list of posts
|
||||||
|
int from = integers[0] == 0 ? -1 : integers[0];
|
||||||
|
PostsList list = new PostsHelper(getContext()).get_latest(from);
|
||||||
|
if(list == null) return null;
|
||||||
|
|
||||||
|
list.setUsersInfo(new GetUsersHelper(getContext()).getMultiple(list.getUsersId()));
|
||||||
|
if(!list.hasUsersInfo()) return null;
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
@ -16,10 +16,12 @@ 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.UserInfo;
|
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.activities.MainActivity;
|
||||||
|
import org.communiquons.android.comunic.client.ui.asynctasks.GetLatestPostsTask;
|
||||||
import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener;
|
import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,27 +39,21 @@ public class LatestPostsFragment extends Fragment
|
|||||||
*/
|
*/
|
||||||
private static final String TAG = "LatestPostsFragment";
|
private static final String TAG = "LatestPostsFragment";
|
||||||
|
|
||||||
/**
|
|
||||||
* Posts helper
|
|
||||||
*/
|
|
||||||
PostsHelper mPostsHelper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User information helper
|
|
||||||
*/
|
|
||||||
GetUsersHelper mUserHelper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of posts
|
* The list of posts
|
||||||
*/
|
*/
|
||||||
PostsList mPostsList;
|
PostsList mPostsList;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fragment that displays the list of posts
|
* Fragment that displays the list of posts
|
||||||
*/
|
*/
|
||||||
private PostsListFragment mPostsListFragment;
|
private PostsListFragment mPostsListFragment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load posts task
|
||||||
|
*/
|
||||||
|
private GetLatestPostsTask mGetLatestPostsTask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loading progress bar
|
* Loading progress bar
|
||||||
*/
|
*/
|
||||||
@ -68,21 +64,6 @@ public class LatestPostsFragment extends Fragment
|
|||||||
*/
|
*/
|
||||||
TextView mNoPostNotice;
|
TextView mNoPostNotice;
|
||||||
|
|
||||||
/**
|
|
||||||
* Posts load lock
|
|
||||||
*/
|
|
||||||
private boolean mLoadPostsLock = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
|
|
||||||
//Create posts helper
|
|
||||||
mPostsHelper = new PostsHelper(getActivity());
|
|
||||||
|
|
||||||
//Create user helper
|
|
||||||
mUserHelper = new GetUsersHelper(getActivity());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
@ -110,6 +91,45 @@ public class LatestPostsFragment extends Fragment
|
|||||||
//Refresh the list of posts of the user
|
//Refresh the list of posts of the user
|
||||||
if(mPostsList == null)
|
if(mPostsList == null)
|
||||||
refresh_posts_list();
|
refresh_posts_list();
|
||||||
|
else {
|
||||||
|
mPostsListFragment = null;
|
||||||
|
show_posts_list();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
|
||||||
|
unset_all_load_tasks();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
unset_all_load_tasks();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unset all pending load tasks
|
||||||
|
*/
|
||||||
|
private void unset_all_load_tasks(){
|
||||||
|
if(mGetLatestPostsTask != null)
|
||||||
|
mGetLatestPostsTask.setOnPostExecuteListener(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether are being loaded now or not
|
||||||
|
*
|
||||||
|
* @return TRUE if some posts are loading / FALSE else
|
||||||
|
*/
|
||||||
|
private boolean is_loading_posts() {
|
||||||
|
|
||||||
|
return mGetLatestPostsTask != null &&
|
||||||
|
mGetLatestPostsTask.hasOnPostExecuteListener() &&
|
||||||
|
!mGetLatestPostsTask.isCancelled() &&
|
||||||
|
mGetLatestPostsTask.getStatus() != AsyncTask.Status.FINISHED;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -122,29 +142,15 @@ public class LatestPostsFragment extends Fragment
|
|||||||
toggleNoPostNoticeVisibility(false);
|
toggleNoPostNoticeVisibility(false);
|
||||||
|
|
||||||
//Get the list of latest posts
|
//Get the list of latest posts
|
||||||
new AsyncTask<Void, Void, PostsList>(){
|
unset_all_load_tasks();
|
||||||
|
mGetLatestPostsTask = new GetLatestPostsTask(getActivity());
|
||||||
|
mGetLatestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
|
||||||
@Override
|
@Override
|
||||||
protected PostsList doInBackground(Void... params) {
|
public void OnPostExecute(PostsList posts) {
|
||||||
PostsList postsList = mPostsHelper.get_latest();
|
|
||||||
|
|
||||||
//Get user information, if possible
|
|
||||||
if(postsList != null)
|
|
||||||
postsList.setUsersInfo(mUserHelper.getMultiple(postsList.getUsersId()));
|
|
||||||
|
|
||||||
return postsList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(PostsList posts) {
|
|
||||||
|
|
||||||
//Check if the activity still exists or not
|
|
||||||
if(getActivity() == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
on_got_new_posts_list(posts);
|
on_got_new_posts_list(posts);
|
||||||
}
|
}
|
||||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
});
|
||||||
|
mGetLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -152,30 +158,43 @@ public class LatestPostsFragment extends Fragment
|
|||||||
*
|
*
|
||||||
* @param list The new list of posts
|
* @param list The new list of posts
|
||||||
*/
|
*/
|
||||||
private void on_got_new_posts_list(@Nullable PostsList list){
|
private void on_got_new_posts_list(@Nullable PostsList list) {
|
||||||
|
|
||||||
//Hide loading bar
|
//Hide loading bar
|
||||||
toggleLoadingBarVisibility(false);
|
toggleLoadingBarVisibility(false);
|
||||||
|
|
||||||
//Check for errors
|
//Check for errors
|
||||||
if(list == null){
|
if (list == null) {
|
||||||
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(!list.hasUsersInfo()){
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Save the list of posts
|
//Save the list of posts
|
||||||
|
if (mPostsList == null)
|
||||||
mPostsList = list;
|
mPostsList = list;
|
||||||
|
else {
|
||||||
|
mPostsList.addAll(list);
|
||||||
|
assert mPostsList.getUsersInfo() != null;
|
||||||
|
mPostsList.getUsersInfo().putAll(list.getUsersInfo());
|
||||||
|
}
|
||||||
|
|
||||||
//Check if the posts list is empty
|
show_posts_list();
|
||||||
if(mPostsList.size() == 0)
|
}
|
||||||
toggleNoPostNoticeVisibility(true);
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show posts list
|
||||||
|
*/
|
||||||
|
private void show_posts_list(){
|
||||||
|
|
||||||
|
//Hide loading bar
|
||||||
|
toggleLoadingBarVisibility(false);
|
||||||
|
|
||||||
|
if(mPostsListFragment == null){
|
||||||
|
|
||||||
//Append the new posts list
|
|
||||||
//Apply the post fragment
|
//Apply the post fragment
|
||||||
mPostsListFragment = new PostsListFragment();
|
mPostsListFragment = new PostsListFragment();
|
||||||
mPostsListFragment.setPostsList(mPostsList);
|
mPostsListFragment.setPostsList(mPostsList);
|
||||||
@ -186,6 +205,43 @@ public class LatestPostsFragment extends Fragment
|
|||||||
transaction.replace(R.id.posts_list_target, mPostsListFragment);
|
transaction.replace(R.id.posts_list_target, mPostsListFragment);
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
//Append the new posts list
|
||||||
|
mPostsListFragment.show();
|
||||||
|
|
||||||
|
//Check if the posts list is empty
|
||||||
|
toggleNoPostNoticeVisibility(mPostsList.size() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoadMorePosts() {
|
||||||
|
|
||||||
|
//Check if post loading is already locked
|
||||||
|
if(is_loading_posts())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(mPostsList == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(mPostsList.size() == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
//Display loading bar
|
||||||
|
toggleLoadingBarVisibility(true);
|
||||||
|
|
||||||
|
//Get the ID of the oldest post to start from
|
||||||
|
int start = mPostsList.get(mPostsList.size()-1).getId() - 1;
|
||||||
|
|
||||||
|
//Get older posts
|
||||||
|
mGetLatestPostsTask = new GetLatestPostsTask(getActivity());
|
||||||
|
mGetLatestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
|
||||||
|
@Override
|
||||||
|
public void OnPostExecute(PostsList posts) {
|
||||||
|
on_got_new_posts_list(posts);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mGetLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Toggle progress bar visibility
|
* Toggle progress bar visibility
|
||||||
@ -204,85 +260,4 @@ public class LatestPostsFragment extends Fragment
|
|||||||
private void toggleNoPostNoticeVisibility(boolean visible){
|
private void toggleNoPostNoticeVisibility(boolean visible){
|
||||||
mNoPostNotice.setVisibility(visible ? View.VISIBLE : View.GONE);
|
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;
|
|
||||||
|
|
||||||
//Get information about the users
|
|
||||||
ArrayMap<Integer, UserInfo> usersInfo
|
|
||||||
= mUserHelper.getMultiple(postsList.getUsersId());
|
|
||||||
|
|
||||||
//Check for errors
|
|
||||||
if(usersInfo == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
assert postsList.getUsersInfo() != null;
|
|
||||||
postsList.getUsersInfo().putAll(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);
|
|
||||||
|
|
||||||
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.show();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user