diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java index ca5f978..0102d7b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/Friend.java @@ -1,5 +1,7 @@ package org.communiquons.android.comunic.client.data.models; +import android.support.annotation.Nullable; + import org.communiquons.android.comunic.client.data.utils.TimeUtils; /** @@ -36,6 +38,11 @@ public class Friend { */ private int last_activity; + /** + * Related user information + */ + private UserInfo userInfo; + /** * Public constructor */ @@ -121,4 +128,13 @@ public class Friend { public boolean signed_in(){ return (TimeUtils.time()-USER_INACTIVE_AFTER) < last_activity; } + + @Nullable + public UserInfo getUserInfo() { + return userInfo; + } + + public void setUserInfo(UserInfo userInfo) { + this.userInfo = userInfo; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/FriendUser.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/FriendUser.java deleted file mode 100644 index ce9a24c..0000000 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/FriendUser.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.communiquons.android.comunic.client.data.models; - -/** - * 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; - } - - - /*** - * Get the ID of the user - * - * @return The id of the user - */ - public int get_user_id(){ - return userInfo.getId(); - } -} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FriendsUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FriendsUtils.java index 6a825b0..e13dbe4 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FriendsUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FriendsUtils.java @@ -2,9 +2,8 @@ package org.communiquons.android.comunic.client.data.utils; import android.util.ArrayMap; -import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.data.models.Friend; -import org.communiquons.android.comunic.client.data.models.FriendUser; +import org.communiquons.android.comunic.client.data.models.UserInfo; import java.util.ArrayList; @@ -33,31 +32,18 @@ public class FriendsUtils { } /** - * Merge a friends user list with a user info list, return the result by a FriendUser list + * Merge a user information list with a friend list * * @param friendsList The list of friend of the user - * @param userInfos Informations about the user - * @return The result of the operation + * @param userInfo Information about the friends */ - public static ArrayList merge_friends_user_infos_list(ArrayList friendsList, - ArrayMap userInfos){ - - ArrayList list = new ArrayList<>(); + public static void MergeFriendsListWithUserInfo(ArrayList friendsList, + ArrayMap userInfo){ //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); - } - + friend.setUserInfo(userInfo.get(friend.getId())); } - return list; - } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/FriendsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/FriendsAdapter.java index a112ff1..5926935 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/FriendsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/FriendsAdapter.java @@ -3,6 +3,7 @@ package org.communiquons.android.comunic.client.ui.adapters; import android.content.Context; import android.support.annotation.CallSuper; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -13,7 +14,6 @@ import android.widget.TextView; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.models.Friend; -import org.communiquons.android.comunic.client.data.models.FriendUser; import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.ui.listeners.OnFriendListActionListener; import org.communiquons.android.comunic.client.ui.utils.UiUtils; @@ -39,7 +39,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter { /** * The list of friends, with their information */ - private ArrayList mList; + private ArrayList mList; /** * Actions listener @@ -51,9 +51,9 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter { * * @param context The context of execution of the application * @param friendsList The list of friends to display (with user information) - * @param listener Actions on friendlist listener + * @param listener Actions on friends listener */ - public FriendsAdapter(Context context, ArrayList friendsList, + public FriendsAdapter(Context context, ArrayList friendsList, OnFriendListActionListener listener){ super(context); @@ -61,6 +61,15 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter { mListener = listener; } + private Friend getFriend(int pos){ + return mList.get(pos); + } + + @Nullable + private UserInfo getUserInfo(int pos){ + return mList.get(pos).getUserInfo(); + } + @Override public int getItemCount() { return mList.size(); @@ -68,7 +77,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter { @Override public int getItemViewType(int position) { - return mList.get(position).getFriend().isAccepted() ? VIEW_TYPE_ACCEPTED_FRIEND : + return mList.get(position).isAccepted() ? VIEW_TYPE_ACCEPTED_FRIEND : VIEW_TYPE_PENDING_FRIEND; } @@ -115,24 +124,19 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter { mUserStatus = itemView.findViewById(R.id.user_status); } - Friend getFriend(int pos){ - return mList.get(pos).getFriend(); - } - - UserInfo getUserInfo(int pos){ - return mList.get(pos).getUserInfo(); - } - int getCurrentUserID(){ - return getUserInfo(getLayoutPosition()).getId(); + return getFriend(getLayoutPosition()).getId(); } @CallSuper void bind(int pos){ //Update user information - mUserAccountImage.setUser(getUserInfo(pos)); - mUserName.setText(getUserInfo(pos).getDisplayFullName()); + UserInfo user = getUserInfo(pos); + mUserAccountImage.removeUser(); + if(user != null) + mUserAccountImage.setUser(user); + mUserName.setText(user == null ? "" : user.getDisplayFullName()); //Update user status boolean signed_in = getFriend(pos).signed_in(); @@ -143,12 +147,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter { //Open user page on click - itemView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mListener.onOpenUserPage(getCurrentUserID()); - } - }); + itemView.setOnClickListener(v -> mListener.onOpenUserPage(getCurrentUserID())); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java index 7fbd0b3..a2b9495 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java @@ -2,7 +2,6 @@ package org.communiquons.android.comunic.client.ui.fragments; import android.app.AlertDialog; import android.content.Context; -import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; @@ -21,22 +20,24 @@ import android.widget.TextView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; -import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; 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.Friend; -import org.communiquons.android.comunic.client.data.models.FriendUser; import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.data.utils.FriendsUtils; import org.communiquons.android.comunic.client.ui.activities.MainActivity; import org.communiquons.android.comunic.client.ui.adapters.FriendsAdapter; +import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; import org.communiquons.android.comunic.client.ui.listeners.OnFriendListActionListener; import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; import org.communiquons.android.comunic.client.ui.listeners.openConversationListener; import org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView; import java.util.ArrayList; +import java.util.Objects; + +import static org.communiquons.android.comunic.client.data.utils.FriendsUtils.MergeFriendsListWithUserInfo; /** * Friends list fragment @@ -51,18 +52,13 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL /** * Debug tag */ - private static final String TAG = "FriendsListFragment"; + private static final String TAG = FriendsListFragment.class.getSimpleName(); /** * Application context */ private Context mContext; - /** - * Database helper - */ - private DatabaseHelper mDbHelper; - /** * Get user helper */ @@ -71,7 +67,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL /** * The current list of friends */ - private ArrayList mList; + private ArrayList mList; /** * Friend list operations object @@ -118,13 +114,10 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL super.onCreate(savedInstanceState); //Save application context - mContext = getActivity().getApplicationContext(); - - //Create database helper - mDbHelper = DatabaseHelper.getInstance(mContext); + mContext = Objects.requireNonNull(getActivity()).getApplicationContext(); //Create friend list helper object - mFriendsHelper = new FriendsListHelper(mDbHelper, mContext); + mFriendsHelper = new FriendsListHelper(DatabaseHelper.getInstance(getActivity()), mContext); //Create get user helper mUsersHelper = new GetUsersHelper(mContext); @@ -173,7 +166,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL super.onResume(); //Update the title of the application - getActivity().setTitle(R.string.fragment_friendslist_title); + Objects.requireNonNull(getActivity()).setTitle(R.string.fragment_friendslist_title); MainActivity.SetNavbarSelectedOption(getActivity(), R.id.action_friendslist); //Refresh the friends list @@ -188,10 +181,10 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL //Display loading bar display_progress_bar(true); - new AsyncTask>() { + new AsyncTask>() { @Override - protected ArrayList doInBackground(Void... params) { + protected ArrayList doInBackground(Void... params) { //Fetch the list of friends ArrayList friendsList = mFriendsHelper.get(); @@ -209,12 +202,13 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL return null; //Merge friend and user and return result - return FriendsUtils.merge_friends_user_infos_list(friendsList, userInfo); + MergeFriendsListWithUserInfo(friendsList, userInfo); + return friendsList; } @Override - protected void onPostExecute(ArrayList friendUsers) { + protected void onPostExecute(ArrayList friendUsers) { //Check the activity still exists if (getActivity() == null) @@ -230,7 +224,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL * * @param friendsList The friends list to apply */ - private void apply_friends_list(@Nullable ArrayList friendsList) { + private void apply_friends_list(@Nullable ArrayList friendsList) { //Remove progress bar display_progress_bar(false); @@ -271,29 +265,26 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL builder.setTitle(R.string.popup_deletefriend_title) .setMessage(R.string.popup_deletefriend_message); - builder.setPositiveButton(R.string.popup_deletefriend_button_confirm, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { + builder.setPositiveButton(R.string.popup_deletefriend_button_confirm, (dialog, which) -> { - //Get the friend to delete - final Friend toDelete = mList.get(pos).getFriend(); + //Get the friend to delete + final Friend toDelete = mList.get(pos); - //Apply new list version - mList.remove(pos); - mAdapter.notifyDataSetChanged(); + //Apply new list version + mList.remove(pos); + mAdapter.notifyDataSetChanged(); - //Remove the friend list on a parallel thread - new AsyncTask() { - @Override - protected Void doInBackground(Integer[] params) { + //Remove the friend list on a parallel thread + new AsyncTask() { + @Override + protected Void doInBackground(Integer[] params) { - //Delete the friend from the list - mFriendsHelper.remove(toDelete); + //Delete the friend from the list + mFriendsHelper.remove(toDelete); - return null; - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pos); - } + return null; + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pos); }); builder.setNegativeButton(R.string.popup_deletefriend_button_cancel, null); builder.show(); @@ -311,7 +302,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL public void onRespondFrienshipRequest(int pos, final boolean response) { //Get the Friend object - Friend targetFriend = mList.get(pos).getFriend(); + Friend targetFriend = mList.get(pos); if (response) //Mark the friend as accepted @@ -347,7 +338,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL //Update following checkbox - menu.getMenu().findItem(R.id.action_follow).setChecked(mList.get(pos).getFriend() + menu.getMenu().findItem(R.id.action_follow).setChecked(mList.get(pos) .isFollowing()); menu.show(); @@ -360,7 +351,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL //To open a private conversation with the friend case R.id.action_private_conversation: - mConvOpener.openPrivateConversation(mList.get(mPosInContextMenu).getFriend().getId()); + mConvOpener.openPrivateConversation(mList.get(mPosInContextMenu).getId()); return true; //To delete the friend @@ -370,7 +361,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL case R.id.action_follow: onSetFollowing(mPosInContextMenu, - !mList.get(mPosInContextMenu).getFriend().isFollowing()); + !mList.get(mPosInContextMenu).isFollowing()); return true; } @@ -381,7 +372,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL @Override public void onSetFollowing(int pos, boolean following) { - Friend friend = mList.get(pos).getFriend(); + Friend friend = mList.get(pos); friend.setFollowing(following); mAdapter.notifyDataSetChanged();