From 7989c2c3779e53ff596a9020629410457f3792b6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 23 Aug 2018 14:51:05 +0200 Subject: [PATCH] Can update friend following status --- .../data/helpers/FriendsListDbHelper.java | 86 +++++++++---- .../data/helpers/FriendsListHelper.java | 48 ++++++-- .../ui/fragments/FriendsListFragment.java | 115 ++++++++++++------ .../listeners/OnFriendListActionListener.java | 10 +- ...t_friendslist_item.xml => menu_friend.xml} | 10 +- app/src/main/res/values/strings.xml | 1 + 6 files changed, 202 insertions(+), 68 deletions(-) rename app/src/main/res/menu/{menu_fragment_friendslist_item.xml => menu_friend.xml} (55%) 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 0fc5066..62459f5 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 @@ -22,6 +22,16 @@ import java.util.ArrayList; public class FriendsListDbHelper { + /** + * Required database columns + */ + private static final String[] mColumns = { + FriendsListSchema.COLUMN_NAME_FRIEND_ID, + FriendsListSchema.COLUMN_NAME_FRIEND_ACCEPTED, + FriendsListSchema.COLUMN_NAME_FRIEND_FOLLOWING, + FriendsListSchema.COLUMN_NAME_FRIEND_LAST_ACTIVITY + }; + /** * Database helper */ @@ -49,16 +59,10 @@ public class FriendsListDbHelper { //Prepare the request on the database String table_name = FriendsListSchema.TABLE_NAME; - String[] columns = { - FriendsListSchema.COLUMN_NAME_FRIEND_ID, - FriendsListSchema.COLUMN_NAME_FRIEND_ACCEPTED, - FriendsListSchema.COLUMN_NAME_FRIEND_FOLLOWING, - FriendsListSchema.COLUMN_NAME_FRIEND_LAST_ACTIVITY - }; String order = FriendsListSchema._ID; //Perform the request - Cursor c = db.query(table_name, columns, null, null, null, null, order); + Cursor c = db.query(table_name, mColumns, null, null, null, null, order); //Check if the request echoed if(c == null) @@ -70,23 +74,8 @@ public class FriendsListDbHelper { c.moveToFirst(); for(int i = 0; i < c.getCount(); i++){ - //Get information about the friend - Friend friend = new Friend(); - - friend.setId(c.getInt(c.getColumnIndexOrThrow( - FriendsListSchema.COLUMN_NAME_FRIEND_ID))); - - friend.setAccepted(c.getInt(c.getColumnIndexOrThrow( - FriendsListSchema.COLUMN_NAME_FRIEND_ACCEPTED)) == 1); - - friend.setFollowing(c.getInt(c.getColumnIndexOrThrow( - FriendsListSchema.COLUMN_NAME_FRIEND_FOLLOWING)) == 1); - - friend.setLast_activity(c.getInt(c.getColumnIndexOrThrow( - FriendsListSchema.COLUMN_NAME_FRIEND_LAST_ACTIVITY))); - //Add the friend to the list - friendsList.add(friend); + friendsList.add(parseDbEntryToFriend(c)); //Move to the next friend if(!c.moveToNext()) @@ -99,6 +88,32 @@ public class FriendsListDbHelper { return friendsList; } + /** + * Get a single friend entry + * + * @param friendID The ID of the friend to get + * @return Information about the friend / null in case of failure + */ + @Nullable + Friend getSingleFriend(int friendID){ + + SQLiteDatabase db = dbHelper.getReadableDatabase(); + + String[] selectionArgs = {friendID+""}; + Cursor c = db.query(FriendsListSchema.TABLE_NAME, mColumns, + FriendsListSchema.COLUMN_NAME_FRIEND_ID + " = ?", selectionArgs, + null, null, null); + + c.moveToFirst(); + if(c.getCount() < 1) + return null; + + Friend friend = parseDbEntryToFriend(c); + c.close(); + + return friend; + } + /** * Update the entire list of friends * @@ -234,4 +249,29 @@ public class FriendsListDbHelper { return values; } + + /** + * Parse a database entry into a cursor object + * + * @param c Database cursor + * @return Generated Friend object + */ + private Friend parseDbEntryToFriend(Cursor c){ + + Friend friend = new Friend(); + + friend.setId(c.getInt(c.getColumnIndexOrThrow( + FriendsListSchema.COLUMN_NAME_FRIEND_ID))); + + friend.setAccepted(c.getInt(c.getColumnIndexOrThrow( + FriendsListSchema.COLUMN_NAME_FRIEND_ACCEPTED)) == 1); + + friend.setFollowing(c.getInt(c.getColumnIndexOrThrow( + FriendsListSchema.COLUMN_NAME_FRIEND_FOLLOWING)) == 1); + + friend.setLast_activity(c.getInt(c.getColumnIndexOrThrow( + FriendsListSchema.COLUMN_NAME_FRIEND_LAST_ACTIVITY))); + + return friend; + } } 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 801a9a3..07e2652 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 @@ -30,7 +30,7 @@ public class FriendsListHelper { //Debug tag private static final String TAG = "FriendsList"; - private FriendsListDbHelper fdbHelper; + private FriendsListDbHelper mDbHelper; private Context mContext; //Friends list access lock @@ -42,7 +42,7 @@ public class FriendsListHelper { * @param context The context of the application */ public FriendsListHelper(Context context){ - this.fdbHelper = new FriendsListDbHelper(DatabaseHelper.getInstance(context)); + this.mDbHelper = new FriendsListDbHelper(DatabaseHelper.getInstance(context)); this.mContext = context; } @@ -53,7 +53,7 @@ public class FriendsListHelper { * @param context the context of the application */ public FriendsListHelper(DatabaseHelper dbHelper, Context context){ - this.fdbHelper = new FriendsListDbHelper(dbHelper); + this.mDbHelper = new FriendsListDbHelper(dbHelper); this.mContext = context.getApplicationContext(); } @@ -70,7 +70,7 @@ public class FriendsListHelper { //Fetch the list ArrayList list; try { - list = fdbHelper.get_list(); + list = mDbHelper.get_list(); } finally { FriendsListHelper.ListAccessLock.unlock(); } @@ -135,7 +135,7 @@ public class FriendsListHelper { //Set friend information friend.setId(friendship_infos.getInt("ID_friend")); friend.setAccepted(friendship_infos.getInt("accepted") == 1); - friend.setFollowing(friendship_infos.getInt("ID_friend") == 1); + friend.setFollowing(friendship_infos.getInt("following") == 1); friend.setLast_activity(friendship_infos.getInt("time_last_activity")); //Add the friend to the list @@ -165,7 +165,7 @@ public class FriendsListHelper { new APIRequestHelper().exec(delparams); //Remove the friend from the local database - fdbHelper.delete_friend(friend); + mDbHelper.delete_friend(friend); } catch (Exception e){ Log.e(TAG, "Couldn't delete friend !"); @@ -233,10 +233,10 @@ public class FriendsListHelper { //Update the friend in the local database if(accept) { friend.setAccepted(true); - fdbHelper.update_friend(friend); + mDbHelper.update_friend(friend); } else { - fdbHelper.delete_friend(friend); + mDbHelper.delete_friend(friend); } } catch(Exception e){ @@ -311,4 +311,36 @@ public class FriendsListHelper { } } + + /** + * Update follow status of a user + * + * @param friendID The ID of the user to update + * @param following TRUE to follow / FALSE else + * @return The result of the operation + */ + public boolean setFollowing(int friendID, boolean following){ + + //Get information about the friend + Friend friend = mDbHelper.getSingleFriend(friendID); + if(friend == null) + return false; + friend.setFollowing(following); + + //Update friend information locally + mDbHelper.update_friend(friend); + + //Update the friend on the server too + APIRequest request = new APIRequest(mContext, "friends/setFollowing"); + request.addInt("friendID", friendID); + request.addBoolean("follow", following); + + //Perform request on the server + try { + return new APIRequestHelper().exec(request).getResponse_code() == 200; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } } 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 738ac9c..d80f1a8 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 @@ -21,6 +21,7 @@ import android.widget.TextView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask; 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; @@ -65,32 +66,32 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL /** * Get user helper */ - private GetUsersHelper usersHelper; + private GetUsersHelper mUsersHelper; /** * The current list of friends */ - private ArrayList friendsList; + private ArrayList mList; /** * Friend list operations object */ - private FriendsListHelper flistHelper; + private FriendsListHelper mFriendsHelper; /** * Conversation opener */ - private openConversationListener convOpener; + private openConversationListener mConvOpener; /** * Users page opener */ - private onOpenUsersPageListener usersPageOpener; + private onOpenUsersPageListener mUsersPageOpener; /** * Friend adapter */ - private FriendsAdapter fAdapter; + private FriendsAdapter mAdapter; /** * Loading progress bar @@ -123,15 +124,15 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL mDbHelper = DatabaseHelper.getInstance(mContext); //Create friend list helper object - flistHelper = new FriendsListHelper(mDbHelper, mContext); + mFriendsHelper = new FriendsListHelper(mDbHelper, mContext); //Create get user helper - usersHelper = new GetUsersHelper(mContext, mDbHelper); + mUsersHelper = new GetUsersHelper(mContext, mDbHelper); - //Cast activity to convOpener + //Cast activity to mConvOpener try { - convOpener = (openConversationListener) getActivity(); - usersPageOpener = (onOpenUsersPageListener) getActivity(); + mConvOpener = (openConversationListener) getActivity(); + mUsersPageOpener = (onOpenUsersPageListener) getActivity(); } catch (ClassCastException e) { e.printStackTrace(); @@ -193,22 +194,22 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL protected ArrayList doInBackground(Void... params) { //Fetch the list of friends - ArrayList friendsList = flistHelper.get(); + ArrayList friendsList = mFriendsHelper.get(); //Check for errors if (friendsList == null) return null; //Get user info - ArrayMap userInfos = usersHelper.getMultiple( + ArrayMap userInfo = mUsersHelper.getMultiple( FriendsUtils.getFriendsIDs(friendsList)); //Check for errors - if (userInfos == null) + if (userInfo == null) return null; //Merge friend and user and return result - return FriendsUtils.merge_friends_user_infos_list(friendsList, userInfos); + return FriendsUtils.merge_friends_user_infos_list(friendsList, userInfo); } @@ -242,17 +243,17 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL } //Save the list of friends - this.friendsList = friendsList; + this.mList = friendsList; //Update the visibility of the no friend notice updateNoFriendNoticeVisibility(); //Set the adapter - fAdapter = new FriendsAdapter(getActivity(), friendsList, this); + mAdapter = new FriendsAdapter(getActivity(), friendsList, this); mFriendsList.setLayoutManager(new LinearLayoutManager(getActivity())); mFriendsList.addItemDecoration(new DividerItemDecoration(mFriendsList.getContext(), DividerItemDecoration.VERTICAL)); - mFriendsList.setAdapter(fAdapter); + mFriendsList.setAdapter(mAdapter); //Register the view for the context menu registerForContextMenu(mFriendsList); @@ -275,11 +276,11 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL public void onClick(DialogInterface dialog, int which) { //Get the friend to delete - final Friend toDelete = friendsList.get(pos).getFriend(); + final Friend toDelete = mList.get(pos).getFriend(); //Apply new list version - friendsList.remove(pos); - fAdapter.notifyDataSetChanged(); + mList.remove(pos); + mAdapter.notifyDataSetChanged(); //Remove the friend list on a parallel thread new AsyncTask() { @@ -287,7 +288,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL protected Void doInBackground(Integer[] params) { //Delete the friend from the list - flistHelper.remove(toDelete); + mFriendsHelper.remove(toDelete); return null; } @@ -303,30 +304,30 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL @Override public void onOpenUserPage(int userID) { - usersPageOpener.openUserPage(userID); + mUsersPageOpener.openUserPage(userID); } @Override public void onRespondFrienshipRequest(int pos, final boolean response) { //Get the Friend object - Friend targetFriend = friendsList.get(pos).getFriend(); + Friend targetFriend = mList.get(pos).getFriend(); if (response) //Mark the friend as accepted targetFriend.setAccepted(true); else //Remove the friend from the list - friendsList.remove(pos); + mList.remove(pos); //Inform the adapter the list has changed - fAdapter.notifyDataSetChanged(); + mAdapter.notifyDataSetChanged(); //Accept the request on a separate thread new AsyncTask() { @Override protected Void doInBackground(Friend... params) { - flistHelper.respondRequest(params[0], response); + mFriendsHelper.respondRequest(params[0], response); return null; } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend); @@ -339,10 +340,16 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL //Save selected position mPosInContextMenu = pos; - //Initialize and show menu + //Initialize menu PopupMenu menu = new PopupMenu(getActivity(), view); - menu.inflate(R.menu.menu_fragment_friendslist_item); + menu.inflate(R.menu.menu_friend); menu.setOnMenuItemClickListener(this); + + + //Update following checkbox + menu.getMenu().findItem(R.id.action_follow).setChecked(mList.get(pos).getFriend() + .isFollowing()); + menu.show(); } @@ -352,20 +359,60 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL switch (item.getItemId()) { //To open a private conversation with the friend - case R.id.menu_fragment_friendslist_private_conversation: - convOpener.openPrivateConversation(friendsList.get(mPosInContextMenu).getFriend().getId()); + case R.id.action_private_conversation: + mConvOpener.openPrivateConversation(mList.get(mPosInContextMenu).getFriend().getId()); return true; //To delete the friend - case R.id.menu_fragment_friendslist_delete_friend: + case R.id.action_delete_friend: delete_friend(mPosInContextMenu); return true; + + case R.id.action_follow: + onSetFollowing(mPosInContextMenu, + !mList.get(mPosInContextMenu).getFriend().isFollowing()); + return true; } return false; } + @Override + public void onSetFollowing(int pos, boolean following) { + + Friend friend = mList.get(pos).getFriend(); + friend.setFollowing(following); + + mAdapter.notifyDataSetChanged(); + + //Perform update + new SetFollowingTask(getActivity(), friend.getId(), friend.isFollowing()) + .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + } + + /** + * Class used to update following status + */ + static class SetFollowingTask extends SafeAsyncTask { + + private int friendID; + private boolean follow; + + SetFollowingTask(Context context, int friendID, boolean follow) { + super(context); + this.friendID = friendID; + this.follow = follow; + } + + @Override + protected Void doInBackground(Void... voids) { + new FriendsListHelper(getContext()).setFollowing(friendID, follow); + return null; + } + } + /** * Hide (or display) progress bar * @@ -379,7 +426,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL * Update the visibility of the no friend notice */ private void updateNoFriendNoticeVisibility() { - if (friendsList != null) - mNoFriendNotice.setVisibility(friendsList.size() == 0 ? View.VISIBLE : View.GONE); + if (mList != null) + mNoFriendNotice.setVisibility(mList.size() == 0 ? View.VISIBLE : View.GONE); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnFriendListActionListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnFriendListActionListener.java index 19fb485..5955ace 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnFriendListActionListener.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnFriendListActionListener.java @@ -19,7 +19,7 @@ public interface OnFriendListActionListener { /** * Respond to a friendship request * - * @param pos Position of the friend on the list + * @param pos Position of the friend on the list * @param response TRUE to accept / FALSE else */ void onRespondFrienshipRequest(int pos, boolean response); @@ -30,4 +30,12 @@ public interface OnFriendListActionListener { * @param pos The position of the friend in the list */ void onOpenContextMenuForFriend(View view, int pos); + + /** + * Specify whether a friend should be followed or not + * + * @param pos The position of the friend to update + * @param following New following status + */ + void onSetFollowing(int pos, boolean following); } diff --git a/app/src/main/res/menu/menu_fragment_friendslist_item.xml b/app/src/main/res/menu/menu_friend.xml similarity index 55% rename from app/src/main/res/menu/menu_fragment_friendslist_item.xml rename to app/src/main/res/menu/menu_friend.xml index 5a627d7..cd2ffb0 100644 --- a/app/src/main/res/menu/menu_fragment_friendslist_item.xml +++ b/app/src/main/res/menu/menu_friend.xml @@ -1,14 +1,20 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 97a0cd5..ccdc222 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -243,4 +243,5 @@ Reject Requested Get more options + Follow