Created FriendsList object

This commit is contained in:
Pierre HUBERT 2019-03-02 11:22:09 +01:00
parent 28e54a271b
commit 383f299455
6 changed files with 67 additions and 81 deletions

View File

@ -0,0 +1,46 @@
package org.communiquons.android.comunic.client.data.arrays;
import android.util.ArrayMap;
import org.communiquons.android.comunic.client.data.models.Friend;
import org.communiquons.android.comunic.client.data.models.UserInfo;
import java.util.ArrayList;
/**
* Friends list
*
* @author Pierre HUBERT
*/
public class FriendsList extends ArrayList<Friend> {
/**
* Get the IDs of all the friends of this list
*
* @return The list of the id of the friend
*/
public ArrayList<Integer> getFriendsIDs(){
ArrayList<Integer> IDs = new ArrayList<>();
for(Friend friend : this)
IDs.add(friend.getId());
return IDs;
}
/**
* Merge a user information list with a friend list
*
* @param usersInfo Information about the friends
*/
public void mergeFriendsListWithUserInfo(ArrayMap<Integer, UserInfo> usersInfo){
//Process the list
for(Friend friend : this){
friend.setUserInfo(usersInfo.get(friend.getId()));
}
}
}

View File

@ -7,6 +7,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import org.communiquons.android.comunic.client.data.DatabaseContract.FriendsListSchema; import org.communiquons.android.comunic.client.data.DatabaseContract.FriendsListSchema;
import org.communiquons.android.comunic.client.data.arrays.FriendsList;
import org.communiquons.android.comunic.client.data.models.Friend; import org.communiquons.android.comunic.client.data.models.Friend;
import java.util.ArrayList; import java.util.ArrayList;
@ -52,7 +53,7 @@ public class FriendsListDbHelper {
* @return The list of friends of the user or null in case of failure * @return The list of friends of the user or null in case of failure
*/ */
@Nullable @Nullable
ArrayList<Friend> get_list(){ FriendsList get_list(){
//Get read access to the database //Get read access to the database
SQLiteDatabase db = dbHelper.getReadableDatabase(); SQLiteDatabase db = dbHelper.getReadableDatabase();
@ -68,7 +69,7 @@ public class FriendsListDbHelper {
if(c == null) if(c == null)
return null; //An error occurred return null; //An error occurred
ArrayList<Friend> friendsList = new ArrayList<>(); FriendsList friendsList = new FriendsList();
//Process the list of responses //Process the list of responses
c.moveToFirst(); c.moveToFirst();
@ -166,7 +167,7 @@ public class FriendsListDbHelper {
* *
* @return The number of affected rows * @return The number of affected rows
*/ */
public int remove_all(){ int remove_all(){
//Get writable database access //Get writable database access
SQLiteDatabase db = dbHelper.getWritableDatabase(); SQLiteDatabase db = dbHelper.getWritableDatabase();
@ -176,12 +177,7 @@ public class FriendsListDbHelper {
String whereClause = FriendsListSchema._ID + " > 0"; String whereClause = FriendsListSchema._ID + " > 0";
String[] whereArgs = new String[0]; String[] whereArgs = new String[0];
int result = db.delete(table_name, whereClause, whereArgs); return db.delete(table_name, whereClause, whereArgs);
//Close the database
//db.close();
return result;
} }
/** /**

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import org.communiquons.android.comunic.client.data.arrays.FriendsList;
import org.communiquons.android.comunic.client.data.models.APIRequest; import org.communiquons.android.comunic.client.data.models.APIRequest;
import org.communiquons.android.comunic.client.data.models.APIResponse; import org.communiquons.android.comunic.client.data.models.APIResponse;
import org.communiquons.android.comunic.client.data.models.Friend; import org.communiquons.android.comunic.client.data.models.Friend;
@ -62,18 +63,14 @@ public class FriendsListHelper {
* *
* @return The list of friends * @return The list of friends
*/ */
public ArrayList<Friend> get() { public FriendsList get() {
//Acquire friends list lock //Acquire friends list lock
FriendsListHelper.ListAccessLock.lock(); FriendsListHelper.ListAccessLock.lock();
//Fetch the list //Fetch the list
ArrayList<Friend> list; FriendsList list = mDbHelper.get_list();
try { FriendsListHelper.ListAccessLock.unlock();
list = mDbHelper.get_list();
} finally {
FriendsListHelper.ListAccessLock.unlock();
}
return list; return list;
} }

View File

@ -1,49 +0,0 @@
package org.communiquons.android.comunic.client.data.utils;
import android.util.ArrayMap;
import org.communiquons.android.comunic.client.data.models.Friend;
import org.communiquons.android.comunic.client.data.models.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<Integer> getFriendsIDs(ArrayList<Friend> friendsList){
ArrayList<Integer> IDs = new ArrayList<>();
for(Friend friend : friendsList)
IDs.add(friend.getId());
return IDs;
}
/**
* Merge a user information list with a friend list
*
* @param friendsList The list of friend of the user
* @param userInfo Information about the friends
*/
public static void MergeFriendsListWithUserInfo(ArrayList<Friend> friendsList,
ArrayMap<Integer, UserInfo> userInfo){
//Process the list
for(Friend friend : friendsList){
friend.setUserInfo(userInfo.get(friend.getId()));
}
}
}

View File

@ -13,14 +13,13 @@ import android.widget.ImageView;
import android.widget.TextView; 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.arrays.FriendsList;
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.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;
import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage;
import java.util.ArrayList;
/** /**
* Adapter that render the list of friend on the friends list fragment * Adapter that render the list of friend on the friends list fragment
* *
@ -39,7 +38,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter {
/** /**
* The list of friends, with their information * The list of friends, with their information
*/ */
private ArrayList<Friend> mList; private FriendsList mList;
/** /**
* Actions listener * Actions listener
@ -53,7 +52,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter {
* @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 friends listener * @param listener Actions on friends listener
*/ */
public FriendsAdapter(Context context, ArrayList<Friend> friendsList, public FriendsAdapter(Context context, FriendsList friendsList,
OnFriendListActionListener listener){ OnFriendListActionListener listener){
super(context); super(context);

View File

@ -20,12 +20,12 @@ 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.data.arrays.FriendsList;
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.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.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.asynctasks.SafeAsyncTask;
@ -34,11 +34,8 @@ import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListe
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.Objects; import java.util.Objects;
import static org.communiquons.android.comunic.client.data.utils.FriendsUtils.MergeFriendsListWithUserInfo;
/** /**
* Friends list fragment * Friends list fragment
* *
@ -67,7 +64,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
/** /**
* The current list of friends * The current list of friends
*/ */
private ArrayList<Friend> mList; private FriendsList mList;
/** /**
* Friend list operations object * Friend list operations object
@ -181,13 +178,13 @@ 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<Friend>>() { new AsyncTask<Void, Void, FriendsList>() {
@Override @Override
protected ArrayList<Friend> doInBackground(Void... params) { protected FriendsList doInBackground(Void... params) {
//Fetch the list of friends //Fetch the list of friends
ArrayList<Friend> friendsList = mFriendsHelper.get(); FriendsList friendsList = mFriendsHelper.get();
//Check for errors //Check for errors
if (friendsList == null) if (friendsList == null)
@ -195,20 +192,20 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL
//Get user info //Get user info
ArrayMap<Integer, UserInfo> userInfo = mUsersHelper.getMultiple( ArrayMap<Integer, UserInfo> userInfo = mUsersHelper.getMultiple(
FriendsUtils.getFriendsIDs(friendsList)); friendsList.getFriendsIDs());
//Check for errors //Check for errors
if (userInfo == null) if (userInfo == null)
return null; return null;
//Merge friend and user and return result //Merge friend and user and return result
MergeFriendsListWithUserInfo(friendsList, userInfo); friendsList.mergeFriendsListWithUserInfo(userInfo);
return friendsList; return friendsList;
} }
@Override @Override
protected void onPostExecute(ArrayList<Friend> friendUsers) { protected void onPostExecute(FriendsList friendUsers) {
//Check the activity still exists //Check the activity still exists
if (getActivity() == null) if (getActivity() == null)
@ -224,7 +221,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<Friend> friendsList) { private void apply_friends_list(@Nullable FriendsList friendsList) {
//Remove progress bar //Remove progress bar
display_progress_bar(false); display_progress_bar(false);