diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/FriendsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/FriendsList.java new file mode 100644 index 0000000..519826c --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/FriendsList.java @@ -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 { + + /** + * Get the IDs of all the friends of this list + * + * @return The list of the id of the friend + */ + public ArrayList getFriendsIDs(){ + + ArrayList 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 usersInfo){ + + //Process the list + for(Friend friend : this){ + friend.setUserInfo(usersInfo.get(friend.getId())); + } + + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListDbHelper.java index 62459f5..b28a583 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListDbHelper.java @@ -7,6 +7,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; 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 java.util.ArrayList; @@ -52,7 +53,7 @@ public class FriendsListDbHelper { * @return The list of friends of the user or null in case of failure */ @Nullable - ArrayList get_list(){ + FriendsList get_list(){ //Get read access to the database SQLiteDatabase db = dbHelper.getReadableDatabase(); @@ -68,7 +69,7 @@ public class FriendsListDbHelper { if(c == null) return null; //An error occurred - ArrayList friendsList = new ArrayList<>(); + FriendsList friendsList = new FriendsList(); //Process the list of responses c.moveToFirst(); @@ -166,7 +167,7 @@ public class FriendsListDbHelper { * * @return The number of affected rows */ - public int remove_all(){ + int remove_all(){ //Get writable database access SQLiteDatabase db = dbHelper.getWritableDatabase(); @@ -176,12 +177,7 @@ public class FriendsListDbHelper { String whereClause = FriendsListSchema._ID + " > 0"; String[] whereArgs = new String[0]; - int result = db.delete(table_name, whereClause, whereArgs); - - //Close the database - //db.close(); - - return result; + return db.delete(table_name, whereClause, whereArgs); } /** 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 4023592..b1f06bd 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 @@ -5,6 +5,7 @@ import android.content.Intent; import android.support.annotation.Nullable; 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.APIResponse; import org.communiquons.android.comunic.client.data.models.Friend; @@ -62,18 +63,14 @@ public class FriendsListHelper { * * @return The list of friends */ - public ArrayList get() { + public FriendsList get() { //Acquire friends list lock FriendsListHelper.ListAccessLock.lock(); //Fetch the list - ArrayList list; - try { - list = mDbHelper.get_list(); - } finally { - FriendsListHelper.ListAccessLock.unlock(); - } + FriendsList list = mDbHelper.get_list(); + FriendsListHelper.ListAccessLock.unlock(); return list; } 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 deleted file mode 100644 index e13dbe4..0000000 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FriendsUtils.java +++ /dev/null @@ -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 getFriendsIDs(ArrayList friendsList){ - ArrayList 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 friendsList, - ArrayMap userInfo){ - - //Process the list - for(Friend friend : friendsList){ - friend.setUserInfo(userInfo.get(friend.getId())); - } - - } -} 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 5926935..7121174 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 @@ -13,14 +13,13 @@ import android.widget.ImageView; import android.widget.TextView; 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.UserInfo; 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.views.WebUserAccountImage; -import java.util.ArrayList; - /** * 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 */ - private ArrayList mList; + private FriendsList mList; /** * Actions listener @@ -53,7 +52,7 @@ public class FriendsAdapter extends BaseRecyclerViewAdapter { * @param friendsList The list of friends to display (with user information) * @param listener Actions on friends listener */ - public FriendsAdapter(Context context, ArrayList friendsList, + public FriendsAdapter(Context context, FriendsList friendsList, OnFriendListActionListener listener){ super(context); 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 a2b9495..6abc91a 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 @@ -20,12 +20,12 @@ import android.widget.TextView; import android.widget.Toast; 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.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.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; @@ -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.views.ScrollRecyclerView; -import java.util.ArrayList; import java.util.Objects; -import static org.communiquons.android.comunic.client.data.utils.FriendsUtils.MergeFriendsListWithUserInfo; - /** * Friends list fragment * @@ -67,7 +64,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL /** * The current list of friends */ - private ArrayList mList; + private FriendsList mList; /** * Friend list operations object @@ -181,13 +178,13 @@ 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 FriendsList doInBackground(Void... params) { //Fetch the list of friends - ArrayList friendsList = mFriendsHelper.get(); + FriendsList friendsList = mFriendsHelper.get(); //Check for errors if (friendsList == null) @@ -195,20 +192,20 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL //Get user info ArrayMap userInfo = mUsersHelper.getMultiple( - FriendsUtils.getFriendsIDs(friendsList)); + friendsList.getFriendsIDs()); //Check for errors if (userInfo == null) return null; //Merge friend and user and return result - MergeFriendsListWithUserInfo(friendsList, userInfo); + friendsList.mergeFriendsListWithUserInfo(userInfo); return friendsList; } @Override - protected void onPostExecute(ArrayList friendUsers) { + protected void onPostExecute(FriendsList friendUsers) { //Check the activity still exists if (getActivity() == null) @@ -224,7 +221,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 FriendsList friendsList) { //Remove progress bar display_progress_bar(false);