mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Improved user posts fragment
This commit is contained in:
parent
4c36303ff9
commit
9d7f811872
@ -89,11 +89,26 @@ public class PostsHelper {
|
||||
*/
|
||||
@Nullable
|
||||
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
|
||||
APIRequest params = new APIRequest(mContext, "posts/get_user");
|
||||
params.addInt("userID", userID);
|
||||
|
||||
if(from > -1)
|
||||
params.addInt("startFrom", from);
|
||||
|
||||
//Perform the request
|
||||
try {
|
||||
|
||||
|
@ -12,7 +12,7 @@ import org.communiquons.android.comunic.client.data.helpers.PostsHelper;
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
public class LoadUserPostsTask extends SafeAsyncTask<Void, Void, PostsList> {
|
||||
public class LoadUserPostsTask extends SafeAsyncTask<Integer, Void, PostsList> {
|
||||
|
||||
private int mUserID;
|
||||
|
||||
@ -29,8 +29,14 @@ public class LoadUserPostsTask extends SafeAsyncTask<Void, Void, PostsList> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PostsList doInBackground(Void... voids) {
|
||||
PostsList list = new PostsHelper(getContext()).get_user(mUserID);
|
||||
protected PostsList doInBackground(Integer ...integers) {
|
||||
|
||||
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
|
||||
if(list != null)
|
||||
|
@ -12,16 +12,16 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
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.ui.asynctasks.LoadUserPostsTask;
|
||||
import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener;
|
||||
|
||||
/**
|
||||
* User posts fragment
|
||||
@ -29,7 +29,7 @@ import org.communiquons.android.comunic.client.ui.asynctasks.LoadUserPostsTask;
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
public class UserPostsFragment extends Fragment
|
||||
implements PostsCreateFormFragment.OnPostCreated {
|
||||
implements PostsCreateFormFragment.OnPostCreated, OnPostListFragmentsUpdateListener {
|
||||
|
||||
/**
|
||||
* Bundle arguments
|
||||
@ -52,16 +52,6 @@ public class UserPostsFragment extends Fragment
|
||||
*/
|
||||
private PostsList mPostsList;
|
||||
|
||||
/**
|
||||
* Posts helper
|
||||
*/
|
||||
private PostsHelper mPostsHelper;
|
||||
|
||||
/**
|
||||
* User information helper
|
||||
*/
|
||||
private GetUsersHelper mUserHelper;
|
||||
|
||||
/**
|
||||
* Create post button
|
||||
*/
|
||||
@ -87,6 +77,11 @@ public class UserPostsFragment extends Fragment
|
||||
*/
|
||||
private PostsListFragment mPostsListFragment;
|
||||
|
||||
/**
|
||||
* Loading bar
|
||||
*/
|
||||
private ProgressBar mProgressBar;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -109,11 +104,11 @@ public class UserPostsFragment extends Fragment
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
|
||||
//Get the views
|
||||
mCreatePostButton = view.findViewById(R.id.create_post_btn);
|
||||
mCreatePostLayout = view.findViewById(R.id.create_posts_form_target);
|
||||
mNoPostNotice = view.findViewById(R.id.no_post_notice);
|
||||
mProgressBar = view.findViewById(R.id.progressBar);
|
||||
|
||||
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
|
||||
if(mCanPostsText)
|
||||
init_create_post_fragment();
|
||||
@ -137,7 +127,7 @@ public class UserPostsFragment extends Fragment
|
||||
mCreatePostButton.setVisibility(View.GONE);
|
||||
|
||||
//Load user posts
|
||||
mPostsList = new PostsList();
|
||||
mPostsList = null;
|
||||
load_posts();
|
||||
}
|
||||
|
||||
@ -147,12 +137,30 @@ public class UserPostsFragment extends Fragment
|
||||
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
|
||||
*/
|
||||
private void load_posts(){
|
||||
|
||||
cancel_load_task();
|
||||
setProgressBarVisibility(true);
|
||||
|
||||
mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity());
|
||||
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
|
||||
*/
|
||||
@UiThread
|
||||
private void apply_posts(@Nullable PostsList posts){
|
||||
|
||||
if(mPostsList == null)
|
||||
return;
|
||||
setProgressBarVisibility(false);
|
||||
|
||||
if(isStateSaved())
|
||||
return;
|
||||
@ -197,14 +199,11 @@ public class UserPostsFragment extends Fragment
|
||||
return;
|
||||
}
|
||||
|
||||
//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();
|
||||
mPostsListFragment.setPostsList(mPostsList);
|
||||
mPostsListFragment.setOnPostListFragmentsUpdateListener(this);
|
||||
|
||||
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
||||
transaction.replace(R.id.posts_list_target, mPostsListFragment);
|
||||
@ -212,9 +211,20 @@ public class UserPostsFragment extends Fragment
|
||||
}
|
||||
|
||||
|
||||
mPostsListFragment.setPostsList(mPostsList);
|
||||
mPostsListFragment.show();
|
||||
if(mPostsList != null){
|
||||
|
||||
//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);
|
||||
}
|
||||
@ -247,6 +257,15 @@ public class UserPostsFragment extends Fragment
|
||||
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
|
||||
*
|
||||
@ -263,8 +282,34 @@ public class UserPostsFragment extends Fragment
|
||||
|
||||
@Override
|
||||
public void onPostCreated(Post post) {
|
||||
mPostsList = new PostsList();
|
||||
mPostsList = null;
|
||||
load_posts();
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,21 @@
|
||||
<?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_height="match_parent"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical">
|
||||
|
||||
<Button
|
||||
android:id="@+id/create_post_btn"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/action_create_post"/>
|
||||
android:text="@string/action_create_post" />
|
||||
|
||||
<!-- Posts form target -->
|
||||
<FrameLayout
|
||||
@ -23,9 +29,9 @@
|
||||
android:id="@+id/no_post_notice"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/notice_no_post_user_page"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginTop="10dp"/>
|
||||
android:layout_marginTop="10dp"
|
||||
android:text="@string/notice_no_post_user_page" />
|
||||
|
||||
<!-- Posts target -->
|
||||
<FrameLayout
|
||||
@ -33,6 +39,22 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
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>
|
Loading…
Reference in New Issue
Block a user