From 189dd61ec7c4a1573d80a63c8f66a7631b741a53 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 29 Aug 2018 11:18:09 +0200 Subject: [PATCH] Added friends tab --- .idea/misc.xml | 2 +- .../data/helpers/FriendsListHelper.java | 30 +++ .../client/data/helpers/GetUsersHelper.java | 2 + .../client/data/models/AdvancedUserInfo.java | 10 + .../ui/asynctasks/GetUserFriendsListTask.java | 33 ++++ .../ui/fragments/UserFriendListFragment.java | 174 ++++++++++++++++++ .../client/ui/fragments/UserPageFragment.java | 17 +- .../res/layout/fragment_user_friends_list.xml | 51 +++++ app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 3 + 10 files changed, 320 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetUserFriendsListTask.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserFriendListFragment.java create mode 100644 app/src/main/res/layout/fragment_user_friends_list.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 99202cc..c0f68ed 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java index 07e2652..4023592 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java @@ -343,4 +343,34 @@ public class FriendsListHelper { return false; } } + + /** + * Get the list of friends of a user + * + * @param userID the ID of the target user + * @return The list of the IDs of the friends of the user / null in case of failure + */ + @Nullable + public ArrayList getUserFriends(int userID){ + APIRequest request = new APIRequest(mContext, "friends/get_user_list"); + request.addInt("userID", userID); + + try { + APIResponse response = new APIRequestHelper().exec(request); + + if(response.getResponse_code() != 200) return null; + + JSONArray array = response.getJSONArray(); + ArrayList list = new ArrayList<>(); + + for (int i = 0; i < array.length(); i++) + list.add(array.getInt(i)); + + return list; + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java index 47e3a7a..5a4f051 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java @@ -403,6 +403,8 @@ public class GetUsersHelper { //Check if user can post text or this page or not advancedUserInfo.setCanPostText(userObject.getBoolean("can_post_texts")); + advancedUserInfo.setFriendListPublic(userObject.getBoolean("friend_list_public")); + } catch (JSONException e){ e.printStackTrace(); return null; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java index 9e2d204..976dd66 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java @@ -13,6 +13,7 @@ public class AdvancedUserInfo extends UserInfo { private int account_creation_time; private boolean accessForbidden = false; private boolean canPostText; + private boolean friendListPublic; /** * Get the account creation time @@ -67,4 +68,13 @@ public class AdvancedUserInfo extends UserInfo { public boolean isCanPostText() { return canPostText; } + + //Set and get friend is public status + public boolean isFriendListPublic() { + return friendListPublic; + } + + public void setFriendListPublic(boolean friendListPublic) { + this.friendListPublic = friendListPublic; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetUserFriendsListTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetUserFriendsListTask.java new file mode 100644 index 0000000..72ed399 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetUserFriendsListTask.java @@ -0,0 +1,33 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; +import android.util.ArrayMap; + +import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.data.helpers.FriendsListHelper; +import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; +import org.communiquons.android.comunic.client.data.models.UserInfo; + +import java.util.ArrayList; + +/** + * Get the list of friends of a specific user with their information + * + * @author Pierre HUBERT + */ +public class GetUserFriendsListTask extends SafeAsyncTask> { + + public GetUserFriendsListTask(Context context) { + super(context); + } + + @Override + protected ArrayMap doInBackground(Integer... integers) { + ArrayList list = new FriendsListHelper(getContext()).getUserFriends(integers[0]); + + if(list == null) + return null; + + return new GetUsersHelper(getContext()).getMultiple(list); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserFriendListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserFriendListFragment.java new file mode 100644 index 0000000..6b1e7d4 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserFriendListFragment.java @@ -0,0 +1,174 @@ +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.util.ArrayMap; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ListView; +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.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.data.models.UserInfo; +import org.communiquons.android.comunic.client.ui.adapters.UsersBasicAdapter; +import org.communiquons.android.comunic.client.ui.asynctasks.GetUserFriendsListTask; +import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; + +import java.util.ArrayList; +import java.util.Objects; + +/** + * Friend list fragment for a specific user (not the current user signed in) + * + * @author Pierre HUBERT + */ +public class UserFriendListFragment extends Fragment implements AdapterView.OnItemClickListener { + + private static final String TAG = UserFriendListFragment.class.getCanonicalName(); + + /** + * Argument to pass to the fragment to transfer user ID + */ + public static final String ARGUMENT_USER_ID = "user_id"; + + private int mUserID; + + /** + * Views + */ + private ProgressBar mProgress; + private TextView mNoFriendsNotice; + private ListView mList; + + /** + * Users list + */ + private ArrayList mUsersList; + + /** + * Get user task + */ + private GetUserFriendsListTask mLoadTask; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + assert getArguments() != null; + mUserID = getArguments().getInt(ARGUMENT_USER_ID); + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_user_friends_list, container, false); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + mProgress = view.findViewById(R.id.progressBar); + mNoFriendsNotice = view.findViewById(R.id.nofriendsNotice); + mList = view.findViewById(R.id.list); + + mList.setOnItemClickListener(this); + + setProgressBarVisibility(true); + setNoFriendsNoticeVisibility(false); + } + + @Override + public void onResume() { + super.onResume(); + + if (mUsersList != null) + showFriendsList(); + + load_list(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + if (mLoadTask != null) + mLoadTask.setOnPostExecuteListener(null); + } + + /** + * Load friends list + */ + private void load_list() { + + if (mLoadTask != null) + mLoadTask.setOnPostExecuteListener(null); + + mLoadTask = new GetUserFriendsListTask(getActivity()); + mLoadTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener>() { + @Override + public void OnPostExecute(ArrayMap users) { + if (getActivity() != null) + applyFriendsList(users); + } + }); + + mLoadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mUserID); + } + + /** + * Apply friend list + * + * @param list The list to apply + */ + private void applyFriendsList(@Nullable ArrayMap list) { + + setProgressBarVisibility(false); + + if (list == null) { + Toast.makeText(getContext(), R.string.err_get_user_friends, Toast.LENGTH_SHORT).show(); + return; + } + + this.mUsersList = new ArrayList<>(list.values()); + + showFriendsList(); + } + + /** + * Show friends list + */ + private void showFriendsList() { + + setNoFriendsNoticeVisibility(mUsersList.size() == 0); + + UsersBasicAdapter usersBasicAdapter = new UsersBasicAdapter( + Objects.requireNonNull(getActivity()), mUsersList); + + mList.setAdapter(usersBasicAdapter); + } + + + private void setProgressBarVisibility(boolean visible) { + mProgress.setVisibility(visible ? View.VISIBLE : View.GONE); + } + + private void setNoFriendsNoticeVisibility(boolean visible){ + mNoFriendsNotice.setVisibility(visible ? View.VISIBLE : View.GONE); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + if (mUsersList.size() > position) + ((onOpenUsersPageListener) Objects.requireNonNull(getActivity())).openUserPage( + mUsersList.get(position).getId()); + } +} 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 575da9a..1f1038c 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,12 +1,12 @@ package org.communiquons.android.comunic.client.ui.fragments; import android.app.AlertDialog; -import android.support.annotation.NonNull; -import android.support.v4.app.Fragment; import android.os.AsyncTask; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; +import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; @@ -17,6 +17,7 @@ import org.communiquons.android.comunic.client.R; 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.models.AdvancedUserInfo; +import org.communiquons.android.comunic.client.data.utils.AccountUtils; 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; @@ -196,6 +197,18 @@ public class UserPageFragment extends Fragment { adapter.addFragment(postsFragment, UiUtils.getString(getActivity(), R.string.tab_posts)); + //Friends fragment (if required) + if(mUserID != AccountUtils.getID(getActivity()) && userInfo.isFriendListPublic()){ + + Bundle friendsArgs = new Bundle(); + friendsArgs.putInt(UserFriendListFragment.ARGUMENT_USER_ID, mUserID); + + UserFriendListFragment friendsFragment = new UserFriendListFragment(); + friendsFragment.setArguments(friendsArgs); + adapter.addFragment(friendsFragment, UiUtils.getString(getActivity(), + R.string.tab_friends)); + } + mPager.setAdapter(adapter); mTabLayout.setupWithViewPager(mPager); diff --git a/app/src/main/res/layout/fragment_user_friends_list.xml b/app/src/main/res/layout/fragment_user_friends_list.xml new file mode 100644 index 0000000..c00b2df --- /dev/null +++ b/app/src/main/res/layout/fragment_user_friends_list.xml @@ -0,0 +1,51 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 14724a8..bf5b5f9 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -249,4 +249,5 @@ Licences Open Source Licences Open Source j + Amis \ 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 35e976b..524ba54 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -253,4 +253,7 @@ Delete Could not delete conversation message! d + Friends + Could not get the friends of the user! + This user has not any friend yet.