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 b9ce423..8d69388 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 @@ -8,17 +8,16 @@ import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; -import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.helpers.FriendsListHelper; import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; -import org.communiquons.android.comunic.client.data.models.FriendshipStatus; import org.communiquons.android.comunic.client.data.models.UserInfo; +import org.communiquons.android.comunic.client.ui.listeners.OnFriendsStatusUpdateListener; import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; +import org.communiquons.android.comunic.client.ui.views.FriendshipStatusButton; import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; /** @@ -28,7 +27,7 @@ import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; * Created by pierre on 4/11/18. */ -public class UserAccessDeniedFragment extends Fragment implements View.OnClickListener { +public class UserAccessDeniedFragment extends Fragment implements OnFriendsStatusUpdateListener { /** * The name in the bundle of the target user ID @@ -60,11 +59,6 @@ public class UserAccessDeniedFragment extends Fragment implements View.OnClickLi */ private UserInfo mUserInfo; - /** - * Information about the friendship status - */ - private FriendshipStatus mFriendshipStatus; - /** * User account image */ @@ -76,29 +70,9 @@ public class UserAccessDeniedFragment extends Fragment implements View.OnClickLi private TextView mUserName; /** - * Buttons list container + * Friendship status button */ - private LinearLayout mButtonsList; - - /** - * Send request button - */ - private Button mSendRequestButton; - - /** - * Cancel request button - */ - private Button mCancelRequestButton; - - /** - * Accept request button - */ - private Button mAcceptRequestButton; - - /** - * Reject request button - */ - private Button mRejectRequestButton; + private FriendshipStatusButton mFriendshipStatus; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -133,22 +107,21 @@ public class UserAccessDeniedFragment extends Fragment implements View.OnClickLi public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - //Get user related fields mUserImage = view.findViewById(R.id.user_account_image); mUserName = view.findViewById(R.id.user_account_name); + mFriendshipStatus = view.findViewById(R.id.friendship_status); - //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); - mRejectRequestButton = view.findViewById(R.id.button_reject_request); + mFriendshipStatus.setOnFriendsStatusUpdateListener(this); } @Override public void onResume() { super.onResume(); + //Initialize FrienshipStatus button + mFriendshipStatus.setUserID(mUserID); + mFriendshipStatus.refreshIfRequired(); + //Check if it is required to fetch user information if(mUserInfo == null){ getUserInfo(); @@ -204,157 +177,11 @@ public class UserAccessDeniedFragment extends Fragment implements View.OnClickLi mUserName.setText(mUserInfo.getDisplayFullName()); mUserImage.setUser(mUserInfo); - //Check if we have got the friends - if(mFriendshipStatus == null){ - getFrienshipStatus(); - } - else { - onGotFriendshipStatus(mFriendshipStatus); - } } - /** - * Get the information about the friendship between the two users - */ - private void getFrienshipStatus(){ - - //Perform the request in the background - new AsyncTask(){ - - @Override - protected FriendshipStatus doInBackground(Integer... params) { - return mFriendListHelper.getFrienshipStatus(params[0]); - } - - @Override - protected void onPostExecute(FriendshipStatus status) { - if(getActivity() == null) - return; - - onGotFriendshipStatus(status); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mUserID); - - } - - /** - * This method is called once we got information about the friendship status - * - * @param status The status of the frienship or null in case of failure - */ - private void onGotFriendshipStatus(@Nullable FriendshipStatus status){ - - //Check for errors - if(status == null){ - Toast.makeText(getActivity(), R.string.err_get_friendship_status, - Toast.LENGTH_SHORT).show(); - return; - } - - //Save the friendship status - mFriendshipStatus = status; - - //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); - mRejectRequestButton.setVisibility(View.GONE); - - //Check if the users are friend - if(mFriendshipStatus.isFriend()) { - mOpenUsersPageListener.openUserPage(mUserID); - return; - } - - //Check if the current user has sent a friendship request - if(mFriendshipStatus.isSentRequest()){ - mCancelRequestButton.setVisibility(View.VISIBLE); - } - - //Check if the current user has received a friendship request - else if(mFriendshipStatus.isReceivedRequest()){ - mAcceptRequestButton.setVisibility(View.VISIBLE); - mRejectRequestButton.setVisibility(View.VISIBLE); - } - - //Else the users have not pending request - 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!"); - } - + public void onAreFriend() { + mOpenUsersPageListener.openUserPage(mUserID); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnFriendsStatusUpdateListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnFriendsStatusUpdateListener.java new file mode 100644 index 0000000..bbb4e20 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnFriendsStatusUpdateListener.java @@ -0,0 +1,17 @@ +package org.communiquons.android.comunic.client.ui.listeners; + +/** + * On friend status update listener + * + * @author Pierre HUBERT + */ +public interface OnFriendsStatusUpdateListener { + + /** + * This method is called when the two people are friends. + * + * Warning ! This may be called several times... + */ + void onAreFriend(); + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/FriendshipStatusButton.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/FriendshipStatusButton.java new file mode 100644 index 0000000..74f23bf --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/FriendshipStatusButton.java @@ -0,0 +1,339 @@ +package org.communiquons.android.comunic.client.ui.views; + +import android.content.Context; +import android.os.AsyncTask; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +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.FriendsListHelper; +import org.communiquons.android.comunic.client.data.models.FriendshipStatus; +import org.communiquons.android.comunic.client.ui.listeners.OnFriendsStatusUpdateListener; + +/** + * Friendship status button + * + * @author Pierre HUBERT + */ +public class FriendshipStatusButton extends BaseFrameLayoutView implements View.OnClickListener { + + /** + * Debug tag + */ + private static final String TAG = FriendshipStatusButton.class.getCanonicalName(); + + /** + * The ID of the target user + */ + private int mUserID; + + /** + * Information about the friendship status + */ + private FriendshipStatus mFriendshipStatus; + + /** + * Buttons list container + */ + private LinearLayout mButtonsList; + + /** + * Actions buttons + */ + private Button mSendRequestButton; + private Button mCancelRequestButton; + private Button mAcceptRequestButton; + private Button mRejectRequestButton; + private Button mFollowButton; + private Button mFollowingButton; + + /** + * Progress bar + */ + private ProgressBar mProgressBar; + + /** + * AsyncTasks + */ + private GetFriendshipStatus mGetFriendShipStatus; + private UpdateStatusClass mUpdateStatusClass; + + /** + * On friend status update listener + */ + private OnFriendsStatusUpdateListener mOnFriendsStatusUpdateListener; + + public FriendshipStatusButton(@NonNull Context context) { + this(context, null); + } + + public FriendshipStatusButton(@NonNull Context context, @Nullable AttributeSet attrs) { + this(context, attrs, 0); + } + + public FriendshipStatusButton(@NonNull Context context, @Nullable AttributeSet attrs, + int defStyleAttr) { + super(context, attrs, defStyleAttr); + + View view = inflate(getContext(), R.layout.view_frienship_status, this); + + mProgressBar = view.findViewById(R.id.progressBar); + mButtonsList = view.findViewById(R.id.buttons_list); + 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); + mRejectRequestButton = view.findViewById(R.id.button_reject_request); + mFollowButton = view.findViewById(R.id.button_follow); + mFollowingButton = view.findViewById(R.id.button_following); + + mSendRequestButton.setOnClickListener(this); + mCancelRequestButton.setOnClickListener(this); + mAcceptRequestButton.setOnClickListener(this); + mRejectRequestButton.setOnClickListener(this); + mFollowButton.setOnClickListener(this); + mFollowingButton.setOnClickListener(this); + + hideAllButtons(); + setProgressBarVisibility(true); + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + + if(mGetFriendShipStatus != null) + mGetFriendShipStatus.setOnPostExecuteListener(null); + + if(mUpdateStatusClass != null) + mUpdateStatusClass.setOnPostExecuteListener(null); + } + + /** + * Set the ID of the target user + * + * @param mUserID The ID of the target user + */ + public void setUserID(int mUserID) { + this.mUserID = mUserID; + } + + public OnFriendsStatusUpdateListener getOnFriendsStatusUpdateListener() { + return mOnFriendsStatusUpdateListener; + } + + public void setOnFriendsStatusUpdateListener(OnFriendsStatusUpdateListener onFriendsStatusUpdateListener) { + this.mOnFriendsStatusUpdateListener = onFriendsStatusUpdateListener; + } + + /** + * Refresh current user status, only if required + */ + public void refreshIfRequired(){ + if(mFriendshipStatus == null) + refresh(); + } + + /** + * Refresh current friendship status + */ + public void refresh(){ + + hideAllButtons(); + setProgressBarVisibility(true); + + mGetFriendShipStatus = new GetFriendshipStatus(getContext()); + mGetFriendShipStatus.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mUserID); + mGetFriendShipStatus.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { + @Override + public void OnPostExecute(FriendshipStatus friendshipStatus) { + onGotFriendshipsStatus(friendshipStatus); + } + }); + } + + + + /** + * Method called when we have got friendship status + * + * @param friendshipStatus New friendship status + */ + private void onGotFriendshipsStatus(@Nullable FriendshipStatus friendshipStatus){ + + hideAllButtons(); + setProgressBarVisibility(false); + + mFriendshipStatus = friendshipStatus; + + if(friendshipStatus == null) { + Toast.makeText(getContext(), R.string.err_get_friendship_status, + Toast.LENGTH_SHORT).show(); + return; + } + + + //Update buttons visibility + if(!mFriendshipStatus.isFriend()){ + + if(mFriendshipStatus.isSentRequest()) + mCancelRequestButton.setVisibility(View.VISIBLE); + + else if(mFriendshipStatus.isReceivedRequest()){ + mAcceptRequestButton.setVisibility(View.VISIBLE); + mRejectRequestButton.setVisibility(View.VISIBLE); + } + + else + mSendRequestButton.setVisibility(View.VISIBLE); + } + + else { + + //The two people are friends + mOnFriendsStatusUpdateListener.onAreFriend(); + + if(mFriendshipStatus.isFollowing()) + mFollowingButton.setVisibility(View.VISIBLE); + else + mFollowingButton.setVisibility(View.GONE); + } + } + + @Override + public void onClick(View v) { + int action_id = v.getId(); + hideAllButtons(); + setProgressBarVisibility(true); + + mUpdateStatusClass = new UpdateStatusClass(mUserID, action_id, getContext()); + mUpdateStatusClass.setOnPostExecuteListener(new SafeAsyncTask. + OnPostExecuteListener() { + @Override + public void OnPostExecute(FriendshipStatus friendshipStatus) { + onGotFriendshipsStatus(friendshipStatus); + } + }); + mUpdateStatusClass.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + + /** + * Hide all view buttons + */ + private void hideAllButtons(){ + mSendRequestButton.setVisibility(View.GONE); + mCancelRequestButton.setVisibility(View.GONE); + mAcceptRequestButton.setVisibility(View.GONE); + mRejectRequestButton.setVisibility(View.GONE); + mFollowButton.setVisibility(View.GONE); + mFollowingButton.setVisibility(View.GONE); + } + + /** + * Update progress bar visibility + * + * @param visible TRUE : visible / FALSE : else + */ + private void setProgressBarVisibility(boolean visible){ + mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE); + } + + /** + * Safe AsyncTask used to get current friendship status + */ + private static class GetFriendshipStatus extends SafeAsyncTask{ + + GetFriendshipStatus(Context context) { + super(context); + } + + @Override + protected FriendshipStatus doInBackground(Integer... integers) { + return new FriendsListHelper(getContext()).getFrienshipStatus(integers[0]); + } + + } + + /** + * Class used to update friendship status + */ + private static class UpdateStatusClass extends SafeAsyncTask{ + + private int mFriendID; + private int mAction; + + /** + * Construct the class + * + * @param friendID The ID of the target friend + * @param action The action to perform + * @param context The context of the application + */ + UpdateStatusClass(int friendID, int action, Context context) { + super(context); + this.mFriendID = friendID; + this.mAction = action; + } + + @Override + protected FriendshipStatus doInBackground(Void... voids) { + + //Friends helper + FriendsListHelper helper = new FriendsListHelper(getContext()); + performUpdate(mAction, mFriendID, helper); + return helper.getFrienshipStatus(mFriendID); + } + + /** + * 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 + * @param friend_id The ID of the target friend + * @param helper Helper object to use + */ + private void performUpdate(int action_id, int friend_id, FriendsListHelper helper){ + + switch (action_id){ + + //Send a friendship request + case R.id.button_send_request: + helper.sendRequest(friend_id); + break; + + //Accept a friendship request + case R.id.button_accept_request: + helper.respondRequest(friend_id, true); + break; + + //Reject a friendship request + case R.id.button_reject_request: + helper.respondRequest(friend_id, false); + break; + + //Cancel a friendship request + case R.id.button_cancel_request: + helper.cancelRequest(friend_id); + break; + + //Update following status + case R.id.button_follow: + helper.setFollowing(friend_id, true); + break; + case R.id.button_following: + helper.setFollowing(friend_id, false); + break; + + default: + throw new RuntimeException("Unsupported action by updater!"); + } + + } + } +} diff --git a/app/src/main/res/layout/fragment_user_access_denied.xml b/app/src/main/res/layout/fragment_user_access_denied.xml index 1e23b1c..3e99bb1 100644 --- a/app/src/main/res/layout/fragment_user_access_denied.xml +++ b/app/src/main/res/layout/fragment_user_access_denied.xml @@ -7,90 +7,49 @@ + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintTop_toTopOf="parent"> + android:src="@drawable/default_account_image" /> + android:layout_gravity="center_vertical" + tools:text="User account name" /> - - - - -