Can send, cancel, accept, reject friendship request from UserAccessDeniedFragment.

This commit is contained in:
Pierre 2018-04-12 11:16:35 +02:00
parent d45bfbf284
commit 6d57c6cc09
2 changed files with 161 additions and 9 deletions

View File

@ -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 * Respond to a friendship request
* *
@ -143,16 +188,12 @@ public class FriendsListHelper {
public void respondRequest(Friend friend, boolean accept){ public void respondRequest(Friend friend, boolean accept){
try { try {
//Perform a request to update the status online //Respond to the request online
APIRequestParameters reqParams = new APIRequestParameters(mContext, respondRequest(friend.getId(), accept);
"friends/respondRequest");
reqParams.addInt("friendID", friend.getId());
reqParams.addString("accept", accept ? "true" : "false");
new APIRequestHelper().exec(reqParams);
//Update the friend in the local database //Update the friend in the local database
if(accept) { if(accept) {
friend.setAccepted(accept); friend.setAccepted(true);
fdbHelper.update_friend(friend); fdbHelper.update_friend(friend);
} }
else { 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 * Get a friendship status
* *

View File

@ -1,6 +1,7 @@
package org.communiquons.android.comunic.client.ui.fragments; package org.communiquons.android.comunic.client.ui.fragments;
import android.app.Fragment; import android.app.Fragment;
import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -9,6 +10,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -27,7 +29,7 @@ import org.communiquons.android.comunic.client.ui.activities.MainActivity;
* Created by pierre on 4/11/18. * 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 * The name in the bundle of the target user ID
@ -69,6 +71,11 @@ public class UserAccessDeniedFragment extends Fragment {
*/ */
private TextView mUserName; private TextView mUserName;
/**
* Buttons list container
*/
private LinearLayout mButtonsList;
/** /**
* Send request button * Send request button
*/ */
@ -118,6 +125,7 @@ public class UserAccessDeniedFragment extends Fragment {
mUserName = view.findViewById(R.id.user_account_name); mUserName = view.findViewById(R.id.user_account_name);
//Get the buttons //Get the buttons
mButtonsList = view.findViewById(R.id.buttons_list);
mSendRequestButton = view.findViewById(R.id.button_send_request); mSendRequestButton = view.findViewById(R.id.button_send_request);
mCancelRequestButton = view.findViewById(R.id.button_cancel_request); mCancelRequestButton = view.findViewById(R.id.button_cancel_request);
mAcceptRequestButton = view.findViewById(R.id.button_accept_request); mAcceptRequestButton = view.findViewById(R.id.button_accept_request);
@ -237,7 +245,8 @@ public class UserAccessDeniedFragment extends Fragment {
//Save the friendship status //Save the friendship status
mFriendshipStatus = 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); mSendRequestButton.setVisibility(View.GONE);
mCancelRequestButton.setVisibility(View.GONE); mCancelRequestButton.setVisibility(View.GONE);
mAcceptRequestButton.setVisibility(View.GONE); mAcceptRequestButton.setVisibility(View.GONE);
@ -262,5 +271,79 @@ public class UserAccessDeniedFragment extends Fragment {
else { else {
mSendRequestButton.setVisibility(View.VISIBLE); 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<Integer, Void, FriendshipStatus>(){
@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!");
}
} }
} }