From d45bfbf28470f28e2cfb1f757a052019b7b72bfb Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 12 Apr 2018 09:09:28 +0200 Subject: [PATCH] Can display friendship status. --- .../data/helpers/FriendsListHelper.java | 42 ++- .../client/data/helpers/GetUsersHelper.java | 51 +++- .../client/data/models/AdvancedUserInfo.java | 19 ++ .../client/data/models/FriendshipStatus.java | 56 ++++ .../client/ui/activities/MainActivity.java | 31 ++ .../ui/fragments/NotificationsFragment.java | 32 ++- .../fragments/UserAccessDeniedFragment.java | 266 ++++++++++++++++++ .../client/ui/fragments/UserPageFragment.java | 16 ++ .../layout/fragment_user_access_denied.xml | 96 +++++++ app/src/main/res/values/colors.xml | 2 +- app/src/main/res/values/strings.xml | 8 +- 11 files changed, 602 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/models/FriendshipStatus.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserAccessDeniedFragment.java create mode 100644 app/src/main/res/layout/fragment_user_access_denied.xml 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 a5a4b82..825ffc2 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 @@ -7,6 +7,7 @@ import android.util.Log; import org.communiquons.android.comunic.client.data.models.APIRequestParameters; import org.communiquons.android.comunic.client.data.models.APIResponse; import org.communiquons.android.comunic.client.data.models.Friend; +import org.communiquons.android.comunic.client.data.models.FriendshipStatus; import org.json.JSONArray; import org.json.JSONObject; @@ -142,7 +143,7 @@ public class FriendsListHelper { public void respondRequest(Friend friend, boolean accept){ try { - //Perform a request to update the satus online + //Perform a request to update the status online APIRequestParameters reqParams = new APIRequestParameters(mContext, "friends/respondRequest"); reqParams.addInt("friendID", friend.getId()); @@ -163,4 +164,43 @@ public class FriendsListHelper { e.printStackTrace(); } } + + /** + * Get a friendship status + * + * @param friendID The ID of the target friend + * @return Information about the friendship / null in case of failure + */ + @Nullable + public FriendshipStatus getFrienshipStatus(int friendID) { + + //Perform a request on the API + APIRequestParameters params = new APIRequestParameters(mContext, "friends/getStatus"); + params.addInt("friendID", friendID); + + try { + + //Get the response + APIResponse response = new APIRequestHelper().exec(params); + + //Check for errors + if(response.getResponse_code() != 200) + return null; + + //Parse the response + JSONObject object = response.getJSONObject(); + FriendshipStatus status = new FriendshipStatus(); + status.setAreFriend(object.getBoolean("are_friend")); + status.setSentRequest(object.getBoolean("sent_request")); + status.setReceivedRequest(object.getBoolean("received_request")); + status.setFollowing(object.getBoolean("following")); + + return status; + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java index f825d67..c8d2cef 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java @@ -64,7 +64,7 @@ public class GetUsersHelper { * Public constructor of the class * * @param context The context of execution of the application - * @param dbHelper Databasehelpepr + * @param dbHelper DatabaseHelper */ public GetUsersHelper(@NonNull Context context, @NonNull DatabaseHelper dbHelper){ mContext = context; @@ -74,8 +74,8 @@ public class GetUsersHelper { /** * Get information about a single user from the server * - * @param id The ID of the user to get informations on - * @param force Force the informations to be fetched from the server + * @param id The ID of the user to get information on + * @param force Force the information to be fetched from the server * @return User information or null in case of failure */ @Nullable @@ -103,7 +103,7 @@ public class GetUsersHelper { //Add the user to the database udbHelper.insertOrUpdate(infos); - //Return user informations + //Return user information return infos; } @@ -118,10 +118,10 @@ public class GetUsersHelper { } /** - * Get advanced informations about a single user + * Get advanced information about a single user * * @param userID The user to get information about - * @return Informations about the user / null in case of failure + * @return Information about the user / null in case of failure */ @Nullable public AdvancedUserInfo get_advanced_infos(int userID){ @@ -129,13 +129,31 @@ public class GetUsersHelper { //Perform an API request APIRequestParameters params = new APIRequestParameters(mContext, "user/getAdvancedUserInfos"); + params.setTryContinueOnError(true); params.addInt("userID", userID); //Perform the request try { APIResponse response = new APIRequestHelper().exec(params); - //Parse user informations + //Check if the request echoed because the user is not allowed to access it + if(response.getResponse_code() != 200){ + + if(response.getResponse_code() == 401){ + + //Return an empty AdvancedUserInfo object with access forbidden set to true + AdvancedUserInfo info = new AdvancedUserInfo(); + info.setAccessForbidden(true); + return info; + + } + + //Else we can not do anything + return null; + + } + + //Parse user information return parse_advanced_user_json(response.getJSONObject()); } catch (Exception e) { @@ -368,7 +386,7 @@ public class GetUsersHelper { @Nullable private AdvancedUserInfo parse_advanced_user_json(JSONObject userObject){ - //Parse basic informations about the user + //Parse basic information about the user AdvancedUserInfo advancedUserInfo = (AdvancedUserInfo) parse_base_user_json(new AdvancedUserInfo(), userObject); @@ -376,7 +394,7 @@ public class GetUsersHelper { if(advancedUserInfo == null) return null; - //Parse advanced user informations + //Parse advanced user information try { //Get account creation time @@ -393,19 +411,19 @@ public class GetUsersHelper { } /** - * Parse user basic informations into a user object + * Parse user basic information into a user object * - * @param userInfos The user informations object to fill + * @param userInfos The user information object to fill * @param userObject The source JSON object * @return The filled user Infos object */ @Nullable private UserInfo parse_base_user_json(UserInfo userInfos, JSONObject userObject){ - //Try to retrieve basic user informations + //Try to retrieve basic user information try { - //Retrieve all user informations + //Retrieve all user information userInfos.setId(userObject.getInt("userID")); userInfos.setFirstName(userObject.getString("firstName")); userInfos.setLastName(userObject.getString("lastName")); @@ -432,5 +450,12 @@ public class GetUsersHelper { * @param userID The ID of the user to create page */ void openUserPage(int userID); + + /** + * Open the page of a user for which the access has been denied + * + * @param userID The ID of the target user + */ + void openUserAccessDeniedPage(int userID); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java index 1a9de91..0a0c3a0 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedUserInfo.java @@ -11,6 +11,7 @@ public class AdvancedUserInfo extends UserInfo { //Private fields private int account_creation_time; + private boolean accessForbidden = false; /** * Get the account creation time @@ -29,4 +30,22 @@ public class AdvancedUserInfo extends UserInfo { public void setAccount_creation_time(int account_creation_time) { this.account_creation_time = account_creation_time; } + + /** + * Check whether the access to the page is forbidden or not + * + * @return TRUE if the access to the page is forbidden / FALSE else + */ + public boolean isAccessForbidden() { + return accessForbidden; + } + + /** + * Set the forbidden state of the page + * + * @param accessForbidden TRUE if the access to the page is forbidden / FALSE else + */ + public void setAccessForbidden(boolean accessForbidden) { + this.accessForbidden = accessForbidden; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/FriendshipStatus.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/FriendshipStatus.java new file mode 100644 index 0000000..3805dd7 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/FriendshipStatus.java @@ -0,0 +1,56 @@ +package org.communiquons.android.comunic.client.data.models; + +/** + * This models handles a friendship status + * + * @author Pierre HUBERT + * Created by pierre on 4/12/18. + */ + +public class FriendshipStatus { + + //Private fields + private boolean isFriend; + private boolean sentRequest; + private boolean receivedRequest; + private boolean following; + + + //Set and get the "are friend" value + public void setAreFriend(boolean areFriend) { + this.isFriend = areFriend; + } + + public boolean isFriend() { + return isFriend; + } + + + //Set and get the sent request status + public void setSentRequest(boolean sentRequest) { + this.sentRequest = sentRequest; + } + + public boolean isSentRequest() { + return sentRequest; + } + + + //Set and get the received request status + public void setReceivedRequest(boolean receivedRequest) { + this.receivedRequest = receivedRequest; + } + + public boolean isReceivedRequest() { + return receivedRequest; + } + + //Set and get the following status + public void setFollowing(boolean following) { + this.following = following; + } + + public boolean isFollowing() { + return following; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index dc95be9..b151e12 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -24,6 +24,7 @@ import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; import org.communiquons.android.comunic.client.data.runnables.FriendRefreshLoopRunnable; import org.communiquons.android.comunic.client.data.services.NotificationsService; +import org.communiquons.android.comunic.client.ui.fragments.UserAccessDeniedFragment; import org.communiquons.android.comunic.client.ui.utils.UiUtils; import org.communiquons.android.comunic.client.ui.fragments.ConversationFragment; import org.communiquons.android.comunic.client.ui.fragments.ConversationsListFragment; @@ -339,6 +340,36 @@ public class MainActivity extends AppCompatActivity } + /** + * Open the page of a user for which the access has been denied + * + * @param userID The ID of the target user + */ + @Override + public void openUserAccessDeniedPage(int userID) { + + //Prepare the argument + Bundle args = new Bundle(); + args.putInt(UserAccessDeniedFragment.ARGUMENT_USER_ID, userID); + + //Create fragment + UserAccessDeniedFragment userAccessDeniedFragment = new UserAccessDeniedFragment(); + userAccessDeniedFragment.setArguments(args); + + //Remove the last entry of the backstack + //This is important in order to avoid to get the user unable to quit the page. + //Because it would get the user back to the user page fragment which would + //redirect immediately to this fragment indefinitely. + getFragmentManager().popBackStackImmediate(); + + //Perform the transition + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.addToBackStack(null); + transaction.replace(R.id.main_fragment, userAccessDeniedFragment); + transaction.commit(); + + } + /** * Open the conversation list fragment */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java index 5c5d6a4..92ad9fa 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java @@ -19,9 +19,11 @@ import android.widget.ProgressBar; import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.enums.NotifElemType; import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; import org.communiquons.android.comunic.client.data.helpers.NotificationsHelper; import org.communiquons.android.comunic.client.data.arrays.NotifsList; +import org.communiquons.android.comunic.client.data.models.Notif; import org.communiquons.android.comunic.client.ui.activities.MainActivity; import org.communiquons.android.comunic.client.ui.adapters.NotificationsAdapter; @@ -32,7 +34,8 @@ import org.communiquons.android.comunic.client.ui.adapters.NotificationsAdapter; * Created by pierre on 4/1/18. */ -public class NotificationsFragment extends Fragment implements View.OnCreateContextMenuListener { +public class NotificationsFragment extends Fragment implements View.OnCreateContextMenuListener, + AdapterView.OnItemClickListener { /** * Notifications helper @@ -69,6 +72,10 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont */ private ProgressBar mLoadingProgress; + /** + * User page opener + */ + private GetUsersHelper.onOpenUsersPageListener mUserPageOpener; @Override public void onAttach(Context context) { @@ -118,6 +125,9 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont ((MainActivity) getActivity()) .setSelectedNavigationItem(R.id.main_bottom_navigation_notif); + //Get user page opener + mUserPageOpener = (GetUsersHelper.onOpenUsersPageListener) getActivity(); + //Check if it is required to fetch the list of notifications if(mNotificationsList == null){ @@ -251,6 +261,7 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont //Set context menu creator mNotificationsListView.setOnCreateContextMenuListener(this); + mNotificationsListView.setOnItemClickListener(this); } @Override @@ -315,4 +326,23 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, notifID); } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + + //Delete the notification + //deleteNotification(position); + + //Perform notification action + Notif notif = mNotificationsList.get(position); + + //For friendship request + if(notif.getOn_elem_type() == NotifElemType.FRIEND_REQUEST){ + + //Open user page + mUserPageOpener.openUserPage(notif.getFrom_user_id()); + + } + + } } 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 new file mode 100644 index 0000000..951c0b9 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserAccessDeniedFragment.java @@ -0,0 +1,266 @@ +package org.communiquons.android.comunic.client.ui.fragments; + +import android.app.Fragment; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +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.helpers.ImageLoadHelper; +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.activities.MainActivity; + +/** + * User access denied fragment + * + * @author Pierre HUBERT + * Created by pierre on 4/11/18. + */ + +public class UserAccessDeniedFragment extends Fragment { + + /** + * The name in the bundle of the target user ID + */ + public static final String ARGUMENT_USER_ID = "user_id"; + + /** + * The ID of the target user + */ + private int mUserID; + + /** + * Get user helper + */ + private GetUsersHelper mUserHelper; + + /** + * Friend list helper + */ + private FriendsListHelper mFriendListHelper; + + /** + * Information about the user + */ + private UserInfo mUserInfo; + + /** + * Information about the friendship status + */ + private FriendshipStatus mFriendshipStatus; + + /** + * User account image + */ + private ImageView mUserImage; + + /** + * User account name + */ + private TextView mUserName; + + /** + * Send request button + */ + private Button mSendRequestButton; + + /** + * Cancel request button + */ + private Button mCancelRequestButton; + + /** + * Accept request button + */ + private Button mAcceptRequestButton; + + /** + * Reject request button + */ + private Button mRejectRequestButton; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Create get user helper + mUserHelper = new GetUsersHelper(getActivity()); + + //Create friend info helper + mFriendListHelper = new FriendsListHelper(getActivity()); + + //Save the ID of the target user + mUserID = getArguments().getInt(ARGUMENT_USER_ID); + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_user_access_denied, container, false); + } + + @Override + public void onViewCreated(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); + + //Get the buttons + 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); + } + + @Override + public void onResume() { + super.onResume(); + + //Update activity dock + ((MainActivity) getActivity()) + .setSelectedNavigationItem(R.id.main_bottom_navigation_me_view); + + //Check if it is required to fetch user information + if(mUserInfo == null){ + getUserInfo(); + } + else + onGotUserInfos(mUserInfo); + } + + /** + * This method get user information. Once it got it, it shows these information on the screen + */ + private void getUserInfo(){ + + //Perform the task in the background + new AsyncTask(){ + + @Override + protected UserInfo doInBackground(Integer... params) { + //Force information about the user to be pulled + return mUserHelper.getSingle(mUserID, true); + } + + @Override + protected void onPostExecute(@Nullable UserInfo userInfo) { + if(getActivity() == null) + return; + + onGotUserInfos(userInfo); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mUserID); + } + + /** + * This method is called once we got information about the user + * + * @param info Information about the user / null in case of failure + */ + private void onGotUserInfos(@Nullable UserInfo info){ + + //Check for error + if(info == null){ + Toast.makeText(getActivity(), R.string.err_get_user_info, Toast.LENGTH_SHORT).show(); + return; + } + + //Save user information + mUserInfo = info; + + //Update activity name + getActivity().setTitle(mUserInfo.getDisplayFullName()); + + //Append user information + mUserName.setText(mUserInfo.getDisplayFullName()); + ImageLoadHelper.load(getActivity(), mUserInfo.getAcountImageURL(), mUserImage); + + //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 + 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()) + 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); + } + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java index 4f886b5..5ebb284 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UserPageFragment.java @@ -107,6 +107,11 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen */ private View mCreatePostForm; + /** + * User page open listener + */ + private GetUsersHelper.onOpenUsersPageListener mOpenUsersPageListener; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -123,6 +128,9 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen //Create posts helper instance mPostsHelper = new PostsHelper(getActivity()); + + //Get the open user page listener + mOpenUsersPageListener = (GetUsersHelper.onOpenUsersPageListener) getActivity(); } @Nullable @@ -215,6 +223,14 @@ public class UserPageFragment extends Fragment implements PostsCreateFormFragmen return; } + //Check if the user is not allowed to access user information + if(info.isAccessForbidden()){ + //Open appropriate fragment + mOpenUsersPageListener.openUserAccessDeniedPage(mUserID); + return; + } + + //Save user information userInfo = info; diff --git a/app/src/main/res/layout/fragment_user_access_denied.xml b/app/src/main/res/layout/fragment_user_access_denied.xml new file mode 100644 index 0000000..f39625f --- /dev/null +++ b/app/src/main/res/layout/fragment_user_access_denied.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + +