From 6d57c6cc0999d2241d131ebc645d76ad0a19b0b3 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 12 Apr 2018 11:16:35 +0200 Subject: [PATCH] Can send, cancel, accept, reject friendship request from UserAccessDeniedFragment. --- .../data/helpers/FriendsListHelper.java | 83 ++++++++++++++++-- .../fragments/UserAccessDeniedFragment.java | 87 ++++++++++++++++++- 2 files changed, 161 insertions(+), 9 deletions(-) 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 825ffc2..69bc5e2 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 @@ -134,6 +134,51 @@ public class FriendsListHelper { } } + /** + * Send a friendship request to a user + * + * @param friendID The ID of the target friend + * @return TRUE in case of success / FALSE else + */ + public boolean sendRequest(int friendID){ + + //Prepare the request + APIRequestParameters params = new APIRequestParameters(mContext, "friends/sendRequest"); + params.addInt("friendID", friendID); + + //Try to perform the request + try { + APIResponse response = new APIRequestHelper().exec(params); + return response.getResponse_code() == 200; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * Cancel a friendship request + * + * @param friendID The ID of the target friend + * @return TRUE in case of success / FALSE else + */ + public boolean cancelRequest(int friendID){ + + //Prepare the request + APIRequestParameters params = new APIRequestParameters(mContext, "friends/removeRequest"); + params.addInt("friendID", friendID); + + //Try to perform the request + try { + APIResponse response = new APIRequestHelper().exec(params); + return response.getResponse_code() == 200; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + /** * Respond to a friendship request * @@ -143,16 +188,12 @@ public class FriendsListHelper { public void respondRequest(Friend friend, boolean accept){ try { - //Perform a request to update the status online - APIRequestParameters reqParams = new APIRequestParameters(mContext, - "friends/respondRequest"); - reqParams.addInt("friendID", friend.getId()); - reqParams.addString("accept", accept ? "true" : "false"); - new APIRequestHelper().exec(reqParams); + //Respond to the request online + respondRequest(friend.getId(), accept); //Update the friend in the local database if(accept) { - friend.setAccepted(accept); + friend.setAccepted(true); fdbHelper.update_friend(friend); } else { @@ -165,6 +206,34 @@ public class FriendsListHelper { } } + /** + * Respond to a friendship request + * + * @param friendID The ID of the target friend + * @param accept TRUE to accept request / FALSE else + * @return TRUE in case of success / FALSE else + */ + public boolean respondRequest(int friendID, boolean accept){ + + //Perform a request to update the status online + APIRequestParameters reqParams = new APIRequestParameters(mContext, + "friends/respondRequest"); + reqParams.addInt("friendID", friendID); + reqParams.addString("accept", accept ? "true" : "false"); + + //Execute the request + try { + //Perform the request + APIResponse response = new APIRequestHelper().exec(reqParams); + return response.getResponse_code() == 200; + + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + /** * Get a friendship status * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserAccessDeniedFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserAccessDeniedFragment.java index 951c0b9..26afb13 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserAccessDeniedFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserAccessDeniedFragment.java @@ -1,6 +1,7 @@ package org.communiquons.android.comunic.client.ui.fragments; import android.app.Fragment; +import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; @@ -9,6 +10,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; @@ -27,7 +29,7 @@ import org.communiquons.android.comunic.client.ui.activities.MainActivity; * Created by pierre on 4/11/18. */ -public class UserAccessDeniedFragment extends Fragment { +public class UserAccessDeniedFragment extends Fragment implements View.OnClickListener { /** * The name in the bundle of the target user ID @@ -69,6 +71,11 @@ public class UserAccessDeniedFragment extends Fragment { */ private TextView mUserName; + /** + * Buttons list container + */ + private LinearLayout mButtonsList; + /** * Send request button */ @@ -118,6 +125,7 @@ public class UserAccessDeniedFragment extends Fragment { mUserName = view.findViewById(R.id.user_account_name); //Get the buttons + mButtonsList = view.findViewById(R.id.buttons_list); mSendRequestButton = view.findViewById(R.id.button_send_request); mCancelRequestButton = view.findViewById(R.id.button_cancel_request); mAcceptRequestButton = view.findViewById(R.id.button_accept_request); @@ -237,7 +245,8 @@ public class UserAccessDeniedFragment extends Fragment { //Save the friendship status mFriendshipStatus = status; - //Hide all the button by default + //Hide all the button by default but the list + mButtonsList.setVisibility(View.VISIBLE); mSendRequestButton.setVisibility(View.GONE); mCancelRequestButton.setVisibility(View.GONE); mAcceptRequestButton.setVisibility(View.GONE); @@ -262,5 +271,79 @@ public class UserAccessDeniedFragment extends Fragment { else { mSendRequestButton.setVisibility(View.VISIBLE); } + + //Make all the buttons click point on the fragment + mSendRequestButton.setOnClickListener(this); + mCancelRequestButton.setOnClickListener(this); + mAcceptRequestButton.setOnClickListener(this); + mRejectRequestButton.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + + //Make buttons list disappear + mButtonsList.setVisibility(View.INVISIBLE); + + //Get the ID of the button + int action_id = v.getId(); + + //Process the request + new AsyncTask(){ + + @Override + protected FriendshipStatus doInBackground(Integer... params) { + + //Perform requested action + performFriendshipUpdate(params[0]); + + //Return new friendship status + return mFriendListHelper.getFrienshipStatus(mUserID); + } + + @Override + protected void onPostExecute(FriendshipStatus status) { + if(getActivity() == null) + return; + + onGotFriendshipStatus(status); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, action_id); + + } + + /** + * Perform the update of the friendship status, as requested per the click on the buttons + * + * @param action_id The ID of the button that has been clicked + */ + private void performFriendshipUpdate(int action_id){ + + switch (action_id){ + + //Send a friendship request + case R.id.button_send_request: + mFriendListHelper.sendRequest(mUserID); + break; + + //Accept a friendship request + case R.id.button_accept_request: + mFriendListHelper.respondRequest(mUserID, true); + break; + + //Reject a friendship request + case R.id.button_reject_request: + mFriendListHelper.respondRequest(mUserID, false); + break; + + //Cancel a friendship request + case R.id.button_cancel_request: + mFriendListHelper.cancelRequest(mUserID); + break; + + default: + throw new RuntimeException("Unsupported action by updater!"); + } + } }