From 9058d059d8e5601150f5f32ad7bcd7c2d732aaa5 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Jul 2018 10:37:43 +0200 Subject: [PATCH] Use bundle to provide information to UserPostsFragment. --- .../client/ui/fragments/UserPageFragment.java | 6 ++- .../ui/fragments/UserPostsFragment.java | 44 +++++++++++-------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java index 2c30ea8..64de9f8 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java @@ -190,8 +190,12 @@ public class UserPageFragment extends Fragment { R.string.tab_user_advanced_info)); //Posts fragment + Bundle args = new Bundle(); + args.putInt(UserPostsFragment.ARGUMENT_USER_ID, userInfo.getId()); + args.putBoolean(UserPostsFragment.ARGUMENT_CAN_POST_TEXT, userInfo.isCanPostText()); + UserPostsFragment postsFragment = new UserPostsFragment(); - postsFragment.setAdvancedUserInfo(userInfo); + postsFragment.setArguments(args); adapter.addFragment(postsFragment, UiUtils.getString(getActivity(), R.string.tab_posts)); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java index 944f5bc..6b71744 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPostsFragment.java @@ -17,7 +17,6 @@ import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.arrays.PostsList; 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.AdvancedUserInfo; import org.communiquons.android.comunic.client.data.models.Post; import org.communiquons.android.comunic.client.data.models.UserInfo; @@ -30,9 +29,20 @@ public class UserPostsFragment extends Fragment implements PostsCreateFormFragment.OnPostCreated { /** - * Information about the user + * Bundle arguments */ - private AdvancedUserInfo mAdvancedUserInfo; + public static final String ARGUMENT_USER_ID = "user_id"; + public static final String ARGUMENT_CAN_POST_TEXT = "can_post_text"; + + /** + * The ID of the user + */ + private int mUserID; + + /** + * Specify whether the user is allowed to create posts on user page or not + */ + private boolean mCanPostsText; /** * The list of posts of the user @@ -74,16 +84,16 @@ public class UserPostsFragment extends Fragment */ private PostsListFragment mPostsListFragment; - /** - * Specify advanced user information - * - * Warning ! This method must absolutely be called before the fragment - * is attached to an activity ! - * - * @param advancedUserInfo Information about the user - */ - public void setAdvancedUserInfo(AdvancedUserInfo advancedUserInfo) { - this.mAdvancedUserInfo = advancedUserInfo; + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Get arguments + Bundle bundle = getArguments(); + assert bundle != null; + + mUserID = bundle.getInt(ARGUMENT_USER_ID); + mCanPostsText = bundle.getBoolean(ARGUMENT_CAN_POST_TEXT); } @Nullable @@ -96,8 +106,6 @@ public class UserPostsFragment extends Fragment public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - if(mAdvancedUserInfo == null) - return; //Get the views mCreatePostButton = view.findViewById(R.id.create_post_btn); @@ -116,7 +124,7 @@ public class UserPostsFragment extends Fragment mUserHelper = new GetUsersHelper(getActivity()); //Add create post fragment, if possible - if(mAdvancedUserInfo.isCanPostText()) + if(mCanPostsText) init_create_post_fragment(); else mCreatePostButton.setVisibility(View.GONE); @@ -136,7 +144,7 @@ public class UserPostsFragment extends Fragment public void run() { //Get the list of posts of the user - mPostsList.addAll(mPostsHelper.get_user(mAdvancedUserInfo.getId())); + mPostsList.addAll(mPostsHelper.get_user(mUserID)); if(mPostsList != null) mUsersInfo = mUserHelper.getMultiple(mPostsList.getUsersId()); @@ -187,7 +195,7 @@ public class UserPostsFragment extends Fragment //Create bundle Bundle args = new Bundle(); args.putInt(PostsCreateFormFragment.PAGE_TYPE_ARG, PostsCreateFormFragment.PAGE_TYPE_USER); - args.putInt(PostsCreateFormFragment.PAGE_ID_ARG, mAdvancedUserInfo.getId()); + args.putInt(PostsCreateFormFragment.PAGE_ID_ARG, mUserID); //Create fragment PostsCreateFormFragment fragment = new PostsCreateFormFragment();