diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java index 0a19a81..ff50ae9 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java @@ -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 { /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java index 4806149..55f483a 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -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); + } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java index 4902f42..1d58f48 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java @@ -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() { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java index 1d54523..09337e6 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java @@ -96,9 +96,6 @@ public class FriendsListDbHelper { //Close cursor c.close(); - //Close the access to the database - //db.close(); - return friendsList; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListHelper.java similarity index 50% rename from app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java rename to app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListHelper.java index 524250d..0ff0631 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListHelper.java @@ -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 get(){ + return fdbHelper.get_list(); + } + + /** + * Download a new version of the friends list + * + * @return The list of friend of the user + */ + @Nullable + ArrayList download(){ + + //Prepare the API request + APIRequestParameters params = new APIRequestParameters(mContext, "friends/getList"); + + //Prepare the result + ArrayList 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; } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java index f7333be..a78ff54 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java @@ -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 friendsList = getFriendsList.get(); + ArrayList friendsList = friendsListHelper.download(); //Save it (only in case of success) if(friendsList != null) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsList.java deleted file mode 100644 index 2136ecd..0000000 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsList.java +++ /dev/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 get(){ - - //Prepare the API request - APIRequestParameters params = new APIRequestParameters(mContext, "friends/getList"); - - //Prepare the result - ArrayList 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; - } - -} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsListTask.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsListTask.java deleted file mode 100644 index 22b1278..0000000 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsListTask.java +++ /dev/null @@ -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> { - - /** - * 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 friendsList); - - /** - * Background operation - * - * @param params - * @return The list of friends of the user - */ - @Override - protected ArrayList doInBackground(Void... params) { - //Create the GetFriendList object and use it to fetch the list - return new FriendsListDbHelper(dbHelper).get_list(); - } -} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java index 2219a5d..5a54628 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java @@ -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 friendsList; + private ArrayList 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,46 +144,36 @@ public class FriendsListFragment extends Fragment { //Display loading bar display_progress_bar(true); - new GetFriendsListTask(mDbHelper){ + new AsyncTask>(){ @Override - protected void onPostExecute(final ArrayList friendsList) { + protected ArrayList doInBackground(Void... params) { - //Remote progress bar - display_progress_bar(false); + //Fetch the list of friends + ArrayList friendsList = flistHelper.get(); //Check for errors - if(friendsList == null){ - Toast.makeText(mContext, R.string.fragment_friendslist_err_refresh, - Toast.LENGTH_LONG).show(); - return; - } + if(friendsList == null) + return null; - new GetUsersInfos(mContext, mDbHelper). - getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() { - @Override - public void callback(ArrayMap info) { - //Check for errors - if (info == null) { - Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos, - Toast.LENGTH_SHORT).show(); - return; - } + //Get user infos + ArrayMap userInfos = usersHelper.getMultiple( + FriendsUtils.getFriendsIDs(friendsList)); - //Merge the user informations list and friends List into FriendInfo list - ArrayList friendsUserList = FriendsUtils.merge_friends_user_infos_list( - friendsList, - info - ); + //Check for errors + if(userInfos == null) + return null; - //Refresh friends list - apply_friends_list(friendsUserList); - } - }); + //Merge friend and user and return result + return FriendsUtils.merge_friends_user_infos_list(friendsList, userInfos); } - }.execute(); + @Override + protected void onPostExecute(ArrayList 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 */ - private void apply_friends_list(final ArrayList friendsList){ + private void apply_friends_list(@Nullable ArrayList 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 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(){ @Override protected Void doInBackground(Friend... params) { - flist.respondRequest(params[0], accept); + flistHelper.respondRequest(params[0], accept); return null; } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend);