diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/FragmentPagerBaseAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/FragmentPagerBaseAdapter.java new file mode 100644 index 0000000..be2fc68 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/FragmentPagerBaseAdapter.java @@ -0,0 +1,44 @@ +package org.communiquons.android.comunic.client.ui.adapters; + + + +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Based on the following SO answer : + * https://stackoverflow.com/a/41656303/3781411 + */ +public class FragmentPagerBaseAdapter extends FragmentPagerAdapter { + + private final List mFragmentList = new ArrayList<>(); + private final List mFragmentTitleList = new ArrayList<>(); + + public FragmentPagerBaseAdapter(FragmentManager manager) { + super(manager); + } + + @Override + public Fragment getItem(int position) { + return mFragmentList.get(position); + } + + @Override + public int getCount() { + return mFragmentList.size(); + } + + public void addFragment(Fragment fragment, String title) { + mFragmentList.add(fragment); + mFragmentTitleList.add(title); + } + + @Override + public CharSequence getPageTitle(int position) { + return mFragmentTitleList.get(position); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AdvancedUserInfoFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AdvancedUserInfoFragment.java new file mode 100644 index 0000000..7ca7e9e --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AdvancedUserInfoFragment.java @@ -0,0 +1,83 @@ +package org.communiquons.android.comunic.client.ui.fragments; + +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.models.AdvancedUserInfo; +import org.communiquons.android.comunic.client.data.utils.Utilities; +import org.communiquons.android.comunic.client.ui.utils.UiUtils; +import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; + +/** + * Advanced user information fragment + * + * @author Pierre HUBERT + */ +public class AdvancedUserInfoFragment extends Fragment { + + /** + * Advanced information about the user + */ + private AdvancedUserInfo mAdvancedUserInfo; + + /** + * User account image + */ + private WebUserAccountImage mUserAccountImage; + + /** + * The name of the user + */ + private TextView mUserName; + + /** + * Target for the time the user has been a member of the group + */ + private TextView mMemberSinceTarget; + + /** + * Set advanced information about the user + * + * Warning! This method must be called before the fragment is started, else a NullPointerException + * will be thrown + * + * @param advancedUserInfo Advanced information about the user + */ + public void setAdvancedUserInfo(AdvancedUserInfo advancedUserInfo) { + this.mAdvancedUserInfo = advancedUserInfo; + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_advanced_user_info, container, false); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + //Get the views + mUserAccountImage = view.findViewById(R.id.user_account_image); + mUserName = view.findViewById(R.id.user_name); + mMemberSinceTarget = view.findViewById(R.id.member_since_value); + } + + @Override + public void onStart() { + super.onStart(); + + //Apply user information + mUserAccountImage.setUser(mAdvancedUserInfo); + mUserName.setText(mAdvancedUserInfo.getDisplayFullName()); + mMemberSinceTarget.setText(new Utilities(getActivity()).timeToString( + Utilities.time() - mAdvancedUserInfo.getAccount_creation_time())); + } +} 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 584f2da..bf63d0c 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 @@ -1,29 +1,25 @@ package org.communiquons.android.comunic.client.ui.fragments; import android.app.AlertDialog; -import android.app.Fragment; -import android.app.FragmentTransaction; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; -import android.util.ArrayMap; +import android.support.design.widget.TabLayout; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; -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.helpers.DatabaseHelper; import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; -import org.communiquons.android.comunic.client.data.helpers.ImageLoadHelper; -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; import org.communiquons.android.comunic.client.ui.activities.MainActivity; +import org.communiquons.android.comunic.client.ui.adapters.FragmentPagerBaseAdapter; import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; import org.communiquons.android.comunic.client.ui.utils.UiUtils; @@ -36,7 +32,7 @@ import org.communiquons.android.comunic.client.ui.utils.UiUtils; * Created by pierre on 1/13/18. */ -public class UserPageFragment extends Fragment implements PostsCreateFormFragment.OnPostCreated { +public class UserPageFragment extends Fragment { /** * Debug tag @@ -59,65 +55,30 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen private AdvancedUserInfo userInfo; /** - * User posts + * Loading alert dialog */ - private PostsList mPostsList; - - /** - * User information - */ - private ArrayMap mUsersInfo; + private AlertDialog loadingDialog; /** * Get user helper */ private GetUsersHelper getUsersHelper; - /** - * Posts helper - */ - private PostsHelper mPostsHelper; - - /** - * Loading alert dialog - */ - private AlertDialog loadingDialog; - - /** - * User account name - */ - private TextView user_name; - - /** - * User account image - */ - private ImageView user_image; - - /** - * No post notice - */ - private TextView mNoPostNotice; - - /** - * Posts list fragment - */ - private PostsListFragment mPostsListFragment; - - /** - * Create a post on user page button - */ - private ImageView mCreatePostButton; - - /** - * Create a post on user page form - */ - private View mCreatePostForm; - /** * User page open listener */ private onOpenUsersPageListener mOpenUsersPageListener; + /** + * View pager of the fragment + */ + private ViewPager mPager; + + /** + * Tab layout + */ + private TabLayout mTabLayout; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -132,47 +93,23 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen //Create getUserHelper instance getUsersHelper = new GetUsersHelper(getActivity(), dbHelper); - //Create posts helper instance - mPostsHelper = new PostsHelper(getActivity()); - //Get the open user page listener mOpenUsersPageListener = (onOpenUsersPageListener) getActivity(); } @Nullable @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_user_page, container, false); } @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - //Get the user views - user_image = view.findViewById(R.id.user_account_image); - user_name = view.findViewById(R.id.user_account_name); - - //Get the no post notice - mNoPostNotice = view.findViewById(R.id.no_post_notice); - mNoPostNotice.setVisibility(View.GONE); - - //Get the view related to the create post form - mCreatePostButton = view.findViewById(R.id.create_post_button); - mCreatePostForm = view.findViewById(R.id.create_post_form); - - //Trigger the form - mCreatePostForm.setVisibility(View.GONE); - mCreatePostButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mCreatePostForm.setVisibility( - mCreatePostForm.getVisibility() == View.GONE ? View.VISIBLE : View.GONE); - } - }); - - //Create the fragment - init_create_post_fragment(); + //Get required views + mPager = view.findViewById(R.id.viewpager); + mTabLayout = view.findViewById(R.id.tab_layout); } @Override @@ -203,23 +140,17 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen else onGotUserInfo(userInfo); - //Render the list of post (if available) - render_list_posts(); } @Override public void onPause() { super.onPause(); - - //Remove loading dialog - if(loadingDialog != null) - loadingDialog.dismiss(); } /** * This function is called on the main thread once we got user informations * - * @param info Informations about the user + * @param info Information about the user */ public void onGotUserInfo(@Nullable AdvancedUserInfo info){ @@ -248,128 +179,24 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen getActivity().setTitle(userInfo.getDisplayFullName()); //Update activity menu dock - //if(AccountUtils.getID(getActivity()) == mUserID) - ((MainActivity) getActivity()).setSelectedNavigationItem( + ((MainActivity) getActivity()).setSelectedNavigationItem( R.id.main_bottom_navigation_me_view); - /*else - ((MainActivity) getActivity()).setSelectedNavigationItem( - R.id.main_bottom_navigation_users_view);*/ - //Update user name and account image - user_name.setText(userInfo.getDisplayFullName()); - ImageLoadHelper.remove(user_image); - ImageLoadHelper.load(getActivity(), userInfo.getAcountImageURL(), user_image); + //Initialize view pager + FragmentPagerBaseAdapter adapter = new FragmentPagerBaseAdapter(getChildFragmentManager()); - //Check if the user can post text on this page - mCreatePostButton.setVisibility(userInfo.isCanPostText() ? View.VISIBLE : View.GONE); + //User advanced information fragment + AdvancedUserInfoFragment infoFragment = new AdvancedUserInfoFragment(); + infoFragment.setAdvancedUserInfo(userInfo); + adapter.addFragment(infoFragment, UiUtils.getString(getActivity(), + R.string.tab_user_advanced_info)); + + //Posts fragment + adapter.addFragment(new Fragment(), UiUtils.getString(getActivity(), + R.string.tab_posts)); - //Load the list of posts of the user - load_posts(); - } - - /** - * Load the posts of the user - */ - private void load_posts(){ - - new AsyncTask(){ - - @Override - protected PostsList doInBackground(Void... params) { - PostsList list = mPostsHelper.get_user(mUserID); - - //Get the information about the users who created the posts - if(list != null) - mUsersInfo = getUsersHelper.getMultiple(list.getUsersId()); - - return list; - } - - @Override - protected void onPostExecute(PostsList posts) { - if(getActivity() != null) - display_posts(posts); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - - /** - * Display the posts of the user - * - * @param list the list of posts / null in case of failure - */ - private void display_posts(@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(mUsersInfo == null){ - Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show(); - return; - } - - //Save the list of posts - mPostsList = list; - - //Render the list of posts - render_list_posts(); - } - - /** - * Render the list of posts - */ - private void render_list_posts(){ - - //Check we have got required information - if(mPostsList == null || mUsersInfo == null) - return; - - //Create the fragment - mPostsListFragment = new PostsListFragment(); - mPostsListFragment.setPostsList(mPostsList); - mPostsListFragment.setUsersInfos(mUsersInfo); - - //Set the fragment - FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); - transaction.replace(R.id.fragment_user_page, mPostsListFragment); - transaction.commit(); - - //Check if there is not any post on the page - mNoPostNotice.setVisibility(mPostsList.size() == 0 ? View.VISIBLE : View.GONE); - } - - /** - * Create and create post fragment - */ - private void init_create_post_fragment(){ - - //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_post_form, fragment); - transaction.commit(); - - } - - @Override - public void onPostCreated(Post post) { - //Reload the list of post - mPostsList = null; - init_create_post_fragment(); - load_posts(); + mPager.setAdapter(adapter); + mTabLayout.setupWithViewPager(mPager); } } diff --git a/app/src/main/res/layout/fragment_advanced_user_info.xml b/app/src/main/res/layout/fragment_advanced_user_info.xml new file mode 100644 index 0000000..0298fc1 --- /dev/null +++ b/app/src/main/res/layout/fragment_advanced_user_info.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_user_page.xml b/app/src/main/res/layout/fragment_user_page.xml index bddeaf2..f8fc0c4 100644 --- a/app/src/main/res/layout/fragment_user_page.xml +++ b/app/src/main/res/layout/fragment_user_page.xml @@ -1,77 +1,38 @@ - + android:layout_height="match_parent"> - - + android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> - - - + app:tabMode="scrollable"/> + - + - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6ca24b2..44f41c8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -191,4 +191,8 @@ You do not have any latest posts to display here. Search user View PDF + Settings + User Info + Posts + Member for diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 13fc489..e0471a8 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,7 @@ -