Optimized friends fragment and related classes

This commit is contained in:
Pierre 2018-01-05 19:27:25 +01:00
parent 689b32e270
commit 5dc34057f4
9 changed files with 162 additions and 208 deletions

View File

@ -31,6 +31,7 @@ import java.util.ArrayList;
* @author Pierre HUBERT * @author Pierre HUBERT
* Created by pierre on 10/31/17. * Created by pierre on 10/31/17.
*/ */
@Deprecated
public abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIResponse> { public abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIResponse> {
/** /**

View File

@ -391,6 +391,13 @@ public class ConversationsListHelper {
*/ */
void openConversation(int id); 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);
} }
/** /**

View File

@ -12,7 +12,7 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
* list. * list.
* *
* However, it launches periodically another thread, FriendsListRefreshRunnable, which is never * 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 * @author Pierre HUBERT
* Created by pierre on 11/19/17. * 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 @Override
public void run() { public void run() {

View File

@ -96,9 +96,6 @@ public class FriendsListDbHelper {
//Close cursor //Close cursor
c.close(); c.close();
//Close the access to the database
//db.close();
return friendsList; return friendsList;
} }

View File

@ -1,11 +1,17 @@
package org.communiquons.android.comunic.client.data.friendsList; package org.communiquons.android.comunic.client.data.friendsList;
import android.content.Context; import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequest;
import org.communiquons.android.comunic.client.api.APIRequestParameters; 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.communiquons.android.comunic.client.data.DatabaseHelper;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
/** /**
* Friends list functions * Friends list functions
@ -16,7 +22,7 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
* Created by pierre on 11/19/17. * Created by pierre on 11/19/17.
*/ */
public class FriendsList { public class FriendsListHelper {
//Debug tag //Debug tag
private static final String TAG = "FriendsList"; private static final String TAG = "FriendsList";
@ -24,15 +30,86 @@ public class FriendsList {
private FriendsListDbHelper fdbHelper; private FriendsListDbHelper fdbHelper;
private Context mContext; 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 * Public application constructor
* *
* @param dbHelper Database helper * @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.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;
} }
/** /**

View File

@ -41,11 +41,11 @@ class FriendsListRefreshRunnable implements Runnable {
@Override @Override
public void run() { public void run() {
GetFriendsList getFriendsList = new GetFriendsList(mContext); FriendsListHelper friendsListHelper = new FriendsListHelper(mContext);
FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper); FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper);
//Get the latest version of the list //Get the latest version of the list
ArrayList<Friend> friendsList = getFriendsList.get(); ArrayList<Friend> friendsList = friendsListHelper.download();
//Save it (only in case of success) //Save it (only in case of success)
if(friendsList != null) if(friendsList != null)

View File

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

View File

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

View File

@ -21,14 +21,14 @@ import android.widget.Toast;
import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.MainActivity;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.DatabaseHelper; 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.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.Friend;
import org.communiquons.android.comunic.client.data.friendsList.FriendUser; 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.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.FriendsUtils;
import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask;
import java.util.ArrayList; import java.util.ArrayList;
@ -49,33 +49,60 @@ public class FriendsListFragment extends Fragment {
/** /**
* The root view of the fragment * The root view of the fragment
*/ */
View rootView; private View rootView;
/** /**
* Application context * Application context
*/ */
Context mContext; private Context mContext;
/** /**
* Database helper * Database helper
*/ */
DatabaseHelper mDbHelper; private DatabaseHelper mDbHelper;
/**
* Get user helper
*/
private GetUsersHelper usersHelper;
/** /**
* The current list of friends * The current list of friends
*/ */
ArrayList<FriendUser> friendsList; private ArrayList<FriendUser> friendsList;
/** /**
* Friend list operations object * Friend list operations object
*/ */
FriendsList flist; private FriendsListHelper flistHelper;
/**
* Conversation opener
*/
private ConversationsListHelper.openConversationListener convOpener;
/** /**
* Friend adapter * Friend adapter
*/ */
private FriendsAdapter fAdapter; 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 @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@ -89,15 +116,6 @@ public class FriendsListFragment extends Fragment {
rootView = view; 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 //Retain the fragment
//setRetainInstance(true); //setRetainInstance(true);
@ -113,11 +131,6 @@ public class FriendsListFragment extends Fragment {
//Update the bottom navigation menu //Update the bottom navigation menu
((MainActivity) getActivity()) ((MainActivity) getActivity())
.setSelectedNavigationItem(R.id.main_bottom_navigation_friends_list); .setSelectedNavigationItem(R.id.main_bottom_navigation_friends_list);
}
@Override
public void onStart() {
super.onStart();
//Refresh the friends list //Refresh the friends list
refresh_friend_list(); refresh_friend_list();
@ -131,46 +144,36 @@ public class FriendsListFragment extends Fragment {
//Display loading bar //Display loading bar
display_progress_bar(true); display_progress_bar(true);
new GetFriendsListTask(mDbHelper){ new AsyncTask<Void, Void, ArrayList<FriendUser>>(){
@Override @Override
protected void onPostExecute(final ArrayList<Friend> friendsList) { protected ArrayList<FriendUser> doInBackground(Void... params) {
//Remote progress bar //Fetch the list of friends
display_progress_bar(false); ArrayList<Friend> friendsList = flistHelper.get();
//Check for errors //Check for errors
if(friendsList == null){ if(friendsList == null)
Toast.makeText(mContext, R.string.fragment_friendslist_err_refresh, return null;
Toast.LENGTH_LONG).show();
return;
}
new GetUsersInfos(mContext, mDbHelper). //Get user infos
getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() { ArrayMap<Integer, UserInfo> userInfos = usersHelper.getMultiple(
@Override FriendsUtils.getFriendsIDs(friendsList));
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 //Check for errors
ArrayList<FriendUser> friendsUserList = FriendsUtils.merge_friends_user_infos_list( if(userInfos == null)
friendsList, return null;
info
);
//Refresh friends list //Merge friend and user and return result
apply_friends_list(friendsUserList); return FriendsUtils.merge_friends_user_infos_list(friendsList, userInfos);
}
});
} }
}.execute(); @Override
protected void onPostExecute(ArrayList<FriendUser> friendUsers) {
apply_friends_list(friendUsers);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
/** /**
@ -178,7 +181,18 @@ public class FriendsListFragment extends Fragment {
* *
* @param friendsList The friends list to apply * @param friendsList The friends list to apply
*/ */
private void apply_friends_list(final ArrayList<FriendUser> friendsList){ private void apply_friends_list(@Nullable ArrayList<FriendUser> friendsList){
//Remove progress bar
display_progress_bar(false);
//Check for errors
if(friendsList == null){
Toast.makeText(mContext, R.string.fragment_friendslist_err_refresh,
Toast.LENGTH_LONG).show();
return;
}
//Save the list of friends //Save the list of friends
this.friendsList = friendsList; this.friendsList = friendsList;
@ -211,6 +225,7 @@ public class FriendsListFragment extends Fragment {
switch (item.getItemId()){ switch (item.getItemId()){
//To delete the friend
case R.id.menu_fragment_friendslist_delete_friend: case R.id.menu_fragment_friendslist_delete_friend:
delete_friend(friendPos); delete_friend(friendPos);
return true; return true;
@ -248,7 +263,7 @@ public class FriendsListFragment extends Fragment {
protected Void doInBackground(Integer[] params) { protected Void doInBackground(Integer[] params) {
//Delete the friend from the list //Delete the friend from the list
flist.remove(toDelete); flistHelper.remove(toDelete);
return null; return null;
} }
@ -316,7 +331,7 @@ public class FriendsListFragment extends Fragment {
new AsyncTask<Friend, Void, Void>(){ new AsyncTask<Friend, Void, Void>(){
@Override @Override
protected Void doInBackground(Friend... params) { protected Void doInBackground(Friend... params) {
flist.respondRequest(params[0], accept); flistHelper.respondRequest(params[0], accept);
return null; return null;
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend);