Made all fragments that display posts extends AbstractPostsListFragment

This commit is contained in:
Pierre HUBERT 2018-08-31 14:08:03 +02:00
parent cf763ccb18
commit ff8f791f1e
10 changed files with 404 additions and 609 deletions

View File

@ -4,9 +4,11 @@ import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Pair;
@ -16,7 +18,11 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import org.communiquons.android.comunic.client.R;
@ -33,13 +39,13 @@ import org.communiquons.android.comunic.client.data.models.UserInfo;
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
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.ScrollRecyclerView;
import java.util.ArrayList;
import java.util.Objects;
/**
* Posts list fragment
@ -50,8 +56,8 @@ import java.util.ArrayList;
* Created by pierre on 3/18/18.
*/
public class PostsListFragment extends Fragment
implements onPostUpdateListener, OnScrollChangeDetectListener {
abstract class AbstractPostsListFragment extends Fragment
implements onPostUpdateListener, OnScrollChangeDetectListener, PostsCreateFormFragment.OnPostCreated {
/**
* Menu action : no action
@ -86,51 +92,80 @@ public class PostsListFragment extends Fragment
/**
* The list of posts
*/
PostsList mPostsList;
/**
* Events listener
*/
private OnPostListFragmentsUpdateListener onPostListFragmentsUpdateListener = null;
private PostsList mPostsList;
/**
* Post adapter
*/
PostsAdapter mPostsAdapter;
/**
* The list of posts
*/
ScrollRecyclerView mRecyclerView;
private PostsAdapter mPostsAdapter;
/**
* Posts helper
*/
PostsHelper mPostsHelper;
private PostsHelper mPostsHelper;
/**
* Comments helper
*/
CommentsHelper mCommentsHelper;
private CommentsHelper mCommentsHelper;
/**
* Users helper
*/
GetUsersHelper mUserHelper;
private GetUsersHelper mUserHelper;
/**
* Likes helper
*/
LikesHelper mLikesHelper;
private LikesHelper mLikesHelper;
/**
* Set the list of posts of the fragment
*
* @param list The list of post
* Views
*/
public void setPostsList(PostsList list) {
this.mPostsList = list;
mPostsAdapter = null;
private ScrollRecyclerView mRecyclerView;
private ProgressBar mProgressBar;
private Button mCreatePostButton;
private FrameLayout mCreatePostLayout;
private TextView mNoPostsNotice;
/**
* Arguments used to create post form
*/
Bundle mCreateFormArgs;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_postslist, container, false);
}
@Override
@CallSuper
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Get views
mRecyclerView = view.findViewById(R.id.posts_list);
mProgressBar = view.findViewById(R.id.progressBar);
mCreatePostButton = view.findViewById(R.id.create_post_btn);
mCreatePostLayout = view.findViewById(R.id.create_post_form);
mNoPostsNotice = view.findViewById(R.id.no_post_notice);
//Setup posts list
mRecyclerView.setOnScrollChangeDetectListener(this);
mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(),
DividerItemDecoration.VERTICAL));
mCreatePostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setPostFormVisibility(mCreatePostLayout.getVisibility() != View.VISIBLE);
}
});
enablePostFormFragment(false);
}
@Override
@ -148,32 +183,103 @@ public class PostsListFragment extends Fragment
//Create likes helper
mLikesHelper = new LikesHelper(getActivity());
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_postslist, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
public void onResume() {
super.onResume();
//Get the list view
mRecyclerView = view.findViewById(R.id.posts_list);
mRecyclerView.setOnScrollChangeDetectListener(this);
mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(),
DividerItemDecoration.VERTICAL));
if(mPostsList == null) {
setProgressBarVisibility(true);
setNoPostsNoticeVisibility(false);
onLoadPosts();
}
else{
setPostsList(getPostsList());
show_posts();
}
}
//Show the posts
show();
/**
* Set the list of posts of the fragment
*
* @param list The list of post
*/
protected void setPostsList(PostsList list) {
this.mPostsList = list;
mPostsAdapter = null;
}
/**
* Get the current list of posts
*
* @return The current list of post / null if none
*/
@NonNull
protected PostsList getPostsList(){
return this.mPostsList;
}
/**
* Check if a PostList has been set or not
*
* @return True if a list has been set / FALSE else
*/
protected boolean hasPostsList(){
return this.mPostsList != null;
}
/**
* Load posts
*/
public abstract void onLoadPosts();
/**
* Load more posts
*
* @param last_post_id The ID of the last post in the list
*/
public abstract void onLoadMorePosts(int last_post_id);
/**
* This method is triggered when we have got a new list of posts to apply
*
* @param list The list
*/
@CallSuper
protected void onGotNewPosts(@Nullable PostsList list){
setProgressBarVisibility(false);
if(getActivity() == null)
return;
if(list == null){
Toast.makeText(getActivity(), R.string.err_get_posts_list, Toast.LENGTH_SHORT).show();
return;
}
if(!list.hasUsersInfo()){
Toast.makeText(getActivity(), R.string.err_get_user_info, Toast.LENGTH_SHORT).show();
return;
}
if(mPostsList == null)
mPostsList = list;
else {
//Merge posts list
mPostsList.addAll(list);
Objects.requireNonNull(mPostsList.getUsersInfo()).putAll(list.getUsersInfo());
}
show_posts();
}
/**
* Display the list of posts
*/
public void show(){
public void show_posts(){
//Check if the view has not been created yet...
if(getView() == null)
@ -195,8 +301,126 @@ public class PostsListFragment extends Fragment
//Notify data set update
mPostsAdapter.notifyDataSetChanged();
//Update no post notice visibility
setProgressBarVisibility(false);
setNoPostsNoticeVisibility(mPostsList.size() == 0);
}
/**
* Get the ID of the last post in the list
*
* @return The ID of the last post in the list / -1 in case of failure
*/
public int getLastPostID(){
if(!hasPostsList())
return -1;
if(getPostsList().size() < 1)
return -1;
return getPostsList().get(getPostsList().size() - 1).getId();
}
@Override
public void onReachTop() {
//Nothing
}
@Override
public void onReachBottom() {
if(hasPostsList() && getPostsList().size() > 0)
onLoadMorePosts(getLastPostID());
}
protected void setProgressBarVisibility(boolean visible){
mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
}
protected void setNoPostsNoticeVisibility(boolean visible){
mNoPostsNotice.setVisibility(visible ? View.VISIBLE : View.GONE);
}
//
//
//
//
// Posts form
//
//
//
//
/**
* Specify whether post form should be enabled or not
*
* @param enable TRUE to enable / FALSE else
*/
protected void enablePostFormFragment(boolean enable){
mCreatePostButton.setVisibility(enable ? View.VISIBLE : View.GONE);
}
/**
* Initialize posts fragment
*
* @param page_type The type of the page
* @param page_id The ID of the page
*/
protected void init_create_post_fragment(int page_type, int page_id){
//Can not perform a transaction if the state has been saved
if(isStateSaved())
return;
//Create bundle
Bundle args = new Bundle();
args.putInt(PostsCreateFormFragment.PAGE_TYPE_ARG, page_type);
args.putInt(PostsCreateFormFragment.PAGE_ID_ARG, page_id);
mCreateFormArgs = new Bundle(args);
//Create fragment
PostsCreateFormFragment fragment = new PostsCreateFormFragment();
fragment.setArguments(args);
fragment.setOnPostCreatedListener(this);
//Perform transaction
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.create_post_form, fragment);
transaction.commit();
//Hide the post form by default
setPostFormVisibility(false);
}
/**
* Set post form visibility
*/
protected void setPostFormVisibility(boolean visible){
mCreatePostLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
}
@Override
public void onPostCreated(Post post) {
init_create_post_fragment(mCreateFormArgs.getInt(PostsCreateFormFragment.PAGE_TYPE_ARG),
mCreateFormArgs.getInt(PostsCreateFormFragment.PAGE_ID_ARG));
setPostsList(null);
setProgressBarVisibility(true);
setNoPostsNoticeVisibility(false);
onLoadPosts();
}
//
//
//
//
// Posts actions
//
//
//
//
@Override
public void onCreateComment(int pos, final View button, final Post post,
final EditCommentContentView input) {
@ -673,27 +897,4 @@ 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

@ -1,28 +1,14 @@
package org.communiquons.android.comunic.client.ui.fragments;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.UserInfo;
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;
/**
* Latest posts fragment
@ -31,55 +17,18 @@ import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsU
* Created by pierre on 5/10/18.
*/
public class LatestPostsFragment extends Fragment
implements OnPostListFragmentsUpdateListener {
public class LatestPostsFragment extends AbstractPostsListFragment {
/**
* Debug tag
*/
private static final String TAG = "LatestPostsFragment";
/**
* The list of posts
*/
PostsList mPostsList;
/**
* Fragment that displays the list of posts
*/
private PostsListFragment mPostsListFragment;
/**
* Load posts task
*/
private GetLatestPostsTask mGetLatestPostsTask;
/**
* Loading progress bar
*/
ProgressBar mProgressBar;
/**
* No posts notice
*/
TextView mNoPostNotice;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_latest_posts, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Get some of the views
mProgressBar = view.findViewById(R.id.loading_progress);
mNoPostNotice = view.findViewById(R.id.no_post_notice);
}
@Override
public void onResume() {
super.onResume();
@ -87,28 +36,14 @@ public class LatestPostsFragment extends Fragment
//Update dock and activity title
getActivity().setTitle(R.string.fragment_latestposts_title);
MainActivity.SetNavbarSelectedOption(getActivity(), R.id.action_latest_posts);
//Refresh the list of posts of the user
if(mPostsList == null)
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
@ -132,36 +67,53 @@ public class LatestPostsFragment extends Fragment
}
/**
* Refresh the list of posts of the user
*/
private void refresh_posts_list(){
//Show progress bar / hide no posts notice
toggleLoadingBarVisibility(true);
toggleNoPostNoticeVisibility(false);
@Override
public void onLoadPosts() {
//Get the list of latest posts
unset_all_load_tasks();
mGetLatestPostsTask = new GetLatestPostsTask(getActivity());
mGetLatestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@Override
public void OnPostExecute(PostsList posts) {
on_got_new_posts_list(posts);
onGotNewPosts(posts);
}
});
mGetLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 0);
}
/**
* This function is called when we have got a new post list
*
* @param list The new list of posts
*/
private void on_got_new_posts_list(@Nullable PostsList list) {
@Override
public void onLoadMorePosts(int last_post_id) {
//Hide loading bar
toggleLoadingBarVisibility(false);
//Check if post loading is already locked
if(is_loading_posts())
return;
if(!hasPostsList())
return;
if(getPostsList().size() == 0)
return;
setProgressBarVisibility(true);
//Get the ID of the oldest post to start from
int start = last_post_id - 1;
//Get older posts
mGetLatestPostsTask = new GetLatestPostsTask(getActivity());
mGetLatestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@Override
public void OnPostExecute(PostsList posts) {
onGotNewPosts(posts);
}
});
mGetLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start);
}
@Override
protected void onGotNewPosts(@Nullable PostsList list) {
setProgressBarVisibility(false);
//Check for errors
if (list == null) {
@ -173,91 +125,7 @@ public class LatestPostsFragment extends Fragment
return;
}
//Save the list of posts
if (mPostsList == null)
mPostsList = list;
else {
mPostsList.addAll(list);
assert mPostsList.getUsersInfo() != null;
mPostsList.getUsersInfo().putAll(list.getUsersInfo());
super.onGotNewPosts(list);
}
show_posts_list();
}
/**
* Show posts list
*/
private void show_posts_list(){
//Hide loading bar
toggleLoadingBarVisibility(false);
if(mPostsListFragment == null){
//Apply the post fragment
mPostsListFragment = new PostsListFragment();
mPostsListFragment.setPostsList(mPostsList);
mPostsListFragment.setOnPostListFragmentsUpdateListener(this);
//Create and commit a transaction
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.posts_list_target, mPostsListFragment);
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
*
* @param visible Specify whether the progress bar should be visible or not
*/
private void toggleLoadingBarVisibility(boolean visible){
mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
}
/**
* Toggle no post notice visibility
*
* @param visible The visibility of the no post notice
*/
private void toggleNoPostNoticeVisibility(boolean visible){
mNoPostNotice.setVisibility(visible ? View.VISIBLE : View.GONE);
}
}

View File

@ -2,13 +2,7 @@ package org.communiquons.android.comunic.client.ui.fragments;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import org.communiquons.android.comunic.client.R;
@ -25,7 +19,7 @@ import org.communiquons.android.comunic.client.ui.asynctasks.GetSinglePostTask;
* Created by pierre on 4/12/18.
*/
public class SinglePostFragment extends Fragment {
public class SinglePostFragment extends AbstractPostsListFragment {
/**
* The name of the argument that contains the ID of the post to open
@ -37,11 +31,6 @@ public class SinglePostFragment extends Fragment {
*/
private int mPostID = 0;
/**
* Post list that contains only a single post
*/
private PostsList mPostsList;
/**
* Get single post task
*/
@ -52,28 +41,11 @@ public class SinglePostFragment extends Fragment {
super.onCreate(savedInstanceState);
//Get post ID
assert getArguments() != null;
mPostID = getArguments().getInt(ARGUMENT_POST_ID);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_single_post, container, false);
}
@Override
public void onResume() {
super.onResume();
//Check if the fragment contains information about the post
if(mPostsList == null){
getPostInfo();
}
else
show_posts();
}
@Override
public void onStop() {
super.onStop();
@ -85,11 +57,8 @@ public class SinglePostFragment extends Fragment {
mGetSinglePostTask.setOnPostExecuteListener(null);
}
/**
* Get information about the post and its related users
*/
private void getPostInfo(){
@Override
public void onLoadPosts() {
//Perform the request in the background
unset_all_tasks();
@ -97,18 +66,22 @@ public class SinglePostFragment extends Fragment {
mGetSinglePostTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@Override
public void OnPostExecute(PostsList posts) {
onGotPostInfo(posts);
onGotNewPosts(posts);
}
});
mGetSinglePostTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mPostID);
}
/**
* This method is triggered once we got information about the post
*/
private void onGotPostInfo(@Nullable PostsList list) {
@Override
public void onLoadMorePosts(int last_post_id) {
}
@Override
protected void onGotNewPosts(@Nullable PostsList list) {
setProgressBarVisibility(false);
//Check if we did not get post information
if (list == null) {
Toast.makeText(getActivity(), R.string.err_get_post_info, Toast.LENGTH_SHORT).show();
return;
@ -120,25 +93,7 @@ public class SinglePostFragment extends Fragment {
return;
}
mPostsList = list;
show_posts();
super.onGotNewPosts(list);
}
/**
* Show the list of posts
*/
private void show_posts(){
//Apply the post fragment
PostsListFragment postsListFragment = new PostsListFragment();
postsListFragment.setPostsList(mPostsList);
//Create and commit a transaction
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.posts_list_target, postsListFragment);
transaction.commit();
}
}

View File

@ -4,16 +4,8 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
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;
@ -21,15 +13,13 @@ 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.models.Post;
import org.communiquons.android.comunic.client.ui.asynctasks.LoadUserPostsTask;
import org.communiquons.android.comunic.client.ui.listeners.OnPostListFragmentsUpdateListener;
/**
* User posts fragment
*
* @author Pierre HUBERT
*/
public class UserPostsFragment extends Fragment
implements PostsCreateFormFragment.OnPostCreated, OnPostListFragmentsUpdateListener {
public class UserPostsFragment extends AbstractPostsListFragment {
/**
* Bundle arguments
@ -47,41 +37,11 @@ public class UserPostsFragment extends Fragment
*/
private boolean mCanPostsText;
/**
* The list of posts of the user
*/
private PostsList mPostsList;
/**
* Create post button
*/
private Button mCreatePostButton;
/**
* No post on user page notice
*/
private TextView mNoPostNotice;
/**
* Create post layout
*/
private FrameLayout mCreatePostLayout;
/**
* Load user posts task
*/
private LoadUserPostsTask mLoadUserPostsTask;
/**
* Posts list fragment
*/
private PostsListFragment mPostsListFragment;
/**
* Loading bar
*/
private ProgressBar mProgressBar;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -94,41 +54,14 @@ public class UserPostsFragment extends Fragment
mCanPostsText = bundle.getBoolean(ARGUMENT_CAN_POST_TEXT);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_postswithform, container, false);
}
@Override
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);
mCreatePostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Invert form visibility
setPostFormVisibility(mCreatePostLayout.getVisibility() != View.VISIBLE);
}
});
//Add create post fragment, if possible
enablePostFormFragment(mCanPostsText);
if(mCanPostsText)
init_create_post_fragment();
else
mCreatePostButton.setVisibility(View.GONE);
//Load user posts
mPostsList = null;
load_posts();
init_create_post_fragment(PostsCreateFormFragment.PAGE_TYPE_USER, mUserID);
}
@Override
@ -154,13 +87,10 @@ public class UserPostsFragment extends Fragment
&& mLoadUserPostsTask.getStatus() != AsyncTask.Status.FINISHED;
}
/**
* Load user posts
*/
private void load_posts(){
@Override
public void onLoadPosts() {
cancel_load_task();
setProgressBarVisibility(true);
mLoadUserPostsTask = new LoadUserPostsTask(mUserID, getActivity());
mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@ -169,132 +99,14 @@ public class UserPostsFragment extends Fragment
if(getActivity() == null)
return;
apply_posts(posts);
onGotNewPosts(posts);
}
});
mLoadUserPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Apply the list of posts
*/
@UiThread
private void apply_posts(@Nullable PostsList posts){
setProgressBarVisibility(false);
if(isStateSaved())
return;
//Check for errors
if(posts == null){
Toast.makeText(getActivity(), R.string.err_get_user_posts, Toast.LENGTH_SHORT).show();
return;
}
//Check we didn't get user information
if(!posts.hasUsersInfo()){
Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show();
return;
}
//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);
transaction.commit();
}
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);
}
/**
* Create and create post fragment
*/
private void init_create_post_fragment(){
//Can not perform a transaction if the state has been saved
if(isStateSaved())
return;
//Create bundle
Bundle args = new Bundle();
args.putInt(PostsCreateFormFragment.PAGE_TYPE_ARG, PostsCreateFormFragment.PAGE_TYPE_USER);
args.putInt(PostsCreateFormFragment.PAGE_ID_ARG, mUserID);
//Create fragment
PostsCreateFormFragment fragment = new PostsCreateFormFragment();
fragment.setArguments(args);
fragment.setOnPostCreatedListener(this);
//Perform transaction
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.create_posts_form_target, fragment);
transaction.commit();
//Hide the post form by default
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
*
* @param visible New visibility
*/
private void setPostFormVisibility(boolean visible){
mCreatePostLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
mCreatePostButton.setActivated(visible);
}
private void setNoPostNoticeVisibility(boolean visible){
mNoPostNotice.setVisibility(visible ? View.VISIBLE : View.GONE);
}
@Override
public void onPostCreated(Post post) {
mPostsList = null;
load_posts();
init_create_post_fragment();
}
@Override
public void onLoadMorePosts() {
if(mPostsList == null)
return;
if(mPostsList.size() < 1)
return;
public void onLoadMorePosts(int last_post_id) {
if(is_loading_posts())
return;
@ -305,11 +117,31 @@ public class UserPostsFragment extends Fragment
mLoadUserPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
@Override
public void OnPostExecute(PostsList posts) {
apply_posts(posts);
onGotNewPosts(posts);
}
});
mLoadUserPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
mPostsList.get(mPostsList.size() -1).getId() -1);
last_post_id -1);
}
@Override
protected void onGotNewPosts(@Nullable PostsList list) {
//Check for errors
if(list == null){
Toast.makeText(getActivity(), R.string.err_get_user_posts, Toast.LENGTH_SHORT).show();
return;
}
//Check we didn't get user information
if(!list.hasUsersInfo()){
Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show();
return;
}
super.onGotNewPosts(list);
}
}

View File

@ -1,16 +0,0 @@
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,29 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Loading progress bar -->
<ProgressBar
android:id="@+id/loading_progress"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<!-- No posts notice -->
<TextView
android:id="@+id/no_post_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/notice_no_latest_posts"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<!-- Posts list -->
<FrameLayout
android:id="@+id/posts_list_target"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

View File

@ -1,5 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView 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"
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"
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" />
<!-- Posts form target -->
<FrameLayout
android:id="@+id/create_post_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/post_create_form" />
<!-- No post notice -->
<TextView
android:id="@+id/no_post_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="@string/notice_no_post_yet" />
<!-- Posts target -->
<org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView
android:id="@+id/posts_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="0dp"
android:layout_weight="1" />
</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>

View File

@ -1,60 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<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"
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" />
<!-- Posts form target -->
<FrameLayout
android:id="@+id/create_posts_form_target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/post_create_form" />
<!-- No post notice -->
<TextView
android:id="@+id/no_post_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="@string/notice_no_post_user_page" />
<!-- Posts target -->
<FrameLayout
android:id="@+id/posts_list_target"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_postslist" />
</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>

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/posts_list_target"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -265,4 +265,6 @@
<string name="err_update_conversation_message_content">Could not update conversation message content!</string>
<string name="success_update_conversation_message_content">The content of the conversation message has been successfully updated!</string>
<string name="err_get_older_posts">Could not get older posts!</string>
<string name="err_get_posts_list">Could not get the list of posts!</string>
<string name="notice_no_post_yet">There is no post to display here yet.</string>
</resources>