From 3a54768224bdf79b859903f7aa52a0c960c9eb22 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 15 Nov 2017 18:29:58 +0100 Subject: [PATCH] Friends List is displayed --- .../client/data/friendsList/FriendUser.java | 69 ++++++++++++ .../data/friendsList/FriendsAdapter.java | 64 +++++++++++ .../client/data/friendsList/FriendsUtils.java | 61 +++++++++++ .../client/fragments/FriendsListFragment.java | 101 ++++++++++++++++-- .../fragment_friends_list_friend_item.xml | 43 ++++++++ .../main/res/layout/fragment_friendslist.xml | 17 ++- app/src/main/res/values/dimens.xml | 7 ++ app/src/main/res/values/strings.xml | 2 + 8 files changed, 351 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendUser.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsUtils.java create mode 100644 app/src/main/res/layout/fragment_friends_list_friend_item.xml create mode 100644 app/src/main/res/values/dimens.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendUser.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendUser.java new file mode 100644 index 0000000..2c6e9f7 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendUser.java @@ -0,0 +1,69 @@ +package org.communiquons.android.comunic.client.data.friendsList; + +import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; + +/** + * This class contains information about a friend but also about user himself + * + * @author Pierre Hubert + * Created by pierre on 11/15/17. + */ + +public class FriendUser { + + /** + * Friendship informations + */ + private Friend friend; + + /** + * Informations about the user + */ + private UserInfo userInfo; + + /** + * Create a FriendUser object + * + * @param friend Informations about the friend + */ + public FriendUser(Friend friend){ + this.friend = friend; + } + + /** + * Set a new user informations + * + * @param userInfo Informations about the user + */ + public void setUserInfo(UserInfo userInfo) { + this.userInfo = userInfo; + } + + /** + * Get informations about the user + * + * @return User informations + */ + public UserInfo getUserInfo() { + return userInfo; + } + + + /** + * Get informations about the friendship + * + * @return Informations about the friendship + */ + public Friend getFriend() { + return friend; + } + + /** + * Set friendship informations + * + * @param friend New friendship informations + */ + public void setFriend(Friend friend) { + this.friend = friend; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java new file mode 100644 index 0000000..c7664a2 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java @@ -0,0 +1,64 @@ +package org.communiquons.android.comunic.client.data.friendsList; + +import android.app.Activity; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.ImageLoadTask; + +import java.util.ArrayList; + +/** + * Adapter that render the list of friend on the friends list fragment + * + * @author Pierre Hubert + * Created by pierre on 11/15/17. + */ + +public class FriendsAdapter extends ArrayAdapter { + + + /** + * Class constructor + * + * @param context The context of execution of the application + * @param friendsList The list of friends to display (with user information) + */ + public FriendsAdapter(Activity context, ArrayList friendsList){ + super(context, 0, friendsList); + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + View listItemView = convertView; + + //Check if the view has to be created + if(listItemView == null){ + listItemView = LayoutInflater.from(getContext()) + .inflate(R.layout.fragment_friends_list_friend_item, parent, false); + } + + //Get friend information + FriendUser friendUser = getItem(position); + + //Update user account image + ImageView user_image = listItemView.findViewById(R.id.fragment_friendslist_item_accountimage); + new ImageLoadTask(getContext(), friendUser.getUserInfo().getAcountImageURL(), user_image) + .execute(); + + //Update user name + TextView user_name = listItemView.findViewById(R.id.fragment_friendslist_item_fullname); + user_name.setText(friendUser.getUserInfo().getFullName()); + + return listItemView; + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsUtils.java new file mode 100644 index 0000000..09b74ff --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsUtils.java @@ -0,0 +1,61 @@ +package org.communiquons.android.comunic.client.data.friendsList; + +import android.util.ArrayMap; + +import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; + +import java.util.ArrayList; + +/** + * Friends utilities + * + * @author Pierre Hubert + * Created by pierre on 11/15/17. + */ + +public class FriendsUtils { + + /** + * Given a friend list, it will return the IDs of the friends + * + * @param friendsList the friends list to process + * @return The list of the id of the friend + */ + public static ArrayList getFriendsIDs(ArrayList friendsList){ + ArrayList IDs = new ArrayList<>(); + + for(Friend friend : friendsList) + IDs.add(friend.getId()); + + return IDs; + } + + /** + * Merge a friends user list with a user info list, return the result by a FriendUser list + * + * @param friendsList The list of friend of the user + * @param userInfos Informations about the user + * @return The result of the operation + */ + public static ArrayList merge_friends_user_infos_list(ArrayList friendsList, + ArrayMap userInfos){ + + ArrayList list = new ArrayList<>(); + + //Process the list + for(Friend friend : friendsList){ + + UserInfo userInfo; + + if((userInfo = userInfos.get(friend.getId())) != null){ + FriendUser item = new FriendUser(friend); + item.setUserInfo(userInfo); + list.add(item); + } + + } + + return list; + + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java index 1e199e0..beaf310 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java @@ -1,13 +1,14 @@ package org.communiquons.android.comunic.client.fragments; import android.app.Fragment; +import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.ArrayMap; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ListView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; @@ -15,6 +16,9 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.friendsList.Friend; +import org.communiquons.android.comunic.client.data.friendsList.FriendUser; +import org.communiquons.android.comunic.client.data.friendsList.FriendsAdapter; +import org.communiquons.android.comunic.client.data.friendsList.FriendsUtils; import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask; import java.util.ArrayList; @@ -28,16 +32,38 @@ import java.util.ArrayList; public class FriendsListFragment extends Fragment { + /** + * The root view of the fragment + */ + View rootView; + + /** + * Application context + */ + Context mContext; + + /** + * Database helper + */ + DatabaseHelper mDbHelper; + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + //Save application context + mContext = getActivity().getApplicationContext(); + + //Create database helper + mDbHelper = new DatabaseHelper(mContext); + //Retain the fragment //setRetainInstance(true); //Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_friendslist, container, false); + rootView = inflater.inflate(R.layout.fragment_friendslist, container, false); + return rootView; } @@ -53,21 +79,78 @@ public class FriendsListFragment extends Fragment { * Refresh the friend list */ void refresh_friend_list(){ + + //Display loading bar + display_progress_bar(true); + new GetFriendsListTask(getActivity().getApplicationContext()){ @Override protected void onPostExecute(ArrayList friendsList) { - //Display informations in the console - for(Friend friend : friendsList){ - Log.v("FriendsListFragment", "Friend: " + friend.getId() + " " + - (friend.isAccepted() ? 1 : 0 ) + " " + (friend.isFollowing() ? 1 : 0) + - " " + friend.getLast_activity() + " " - ); - } + //Remote progress bar + display_progress_bar(false); + //Check for errors + if(friendsList == null){ + Toast.makeText(mContext, R.string.fragment_friendslist_err_refresh, + Toast.LENGTH_LONG).show(); + return; + } + else + //Update the friends list + update_friends_list(friendsList); } }.execute(); } + + /** + * Set the the list of friend attached to the list adapter + * + * @param friendsList The friends list to apply + */ + private void update_friends_list(final ArrayList friendsList){ + + //Get user informations + new GetUsersInfos(mContext, mDbHelper). + getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() { + @Override + public void callback(ArrayMap info) { + //Check for errors + if(info == null){ + Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos, + Toast.LENGTH_SHORT).show(); + return; + } + + //Merge the user informations list and friends List into FriendInfo list + ArrayList friendsUserList = FriendsUtils.merge_friends_user_infos_list( + friendsList, + info + ); + + //Set the adapter + FriendsAdapter friendsAdapter = new FriendsAdapter(getActivity(), friendsUserList); + ListView listView = rootView.findViewById(R.id.fragment_friendslist_listview); + listView.setAdapter(friendsAdapter); + } + }); + + } + + /** + * Hide (or display) progress bar + * + * @param display Specify whether the loading bar has to be shown or not + */ + private void display_progress_bar(boolean display){ + + //Get the view + rootView.findViewById(R.id.fragment_friendslist_progressbar).setVisibility( + display ? View.VISIBLE : View.GONE + ); + + + } } diff --git a/app/src/main/res/layout/fragment_friends_list_friend_item.xml b/app/src/main/res/layout/fragment_friends_list_friend_item.xml new file mode 100644 index 0000000..f31c1c4 --- /dev/null +++ b/app/src/main/res/layout/fragment_friends_list_friend_item.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_friendslist.xml b/app/src/main/res/layout/fragment_friendslist.xml index 0f29d86..957b4fb 100644 --- a/app/src/main/res/layout/fragment_friendslist.xml +++ b/app/src/main/res/layout/fragment_friendslist.xml @@ -1,11 +1,20 @@ - - + + android:layout_centerHorizontal="true" /> - \ No newline at end of file + + + \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..b216897 --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,7 @@ + + + 64dp + 64dp + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3d2c924..bf9ce7f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,4 +16,6 @@ Cancel Friends Me + Couldn\'t refresh friends list ! + Couldn\'t get information about your friends!