Can display friendship status.

This commit is contained in:
Pierre 2018-04-12 09:09:28 +02:00
parent 84de50f406
commit d45bfbf284
11 changed files with 602 additions and 17 deletions

View File

@ -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;
}
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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
*/

View File

@ -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());
}
}
}

View File

@ -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<Integer, Void, UserInfo>(){
@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<Integer, Void, FriendshipStatus>(){
@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);
}
}
}

View File

@ -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;

View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Basic account information -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp">
<ImageView
android:id="@+id/user_account_image"
android:layout_width="@dimen/account_image_default_width"
android:layout_height="@dimen/account_image_default_height"
android:src="@drawable/default_account_image"
android:contentDescription="@string/user_image_description"
android:layout_marginEnd="10dp"/>
<TextView
android:id="@+id/user_account_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="User account name"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<!-- Available actions -->
<LinearLayout
android:id="@+id/buttons_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="0dp">
<Button
android:id="@+id/button_send_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_send_friend_request"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/button_cancel_request"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_cancel_friend_request"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_send_request" />
<Button
android:id="@+id/button_accept_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_accept_friend_request"
app:layout_constraintTop_toBottomOf="@+id/button_cancel_request"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button_reject_request"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_reject_friend_request"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_accept_request" />
</LinearLayout>
<!-- Account private notice -->
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="@string/notice_user_page_private"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@id/buttons_list"
android:layout_marginBottom="8dp" />
</android.support.constraint.ConstraintLayout>

View File

@ -7,7 +7,7 @@
<color name="holo_green_dark">#ff669900</color>
<color name="darker_gray">#aaa</color>
<color name="darker_darker_gray">#5b5b5b</color>
<color name="dark_blue">#303F9F</color>
<color name="dark_blue">#303f9f</color>
<!-- Conversation messages -->
<color name="conversation_user_messages_background">#3F51B5</color>

View File

@ -50,7 +50,7 @@
<string name="date_m">m</string>
<string name="date_s">s</string>
<string name="date_hours">date_hours</string>
<string name="err_get_user_info">Couldn\'t get user information !</string>
<string name="err_get_user_info">Could not get user information !</string>
<string name="fragment_conversation_err_load_message">Could not load messages!</string>
<string name="fragment_conversation_message_image">Conversation message image</string>
<string name="conversation_message_send">Send</string>
@ -166,4 +166,10 @@
<string name="notif_on_user_page">on %1$s\'s page</string>
<string name="action_delete_notification">Delete</string>
<string name="err_delete_notification">An error occurred while trying to delete the notification!</string>
<string name="notice_user_page_private">This account is private.</string>
<string name="button_send_friend_request">Send request</string>
<string name="button_cancel_friend_request">Cancel request</string>
<string name="button_accept_friend_request">Accept request</string>
<string name="button_reject_friend_request">Reject request</string>
<string name="err_get_friendship_status">An error occurred while retrieving friendship status!</string>
</resources>