Deleted the FriendUser class

This commit is contained in:
Pierre HUBERT 2019-03-02 11:11:43 +01:00
parent dadb64dbff
commit 28e54a271b
5 changed files with 78 additions and 163 deletions

View File

@ -1,5 +1,7 @@
package org.communiquons.android.comunic.client.data.models; package org.communiquons.android.comunic.client.data.models;
import android.support.annotation.Nullable;
import org.communiquons.android.comunic.client.data.utils.TimeUtils; import org.communiquons.android.comunic.client.data.utils.TimeUtils;
/** /**
@ -36,6 +38,11 @@ public class Friend {
*/ */
private int last_activity; private int last_activity;
/**
* Related user information
*/
private UserInfo userInfo;
/** /**
* Public constructor * Public constructor
*/ */
@ -121,4 +128,13 @@ public class Friend {
public boolean signed_in(){ public boolean signed_in(){
return (TimeUtils.time()-USER_INACTIVE_AFTER) < last_activity; return (TimeUtils.time()-USER_INACTIVE_AFTER) < last_activity;
} }
@Nullable
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
} }

View File

@ -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();
}
}

View File

@ -2,9 +2,8 @@ package org.communiquons.android.comunic.client.data.utils;
import android.util.ArrayMap; 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.Friend;
import org.communiquons.android.comunic.client.data.models.FriendUser; import org.communiquons.android.comunic.client.data.models.UserInfo;
import java.util.ArrayList; 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 friendsList The list of friend of the user
* @param userInfos Informations about the user * @param userInfo Information about the friends
* @return The result of the operation
*/ */
public static ArrayList<FriendUser> merge_friends_user_infos_list(ArrayList<Friend> friendsList, public static void MergeFriendsListWithUserInfo(ArrayList<Friend> friendsList,
ArrayMap<Integer, UserInfo> userInfos){ ArrayMap<Integer, UserInfo> userInfo){
ArrayList<FriendUser> list = new ArrayList<>();
//Process the list //Process the list
for(Friend friend : friendsList){ for(Friend friend : friendsList){
friend.setUserInfo(userInfo.get(friend.getId()));
UserInfo userInfo;
if((userInfo = userInfos.get(friend.getId())) != null){
FriendUser item = new FriendUser(friend);
item.setUserInfo(userInfo);
list.add(item);
}
} }
return list;
} }
} }

View File

@ -3,6 +3,7 @@ package org.communiquons.android.comunic.client.ui.adapters;
import android.content.Context; import android.content.Context;
import android.support.annotation.CallSuper; import android.support.annotation.CallSuper;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; 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.R;
import org.communiquons.android.comunic.client.data.models.Friend; 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.models.UserInfo;
import org.communiquons.android.comunic.client.ui.listeners.OnFriendListActionListener; import org.communiquons.android.comunic.client.ui.listeners.OnFriendListActionListener;
import org.communiquons.android.comunic.client.ui.utils.UiUtils; 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 * The list of friends, with their information
*/ */
private ArrayList<FriendUser> mList; private ArrayList<Friend> mList;
/** /**
* Actions listener * Actions listener
@ -51,9 +51,9 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter {
* *
* @param context The context of execution of the application * @param context The context of execution of the application
* @param friendsList The list of friends to display (with user information) * @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<FriendUser> friendsList, public FriendsAdapter(Context context, ArrayList<Friend> friendsList,
OnFriendListActionListener listener){ OnFriendListActionListener listener){
super(context); super(context);
@ -61,6 +61,15 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter {
mListener = listener; mListener = listener;
} }
private Friend getFriend(int pos){
return mList.get(pos);
}
@Nullable
private UserInfo getUserInfo(int pos){
return mList.get(pos).getUserInfo();
}
@Override @Override
public int getItemCount() { public int getItemCount() {
return mList.size(); return mList.size();
@ -68,7 +77,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter {
@Override @Override
public int getItemViewType(int position) { 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; VIEW_TYPE_PENDING_FRIEND;
} }
@ -115,24 +124,19 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter {
mUserStatus = itemView.findViewById(R.id.user_status); 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(){ int getCurrentUserID(){
return getUserInfo(getLayoutPosition()).getId(); return getFriend(getLayoutPosition()).getId();
} }
@CallSuper @CallSuper
void bind(int pos){ void bind(int pos){
//Update user information //Update user information
mUserAccountImage.setUser(getUserInfo(pos)); UserInfo user = getUserInfo(pos);
mUserName.setText(getUserInfo(pos).getDisplayFullName()); mUserAccountImage.removeUser();
if(user != null)
mUserAccountImage.setUser(user);
mUserName.setText(user == null ? "" : user.getDisplayFullName());
//Update user status //Update user status
boolean signed_in = getFriend(pos).signed_in(); boolean signed_in = getFriend(pos).signed_in();
@ -143,12 +147,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter {
//Open user page on click //Open user page on click
itemView.setOnClickListener(new View.OnClickListener() { itemView.setOnClickListener(v -> mListener.onOpenUserPage(getCurrentUserID()));
@Override
public void onClick(View v) {
mListener.onOpenUserPage(getCurrentUserID());
}
});
} }
} }

View File

@ -2,7 +2,6 @@ package org.communiquons.android.comunic.client.ui.fragments;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -21,22 +20,24 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.communiquons.android.comunic.client.R; 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.DatabaseHelper;
import org.communiquons.android.comunic.client.data.helpers.FriendsListHelper; 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.helpers.GetUsersHelper;
import org.communiquons.android.comunic.client.data.models.Friend; 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.models.UserInfo;
import org.communiquons.android.comunic.client.data.utils.FriendsUtils; 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.activities.MainActivity;
import org.communiquons.android.comunic.client.ui.adapters.FriendsAdapter; 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.OnFriendListActionListener;
import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; 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.listeners.openConversationListener;
import org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView; import org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects;
import static org.communiquons.android.comunic.client.data.utils.FriendsUtils.MergeFriendsListWithUserInfo;
/** /**
* Friends list fragment * Friends list fragment
@ -51,18 +52,13 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
/** /**
* Debug tag * Debug tag
*/ */
private static final String TAG = "FriendsListFragment"; private static final String TAG = FriendsListFragment.class.getSimpleName();
/** /**
* Application context * Application context
*/ */
private Context mContext; private Context mContext;
/**
* Database helper
*/
private DatabaseHelper mDbHelper;
/** /**
* Get user helper * Get user helper
*/ */
@ -71,7 +67,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
/** /**
* The current list of friends * The current list of friends
*/ */
private ArrayList<FriendUser> mList; private ArrayList<Friend> mList;
/** /**
* Friend list operations object * Friend list operations object
@ -118,13 +114,10 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//Save application context //Save application context
mContext = getActivity().getApplicationContext(); mContext = Objects.requireNonNull(getActivity()).getApplicationContext();
//Create database helper
mDbHelper = DatabaseHelper.getInstance(mContext);
//Create friend list helper object //Create friend list helper object
mFriendsHelper = new FriendsListHelper(mDbHelper, mContext); mFriendsHelper = new FriendsListHelper(DatabaseHelper.getInstance(getActivity()), mContext);
//Create get user helper //Create get user helper
mUsersHelper = new GetUsersHelper(mContext); mUsersHelper = new GetUsersHelper(mContext);
@ -173,7 +166,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
super.onResume(); super.onResume();
//Update the title of the application //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); MainActivity.SetNavbarSelectedOption(getActivity(), R.id.action_friendslist);
//Refresh the friends list //Refresh the friends list
@ -188,10 +181,10 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
//Display loading bar //Display loading bar
display_progress_bar(true); display_progress_bar(true);
new AsyncTask<Void, Void, ArrayList<FriendUser>>() { new AsyncTask<Void, Void, ArrayList<Friend>>() {
@Override @Override
protected ArrayList<FriendUser> doInBackground(Void... params) { protected ArrayList<Friend> doInBackground(Void... params) {
//Fetch the list of friends //Fetch the list of friends
ArrayList<Friend> friendsList = mFriendsHelper.get(); ArrayList<Friend> friendsList = mFriendsHelper.get();
@ -209,12 +202,13 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
return null; return null;
//Merge friend and user and return result //Merge friend and user and return result
return FriendsUtils.merge_friends_user_infos_list(friendsList, userInfo); MergeFriendsListWithUserInfo(friendsList, userInfo);
return friendsList;
} }
@Override @Override
protected void onPostExecute(ArrayList<FriendUser> friendUsers) { protected void onPostExecute(ArrayList<Friend> friendUsers) {
//Check the activity still exists //Check the activity still exists
if (getActivity() == null) if (getActivity() == null)
@ -230,7 +224,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
* *
* @param friendsList The friends list to apply * @param friendsList The friends list to apply
*/ */
private void apply_friends_list(@Nullable ArrayList<FriendUser> friendsList) { private void apply_friends_list(@Nullable ArrayList<Friend> friendsList) {
//Remove progress bar //Remove progress bar
display_progress_bar(false); display_progress_bar(false);
@ -271,29 +265,26 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
builder.setTitle(R.string.popup_deletefriend_title) builder.setTitle(R.string.popup_deletefriend_title)
.setMessage(R.string.popup_deletefriend_message); .setMessage(R.string.popup_deletefriend_message);
builder.setPositiveButton(R.string.popup_deletefriend_button_confirm, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.popup_deletefriend_button_confirm, (dialog, which) -> {
@Override
public void onClick(DialogInterface dialog, int which) {
//Get the friend to delete //Get the friend to delete
final Friend toDelete = mList.get(pos).getFriend(); final Friend toDelete = mList.get(pos);
//Apply new list version //Apply new list version
mList.remove(pos); mList.remove(pos);
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
//Remove the friend list on a parallel thread //Remove the friend list on a parallel thread
new AsyncTask<Integer, Void, Void>() { new AsyncTask<Integer, Void, Void>() {
@Override @Override
protected Void doInBackground(Integer[] params) { protected Void doInBackground(Integer[] params) {
//Delete the friend from the list //Delete the friend from the list
mFriendsHelper.remove(toDelete); mFriendsHelper.remove(toDelete);
return null; return null;
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pos); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pos);
}
}); });
builder.setNegativeButton(R.string.popup_deletefriend_button_cancel, null); builder.setNegativeButton(R.string.popup_deletefriend_button_cancel, null);
builder.show(); builder.show();
@ -311,7 +302,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
public void onRespondFrienshipRequest(int pos, final boolean response) { public void onRespondFrienshipRequest(int pos, final boolean response) {
//Get the Friend object //Get the Friend object
Friend targetFriend = mList.get(pos).getFriend(); Friend targetFriend = mList.get(pos);
if (response) if (response)
//Mark the friend as accepted //Mark the friend as accepted
@ -347,7 +338,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
//Update following checkbox //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()); .isFollowing());
menu.show(); menu.show();
@ -360,7 +351,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
//To open a private conversation with the friend //To open a private conversation with the friend
case R.id.action_private_conversation: case R.id.action_private_conversation:
mConvOpener.openPrivateConversation(mList.get(mPosInContextMenu).getFriend().getId()); mConvOpener.openPrivateConversation(mList.get(mPosInContextMenu).getId());
return true; return true;
//To delete the friend //To delete the friend
@ -370,7 +361,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
case R.id.action_follow: case R.id.action_follow:
onSetFollowing(mPosInContextMenu, onSetFollowing(mPosInContextMenu,
!mList.get(mPosInContextMenu).getFriend().isFollowing()); !mList.get(mPosInContextMenu).isFollowing());
return true; return true;
} }
@ -381,7 +372,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
@Override @Override
public void onSetFollowing(int pos, boolean following) { public void onSetFollowing(int pos, boolean following) {
Friend friend = mList.get(pos).getFriend(); Friend friend = mList.get(pos);
friend.setFollowing(following); friend.setFollowing(following);
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();