mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 15:59:29 +00:00
Optimized friends fragment and related classes
This commit is contained in:
parent
689b32e270
commit
5dc34057f4
@ -31,6 +31,7 @@ import java.util.ArrayList;
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 10/31/17.
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIResponse> {
|
||||
|
||||
/**
|
||||
|
@ -391,6 +391,13 @@ public class ConversationsListHelper {
|
||||
*/
|
||||
void openConversation(int id);
|
||||
|
||||
/**
|
||||
* Open a private conversation with the specified user ID
|
||||
*
|
||||
* @param userID The ID with who to start a private conversation
|
||||
*/
|
||||
void openPrivateConversation(int userID);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,7 +12,7 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
* list.
|
||||
*
|
||||
* However, it launches periodically another thread, FriendsListRefreshRunnable, which is never
|
||||
* killed, in order not to get errors.
|
||||
* killed, in order not to download errors.
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/19/17.
|
||||
@ -31,7 +31,7 @@ public class FriendRefreshLoopRunnable implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Perpetual loop that only stops when it get killed
|
||||
* Perpetual loop that only stops when it download killed
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -96,9 +96,6 @@ public class FriendsListDbHelper {
|
||||
//Close cursor
|
||||
c.close();
|
||||
|
||||
//Close the access to the database
|
||||
//db.close();
|
||||
|
||||
return friendsList;
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,17 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import org.communiquons.android.comunic.client.api.APIRequest;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestParameters;
|
||||
import org.communiquons.android.comunic.client.api.APIResponse;
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Friends list functions
|
||||
@ -16,7 +22,7 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
* Created by pierre on 11/19/17.
|
||||
*/
|
||||
|
||||
public class FriendsList {
|
||||
public class FriendsListHelper {
|
||||
|
||||
//Debug tag
|
||||
private static final String TAG = "FriendsList";
|
||||
@ -24,15 +30,86 @@ public class FriendsList {
|
||||
private FriendsListDbHelper fdbHelper;
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Public constructor
|
||||
*
|
||||
* @param context The context of the application
|
||||
*/
|
||||
public FriendsListHelper(Context context){
|
||||
this.fdbHelper = new FriendsListDbHelper(DatabaseHelper.getInstance(context));
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public application constructor
|
||||
*
|
||||
* @param dbHelper Database helper
|
||||
* @param mContext the context of the application
|
||||
* @param context the context of the application
|
||||
*/
|
||||
public FriendsList(DatabaseHelper dbHelper, Context mContext){
|
||||
public FriendsListHelper(DatabaseHelper dbHelper, Context context){
|
||||
this.fdbHelper = new FriendsListDbHelper(dbHelper);
|
||||
this.mContext = mContext;
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return the friends list
|
||||
*
|
||||
* @return The list of firned
|
||||
*/
|
||||
public ArrayList<Friend> get(){
|
||||
return fdbHelper.get_list();
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a new version of the friends list
|
||||
*
|
||||
* @return The list of friend of the user
|
||||
*/
|
||||
@Nullable
|
||||
ArrayList<Friend> download(){
|
||||
|
||||
//Prepare the API request
|
||||
APIRequestParameters params = new APIRequestParameters(mContext, "friends/getList");
|
||||
|
||||
//Prepare the result
|
||||
ArrayList<Friend> friends = new ArrayList<>();
|
||||
|
||||
try {
|
||||
|
||||
//Perform the request and retrieve the response
|
||||
APIResponse response = new APIRequest().exec(params);
|
||||
JSONArray friendsList = response.getJSONArray();
|
||||
|
||||
if(friendsList == null)
|
||||
return null;
|
||||
|
||||
//Process JSON array
|
||||
for(int i = 0; i < friendsList.length(); i++){
|
||||
|
||||
//Try to extract JSON object containing informations
|
||||
JSONObject friendship_infos = friendsList.getJSONObject(i);
|
||||
|
||||
//Save informations about the friend in the friend object
|
||||
Friend friend = new Friend();
|
||||
|
||||
//Set friend informations
|
||||
friend.setId(friendship_infos.getInt("ID_friend"));
|
||||
friend.setAccepted(friendship_infos.getInt("accepted") == 1);
|
||||
friend.setFollowing(friendship_infos.getInt("ID_friend") == 1);
|
||||
friend.setLast_activity(friendship_infos.getInt("time_last_activity"));
|
||||
|
||||
//Add the friend to the list
|
||||
friends.add(friend);
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return friends;
|
||||
}
|
||||
|
||||
/**
|
@ -41,11 +41,11 @@ class FriendsListRefreshRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
GetFriendsList getFriendsList = new GetFriendsList(mContext);
|
||||
FriendsListHelper friendsListHelper = new FriendsListHelper(mContext);
|
||||
FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper);
|
||||
|
||||
//Get the latest version of the list
|
||||
ArrayList<Friend> friendsList = getFriendsList.get();
|
||||
ArrayList<Friend> friendsList = friendsListHelper.download();
|
||||
|
||||
//Save it (only in case of success)
|
||||
if(friendsList != null)
|
||||
|
@ -1,89 +0,0 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.communiquons.android.comunic.client.api.APIRequest;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestParameters;
|
||||
import org.communiquons.android.comunic.client.api.APIResponse;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Fetch and return friends list
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/12/17.
|
||||
*/
|
||||
|
||||
class GetFriendsList {
|
||||
|
||||
/**
|
||||
* The context of execution of the application
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Public constructor of the class
|
||||
*
|
||||
* @param context the context of execution of the application
|
||||
*/
|
||||
GetFriendsList(Context context){
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return the friends list
|
||||
*
|
||||
* @return The list of friend of the user
|
||||
*/
|
||||
@Nullable
|
||||
ArrayList<Friend> get(){
|
||||
|
||||
//Prepare the API request
|
||||
APIRequestParameters params = new APIRequestParameters(mContext, "friends/getList");
|
||||
|
||||
//Prepare the result
|
||||
ArrayList<Friend> friends = new ArrayList<>();
|
||||
|
||||
try {
|
||||
|
||||
//Perform the request and retrieve the response
|
||||
APIResponse response = new APIRequest().exec(params);
|
||||
JSONArray friendsList = response.getJSONArray();
|
||||
|
||||
if(friendsList == null)
|
||||
return null;
|
||||
|
||||
//Process JSON array
|
||||
for(int i = 0; i < friendsList.length(); i++){
|
||||
|
||||
//Try to extract JSON object containing informations
|
||||
JSONObject friendship_infos = friendsList.getJSONObject(i);
|
||||
|
||||
//Save informations about the friend in the friend object
|
||||
Friend friend = new Friend();
|
||||
|
||||
//Set friend informations
|
||||
friend.setId(friendship_infos.getInt("ID_friend"));
|
||||
friend.setAccepted(friendship_infos.getInt("accepted") == 1);
|
||||
friend.setFollowing(friendship_infos.getInt("ID_friend") == 1);
|
||||
friend.setLast_activity(friendship_infos.getInt("time_last_activity"));
|
||||
|
||||
//Add the friend to the list
|
||||
friends.add(friend);
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return friends;
|
||||
}
|
||||
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class handles the asynchronous refresh of the friends list
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/12/17.
|
||||
*/
|
||||
|
||||
public abstract class GetFriendsListTask extends AsyncTask<Void, Void, ArrayList<Friend>> {
|
||||
|
||||
/**
|
||||
* The context of the task
|
||||
*/
|
||||
private DatabaseHelper dbHelper;
|
||||
|
||||
/**
|
||||
* Public constructor to the class
|
||||
*
|
||||
* @param dbHelper Database helper
|
||||
*/
|
||||
public GetFriendsListTask(DatabaseHelper dbHelper){
|
||||
this.dbHelper = dbHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* What to to once the users list has been refreshed
|
||||
*
|
||||
* It it the role of each class that use this task class to implement this method
|
||||
*
|
||||
* @param friendsList The list of friends of the user / null in case of failure
|
||||
*/
|
||||
@Override
|
||||
abstract protected void onPostExecute(ArrayList<Friend> friendsList);
|
||||
|
||||
/**
|
||||
* Background operation
|
||||
*
|
||||
* @param params
|
||||
* @return The list of friends of the user
|
||||
*/
|
||||
@Override
|
||||
protected ArrayList<Friend> doInBackground(Void... params) {
|
||||
//Create the GetFriendList object and use it to fetch the list
|
||||
return new FriendsListDbHelper(dbHelper).get_list();
|
||||
}
|
||||
}
|
@ -21,14 +21,14 @@ import android.widget.Toast;
|
||||
import org.communiquons.android.comunic.client.MainActivity;
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
|
||||
import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.Friend;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.FriendUser;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.FriendsAdapter;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.FriendsList;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.FriendsListHelper;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.FriendsUtils;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -49,33 +49,60 @@ public class FriendsListFragment extends Fragment {
|
||||
/**
|
||||
* The root view of the fragment
|
||||
*/
|
||||
View rootView;
|
||||
private View rootView;
|
||||
|
||||
/**
|
||||
* Application context
|
||||
*/
|
||||
Context mContext;
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Database helper
|
||||
*/
|
||||
DatabaseHelper mDbHelper;
|
||||
private DatabaseHelper mDbHelper;
|
||||
|
||||
/**
|
||||
* Get user helper
|
||||
*/
|
||||
private GetUsersHelper usersHelper;
|
||||
|
||||
/**
|
||||
* The current list of friends
|
||||
*/
|
||||
ArrayList<FriendUser> friendsList;
|
||||
private ArrayList<FriendUser> friendsList;
|
||||
|
||||
/**
|
||||
* Friend list operations object
|
||||
*/
|
||||
FriendsList flist;
|
||||
private FriendsListHelper flistHelper;
|
||||
|
||||
/**
|
||||
* Conversation opener
|
||||
*/
|
||||
private ConversationsListHelper.openConversationListener convOpener;
|
||||
|
||||
/**
|
||||
* Friend adapter
|
||||
*/
|
||||
private FriendsAdapter fAdapter;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
//Save application context
|
||||
mContext = getActivity().getApplicationContext();
|
||||
|
||||
//Create database helper
|
||||
mDbHelper = DatabaseHelper.getInstance(mContext);
|
||||
|
||||
//Create friendlist operation object
|
||||
flistHelper = new FriendsListHelper(mDbHelper, mContext);
|
||||
|
||||
//Create get user helper
|
||||
usersHelper = new GetUsersHelper(mContext, mDbHelper);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@ -89,15 +116,6 @@ public class FriendsListFragment extends Fragment {
|
||||
|
||||
rootView = view;
|
||||
|
||||
//Save application context
|
||||
mContext = getActivity().getApplicationContext();
|
||||
|
||||
//Create database helper
|
||||
mDbHelper = DatabaseHelper.getInstance(mContext);
|
||||
|
||||
//Create friendlist operation object
|
||||
flist = new FriendsList(mDbHelper, mContext);
|
||||
|
||||
//Retain the fragment
|
||||
//setRetainInstance(true);
|
||||
|
||||
@ -113,11 +131,6 @@ public class FriendsListFragment extends Fragment {
|
||||
//Update the bottom navigation menu
|
||||
((MainActivity) getActivity())
|
||||
.setSelectedNavigationItem(R.id.main_bottom_navigation_friends_list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
//Refresh the friends list
|
||||
refresh_friend_list();
|
||||
@ -131,12 +144,46 @@ public class FriendsListFragment extends Fragment {
|
||||
//Display loading bar
|
||||
display_progress_bar(true);
|
||||
|
||||
new GetFriendsListTask(mDbHelper){
|
||||
new AsyncTask<Void, Void, ArrayList<FriendUser>>(){
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(final ArrayList<Friend> friendsList) {
|
||||
protected ArrayList<FriendUser> doInBackground(Void... params) {
|
||||
|
||||
//Remote progress bar
|
||||
//Fetch the list of friends
|
||||
ArrayList<Friend> friendsList = flistHelper.get();
|
||||
|
||||
//Check for errors
|
||||
if(friendsList == null)
|
||||
return null;
|
||||
|
||||
//Get user infos
|
||||
ArrayMap<Integer, UserInfo> userInfos = usersHelper.getMultiple(
|
||||
FriendsUtils.getFriendsIDs(friendsList));
|
||||
|
||||
//Check for errors
|
||||
if(userInfos == null)
|
||||
return null;
|
||||
|
||||
//Merge friend and user and return result
|
||||
return FriendsUtils.merge_friends_user_infos_list(friendsList, userInfos);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(ArrayList<FriendUser> friendUsers) {
|
||||
apply_friends_list(friendUsers);
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the the list of friend attached to the list adapter
|
||||
*
|
||||
* @param friendsList The friends list to apply
|
||||
*/
|
||||
private void apply_friends_list(@Nullable ArrayList<FriendUser> friendsList){
|
||||
|
||||
//Remove progress bar
|
||||
display_progress_bar(false);
|
||||
|
||||
//Check for errors
|
||||
@ -146,39 +193,6 @@ public class FriendsListFragment extends Fragment {
|
||||
return;
|
||||
}
|
||||
|
||||
new GetUsersInfos(mContext, mDbHelper).
|
||||
getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() {
|
||||
@Override
|
||||
public void callback(ArrayMap<Integer, UserInfo> info) {
|
||||
//Check for errors
|
||||
if (info == null) {
|
||||
Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
//Merge the user informations list and friends List into FriendInfo list
|
||||
ArrayList<FriendUser> friendsUserList = FriendsUtils.merge_friends_user_infos_list(
|
||||
friendsList,
|
||||
info
|
||||
);
|
||||
|
||||
//Refresh friends list
|
||||
apply_friends_list(friendsUserList);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the the list of friend attached to the list adapter
|
||||
*
|
||||
* @param friendsList The friends list to apply
|
||||
*/
|
||||
private void apply_friends_list(final ArrayList<FriendUser> friendsList){
|
||||
|
||||
//Save the list of friends
|
||||
this.friendsList = friendsList;
|
||||
@ -211,6 +225,7 @@ public class FriendsListFragment extends Fragment {
|
||||
|
||||
switch (item.getItemId()){
|
||||
|
||||
//To delete the friend
|
||||
case R.id.menu_fragment_friendslist_delete_friend:
|
||||
delete_friend(friendPos);
|
||||
return true;
|
||||
@ -248,7 +263,7 @@ public class FriendsListFragment extends Fragment {
|
||||
protected Void doInBackground(Integer[] params) {
|
||||
|
||||
//Delete the friend from the list
|
||||
flist.remove(toDelete);
|
||||
flistHelper.remove(toDelete);
|
||||
|
||||
return null;
|
||||
}
|
||||
@ -316,7 +331,7 @@ public class FriendsListFragment extends Fragment {
|
||||
new AsyncTask<Friend, Void, Void>(){
|
||||
@Override
|
||||
protected Void doInBackground(Friend... params) {
|
||||
flist.respondRequest(params[0], accept);
|
||||
flistHelper.respondRequest(params[0], accept);
|
||||
return null;
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend);
|
||||
|
Loading…
Reference in New Issue
Block a user