diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetFriendsListTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetFriendsListTask.java new file mode 100644 index 0000000..bca377e --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetFriendsListTask.java @@ -0,0 +1,39 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; +import android.util.ArrayMap; + +import org.communiquons.android.comunic.client.data.arrays.FriendsList; +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.UserInfo; + +/** + * Get the list of friends of the user + * + * @author Pierre HUBERT + */ +public class GetFriendsListTask extends SafeAsyncTask { + + public GetFriendsListTask(Context context) { + super(context); + } + + @Override + protected FriendsList doInBackground(Void... voids) { + + //Fetch the list of friends + FriendsList friendsList = new FriendsListHelper(getContext()).get(); + + ArrayMap userInfo; + + //Try to get information about related users if possible + if (friendsList == null || (userInfo = new GetUsersHelper(getContext()).getMultiple( + friendsList.getFriendsIDs())) == null) + return null; + + //Merge friend and user and return result + friendsList.mergeFriendsListWithUserInfo(userInfo); + return friendsList; + } +} 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 6abc91a..0e5231f 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 @@ -6,10 +6,8 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; -import android.util.ArrayMap; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; @@ -25,9 +23,9 @@ 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.ui.activities.MainActivity; import org.communiquons.android.comunic.client.ui.adapters.FriendsAdapter; +import org.communiquons.android.comunic.client.ui.asynctasks.GetFriendsListTask; 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; @@ -43,7 +41,7 @@ import java.util.Objects; * Created by pierre on 11/11/17. */ -public class FriendsListFragment extends Fragment implements OnFriendListActionListener, +public class FriendsListFragment extends AbstractFragment implements OnFriendListActionListener, PopupMenu.OnMenuItemClickListener { /** @@ -178,42 +176,10 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL //Display loading bar display_progress_bar(true); - new AsyncTask() { - - @Override - protected FriendsList doInBackground(Void... params) { - - //Fetch the list of friends - FriendsList friendsList = mFriendsHelper.get(); - - //Check for errors - if (friendsList == null) - return null; - - //Get user info - ArrayMap userInfo = mUsersHelper.getMultiple( - friendsList.getFriendsIDs()); - - //Check for errors - if (userInfo == null) - return null; - - //Merge friend and user and return result - friendsList.mergeFriendsListWithUserInfo(userInfo); - return friendsList; - - } - - @Override - protected void onPostExecute(FriendsList friendUsers) { - - //Check the activity still exists - if (getActivity() == null) - return; - - apply_friends_list(friendUsers); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + GetFriendsListTask getFriendsListTask = new GetFriendsListTask(getActivity()); + getFriendsListTask.setOnPostExecuteListener(this::apply_friends_list); + getFriendsListTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + getTasksManager().addTask(getFriendsListTask); } /**