Improved user posts fragment

This commit is contained in:
Pierre HUBERT 2018-08-31 11:42:26 +02:00
parent 4c36303ff9
commit 9d7f811872
4 changed files with 155 additions and 67 deletions

View File

@ -89,11 +89,26 @@ public class PostsHelper {
*/ */
@Nullable @Nullable
public PostsList get_user(int userID){ public PostsList get_user(int userID){
return get_user(userID, -1);
}
/**
* Get the list of the posts of a user
*
* @param userID The ID of the user to get the post from
* @param from The post to start from (-1 not to specify)
* @return The list of posts / null in case of failure
*/
@Nullable
public PostsList get_user(int userID, int from){
//Perform a request on the API //Perform a request on the API
APIRequest params = new APIRequest(mContext, "posts/get_user"); APIRequest params = new APIRequest(mContext, "posts/get_user");
params.addInt("userID", userID); params.addInt("userID", userID);
if(from > -1)
params.addInt("startFrom", from);
//Perform the request //Perform the request
try { try {

View File

@ -12,7 +12,7 @@ import org.communiquons.android.comunic.client.data.helpers.PostsHelper;
* *
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
public class LoadUserPostsTask extends SafeAsyncTask<Void, Void, PostsList> { public class LoadUserPostsTask extends SafeAsyncTask<Integer, Void, PostsList> {
private int mUserID; private int mUserID;
@ -29,8 +29,14 @@ public class LoadUserPostsTask extends SafeAsyncTask<Void, Void, PostsList> {
} }
@Override @Override
protected PostsList doInBackground(Void... voids) { protected PostsList doInBackground(Integer ...integers) {
PostsList list = new PostsHelper(getContext()).get_user(mUserID);
PostsList list;
if(integers.length == 0)
list = new PostsHelper(getContext()).get_user(mUserID);
else
list = new PostsHelper(getContext()).get_user(mUserID, integers[0]);
//Get associated user information, if possible //Get associated user information, if possible
if(list != null) if(list != null)

View File

@ -12,16 +12,16 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; 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.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.Post;
import org.communiquons.android.comunic.client.ui.asynctasks.LoadUserPostsTask; import org.communiquons.android.comunic.client.ui.asynctasks.LoadUserPostsTask;
import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener;
/** /**
* User posts fragment * User posts fragment
@ -29,7 +29,7 @@ import org.communiquons.android.comunic.client.ui.asynctasks.LoadUserPostsTask;
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
public class UserPostsFragment extends Fragment public class UserPostsFragment extends Fragment
implements PostsCreateFormFragment.OnPostCreated { implements PostsCreateFormFragment.OnPostCreated, OnPostListFragmentsUpdateListener {
/** /**
* Bundle arguments * Bundle arguments
@ -52,16 +52,6 @@ public class UserPostsFragment extends Fragment
*/ */
private PostsList mPostsList; private PostsList mPostsList;
/**
* Posts helper
*/
private PostsHelper mPostsHelper;
/**
* User information helper
*/
private GetUsersHelper mUserHelper;
/** /**
* Create post button * Create post button
*/ */
@ -87,6 +77,11 @@ public class UserPostsFragment extends Fragment
*/ */
private PostsListFragment mPostsListFragment; private PostsListFragment mPostsListFragment;
/**
* Loading bar
*/
private ProgressBar mProgressBar;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -109,11 +104,11 @@ public class UserPostsFragment extends Fragment
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
//Get the views //Get the views
mCreatePostButton = view.findViewById(R.id.create_post_btn); mCreatePostButton = view.findViewById(R.id.create_post_btn);
mCreatePostLayout = view.findViewById(R.id.create_posts_form_target); mCreatePostLayout = view.findViewById(R.id.create_posts_form_target);
mNoPostNotice = view.findViewById(R.id.no_post_notice); mNoPostNotice = view.findViewById(R.id.no_post_notice);
mProgressBar = view.findViewById(R.id.progressBar);
setNoPostNoticeVisibility(false); setNoPostNoticeVisibility(false);
@ -125,11 +120,6 @@ public class UserPostsFragment extends Fragment
} }
}); });
//Initialize helpers
assert getActivity() != null;
mPostsHelper = new PostsHelper(getActivity());
mUserHelper = new GetUsersHelper(getActivity());
//Add create post fragment, if possible //Add create post fragment, if possible
if(mCanPostsText) if(mCanPostsText)
init_create_post_fragment(); init_create_post_fragment();
@ -137,7 +127,7 @@ public class UserPostsFragment extends Fragment
mCreatePostButton.setVisibility(View.GONE); mCreatePostButton.setVisibility(View.GONE);
//Load user posts //Load user posts
mPostsList = new PostsList(); mPostsList = null;
load_posts(); load_posts();
} }
@ -147,12 +137,30 @@ public class UserPostsFragment extends Fragment
cancel_load_task(); cancel_load_task();
} }
private void cancel_load_task(){
if(mLoadUserPostsTask != null)
mLoadUserPostsTask.setOnPostExecuteListener(null);
}
/**
* Check whether some posts are loading or not
*
* @return TRUE or FALSE
*/
private boolean is_loading_posts(){
return mLoadUserPostsTask != null
&& !mLoadUserPostsTask.isCancelled()
&& mLoadUserPostsTask.hasOnPostExecuteListener()
&& mLoadUserPostsTask.getStatus() != AsyncTask.Status.FINISHED;
}
/** /**
* Load user posts * Load user posts
*/ */
private void load_posts(){ private void load_posts(){
cancel_load_task(); cancel_load_task();
setProgressBarVisibility(true);
mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity()); mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity());
mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() { mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@ -168,19 +176,13 @@ public class UserPostsFragment extends Fragment
} }
private void cancel_load_task(){
if(mLoadUserPostsTask != null)
mLoadUserPostsTask.setOnPostExecuteListener(null);
}
/** /**
* Apply the list of posts * Apply the list of posts
*/ */
@UiThread @UiThread
private void apply_posts(@Nullable PostsList posts){ private void apply_posts(@Nullable PostsList posts){
if(mPostsList == null) setProgressBarVisibility(false);
return;
if(isStateSaved()) if(isStateSaved())
return; return;
@ -197,14 +199,11 @@ public class UserPostsFragment extends Fragment
return; return;
} }
//Merge post information with existing one
mPostsList.addAll(posts);
assert mPostsList.getUsersInfo() != null;
mPostsList.getUsersInfo().putAll(posts.getUsersInfo());
//Create fragment if required //Create fragment if required
if(mPostsListFragment == null){ if(mPostsListFragment == null){
mPostsListFragment = new PostsListFragment(); mPostsListFragment = new PostsListFragment();
mPostsListFragment.setPostsList(mPostsList);
mPostsListFragment.setOnPostListFragmentsUpdateListener(this);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.posts_list_target, mPostsListFragment); transaction.replace(R.id.posts_list_target, mPostsListFragment);
@ -212,9 +211,20 @@ public class UserPostsFragment extends Fragment
} }
mPostsListFragment.setPostsList(mPostsList); if(mPostsList != null){
mPostsListFragment.show();
//Merge post information with existing one
mPostsList.addAll(posts);
assert mPostsList.getUsersInfo() != null;
mPostsList.getUsersInfo().putAll(posts.getUsersInfo());
}
else {
mPostsList = posts;
mPostsListFragment.setPostsList(mPostsList);
}
mPostsListFragment.show();
setNoPostNoticeVisibility(mPostsList.size() < 1); setNoPostNoticeVisibility(mPostsList.size() < 1);
} }
@ -247,6 +257,15 @@ public class UserPostsFragment extends Fragment
setPostFormVisibility(false); setPostFormVisibility(false);
} }
/**
* Update progress bar visibility
*
* @param visibility TRUE for visible / FALSE else
*/
private void setProgressBarVisibility(boolean visibility){
mProgressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
}
/** /**
* Update post creation form visibility * Update post creation form visibility
* *
@ -263,8 +282,34 @@ public class UserPostsFragment extends Fragment
@Override @Override
public void onPostCreated(Post post) { public void onPostCreated(Post post) {
mPostsList = new PostsList(); mPostsList = null;
load_posts(); load_posts();
init_create_post_fragment(); init_create_post_fragment();
} }
@Override
public void onLoadMorePosts() {
if(mPostsList == null)
return;
if(mPostsList.size() < 1)
return;
if(is_loading_posts())
return;
setProgressBarVisibility(true);
mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity());
mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@Override
public void OnPostExecute(PostsList posts) {
apply_posts(posts);
}
});
mLoadUserPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
mPostsList.get(mPostsList.size() -1).getId() -1);
}
} }

View File

@ -1,8 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"> android:orientation="vertical">
<Button <Button
@ -23,9 +29,9 @@
android:id="@+id/no_post_notice" android:id="@+id/no_post_notice"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/notice_no_post_user_page"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginTop="10dp"/> android:layout_marginTop="10dp"
android:text="@string/notice_no_post_user_page" />
<!-- Posts target --> <!-- Posts target -->
<FrameLayout <FrameLayout
@ -36,3 +42,19 @@
tools:layout="@layout/fragment_postslist" /> tools:layout="@layout/fragment_postslist" />
</LinearLayout> </LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/linearLayout2"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>