From b89221ab94a3ab45dbb0334e0622e5de4536f8e7 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 6 Dec 2017 18:23:34 +0100 Subject: [PATCH] Friend can be delete and friendship request accepted --- .../data/friendsList/FriendsAdapter.java | 45 +++++++++++- .../client/data/friendsList/FriendsList.java | 47 ++++++++++-- .../data/friendsList/FriendsListDbHelper.java | 72 +++++++++++++++++-- .../client/fragments/FriendsListFragment.java | 60 ++++++++++++++-- .../fragment_friends_list_friend_item.xml | 39 +++++----- app/src/main/res/values/strings.xml | 2 + 6 files changed, 231 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java index 78c1fbe..5b497a9 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java @@ -7,6 +7,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; @@ -14,6 +15,7 @@ import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.fragments.FriendsListFragment; import java.util.ArrayList; @@ -26,20 +28,27 @@ import java.util.ArrayList; public class FriendsAdapter extends ArrayAdapter { + /** + * The fragment creating the adapter + */ + private FriendsListFragment mFLfragment; /** * Class constructor * + * @param friendsListFragment Friends list fragment object * @param context The context of execution of the application * @param friendsList The list of friends to display (with user information) */ - public FriendsAdapter(Activity context, ArrayList friendsList){ + public FriendsAdapter(FriendsListFragment friendsListFragment, + Activity context, ArrayList friendsList){ super(context, 0, friendsList); + mFLfragment = friendsListFragment; } @NonNull @Override - public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { View listItemView = convertView; //Check if the view has to be created @@ -71,7 +80,7 @@ public class FriendsAdapter extends ArrayAdapter { ); //Set the color - int status_color = 0; + int status_color; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { status_color = getContext().getResources().getColor(signed_in ? R.color.holo_green_dark : R.color.darker_gray, null); } @@ -80,6 +89,36 @@ public class FriendsAdapter extends ArrayAdapter { } statusView.setTextColor(status_color); + //Action button + Button action = listItemView.findViewById(R.id.fragment_friendslist_item_action); + + //Define the action of the accept request button + if(!friendUser.getFriend().isAccepted()){ + + //Update the button + action.setVisibility(View.VISIBLE); + action.setText(R.string.action_friends_accept_request); + + //Make the button lives + action.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + //Hide the view + v.setVisibility(View.GONE); + + mFLfragment.acceptRequest(position); + } + }); + + } + else { + + //Remove button actions and hide it + action.setVisibility(View.GONE); + action.setOnClickListener(null); + } + return listItemView; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java index a28c90a..51f1a51 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java @@ -1,7 +1,10 @@ package org.communiquons.android.comunic.client.data.friendsList; import android.content.Context; +import android.util.Log; +import org.communiquons.android.comunic.client.api.APIRequest; +import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.data.DatabaseHelper; /** @@ -15,6 +18,9 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper; public class FriendsList { + //Debug tag + private static final String TAG = "FriendsList"; + private FriendsListDbHelper fdbHelper; private Context mContext; @@ -35,11 +41,44 @@ public class FriendsList { * @param friend The friend to delete */ public void remove(Friend friend){ + try { + //Remove the friend online + APIRequestParameters delparams = new APIRequestParameters(mContext, "friends/remove"); + delparams.addParameter("friendID", ""+friend.getId()); + new APIRequest().exec(delparams); - //Remove the friend online - //TODO : Remove the friend form online + //Remove the friend from the local database + fdbHelper.delete_friend(friend); - //Remove the friend from the local database - //TODO : Remove the friend from the local database + } catch (Exception e){ + Log.e(TAG, "Couldn't delete friend !"); + e.printStackTrace(); + } + } + + /** + * Respond to a friendship request + * + * @param friend The friend to update + * @param accept The new status for the request + */ + public void respondRequest(Friend friend, boolean accept){ + try { + + //Perform a request to update the satus online + APIRequestParameters reqParams = new APIRequestParameters(mContext, + "friends/respondRequest"); + reqParams.addParameter("friendID", ""+friend.getId()); + reqParams.addParameter("accept", accept ? "true" : "false"); + new APIRequest().exec(reqParams); + + //Update the friend in the local database + friend.setAccepted(accept); + fdbHelper.update_friend(friend); + + } catch(Exception e){ + Log.e(TAG, "Couldn't respond to friendship request !"); + e.printStackTrace(); + } } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java index 8621e22..ff27c69 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListDbHelper.java @@ -143,11 +143,7 @@ public class FriendsListDbHelper { String nullColumnHack = null; //Set the values - ContentValues values = new ContentValues(); - values.put(FriendsListSchema.COLUMN_NAME_FRIEND_ID, friend.getId()); - values.put(FriendsListSchema.COLUMN_NAME_FRIEND_ACCEPTED, friend.isAccepted() ? 1 : 0); - values.put(FriendsListSchema.COLUMN_NAME_FRIEND_FOLLOWING, friend.isFollowing() ? 1 : 0); - values.put(FriendsListSchema.COLUMN_NAME_FRIEND_LAST_ACTIVITY, friend.getLast_activity()); + ContentValues values = createContentValue(friend); //Perform the query return db.insert(table_name, nullColumnHack, values) > -1; @@ -175,4 +171,70 @@ public class FriendsListDbHelper { return result; } + + /** + * Remove a friend from the list + * + * @param friend The friend to delete + */ + boolean delete_friend(Friend friend){ + + //Get access to the database + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + //Prepare the request + String table_name = FriendsListSchema.TABLE_NAME; + String whereClause = FriendsListSchema.COLUMN_NAME_FRIEND_ID + " = ?"; + String[] whereValues = {""+friend.getId()}; + + int result = db.delete(table_name, whereClause, whereValues); + + //Close access to the database + db.close(); + + return result > 0; + } + + /** + * Update a specified user in the database with specified information + * + * @param friend The friend to update on the databse + * @return The result of the operation + */ + boolean update_friend(Friend friend){ + + //Get access to the database + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + //Prepare the request + String table = FriendsListSchema.TABLE_NAME; + ContentValues values = createContentValue(friend); + String whereClause = FriendsListSchema.COLUMN_NAME_FRIEND_ID + " = ?"; + String[] whereArgs = {""+friend.getId()}; + + //Perform it + int result = db.update(table, values, whereClause, whereArgs); + + //Close access to the database + db.close(); + + return result > 0; + } + + /** + * Create a full content value based on user informations in order to make operation on + * the database easier + * + * @param friend The friend which will be turned into a contentvalue + * @return The generated content value + */ + private ContentValues createContentValue(Friend friend){ + ContentValues values = new ContentValues(); + values.put(FriendsListSchema.COLUMN_NAME_FRIEND_ID, friend.getId()); + values.put(FriendsListSchema.COLUMN_NAME_FRIEND_ACCEPTED, friend.isAccepted() ? 1 : 0); + values.put(FriendsListSchema.COLUMN_NAME_FRIEND_FOLLOWING, friend.isFollowing() ? 1 : 0); + values.put(FriendsListSchema.COLUMN_NAME_FRIEND_LAST_ACTIVITY, friend.getLast_activity()); + + return values; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java index 86822fe..84e108c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java @@ -4,9 +4,11 @@ import android.app.AlertDialog; import android.app.Fragment; import android.content.Context; import android.content.DialogInterface; +import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.ArrayMap; +import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.MenuInflater; @@ -39,6 +41,11 @@ import java.util.ArrayList; public class FriendsListFragment extends Fragment { + /** + * Debug tag + */ + private String TAG = "FriendsListFragment"; + /** * The root view of the fragment */ @@ -64,6 +71,11 @@ public class FriendsListFragment extends Fragment { */ FriendsList flist; + /** + * Friend adapter + */ + private FriendsAdapter fAdapter; + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @@ -156,9 +168,9 @@ public class FriendsListFragment extends Fragment { this.friendsList = friendsList; //Set the adapter - FriendsAdapter friendsAdapter = new FriendsAdapter(getActivity(), friendsList); + fAdapter = new FriendsAdapter(this, getActivity(), friendsList); ListView listView = rootView.findViewById(R.id.fragment_friendslist_listview); - listView.setAdapter(friendsAdapter); + listView.setAdapter(fAdapter); //Register the view for the context menu registerForContextMenu(listView); @@ -207,11 +219,24 @@ public class FriendsListFragment extends Fragment { @Override public void onClick(DialogInterface dialog, int which) { - //Delete the friend from the list - flist.remove(friendsList.get(pos).getFriend()); + //Get the friend to delete + final Friend toDelete = friendsList.get(pos).getFriend(); - //Refresh the current friend list - refresh_friend_list(); + //Apply new list version + friendsList.remove(pos); + fAdapter.notifyDataSetChanged(); + + //Remove the friend list on a parallel thread + new AsyncTask(){ + @Override + protected Void doInBackground(Integer[] params) { + + //Delete the friend from the list + flist.remove(toDelete); + + return null; + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pos); } }); builder.setNegativeButton(R.string.popup_deletefriend_button_cancel, null); @@ -220,6 +245,29 @@ public class FriendsListFragment extends Fragment { } + /** + * Accept friendship request + * + * @param pos The position of the friend accepting the request + */ + public void acceptRequest(int pos){ + + //Get the Friend object + Friend targetFriend = friendsList.get(pos).getFriend(); + + //Mark the friend as accepted + targetFriend.setAccepted(true); + + //Accept the request on a separate thread + new AsyncTask(){ + @Override + protected Void doInBackground(Friend... params) { + flist.respondRequest(params[0], true); + return null; + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend); + } + /** * Hide (or display) progress bar * diff --git a/app/src/main/res/layout/fragment_friends_list_friend_item.xml b/app/src/main/res/layout/fragment_friends_list_friend_item.xml index 5eee0e9..be33ae0 100644 --- a/app/src/main/res/layout/fragment_friends_list_friend_item.xml +++ b/app/src/main/res/layout/fragment_friends_list_friend_item.xml @@ -1,45 +1,52 @@ + android:orientation="horizontal" + android:paddingEnd="8dp" + android:paddingStart="8dp"> + android:layout_gravity="center" + android:contentDescription="@string/user_image_description" + android:src="@drawable/default_account_image" /> + android:layout_weight="1" + android:orientation="vertical" + android:paddingEnd="4dp" + android:paddingStart="8dp"> + android:textSize="16sp" + tools:text="Full user name" /> + android:textColor="@android:color/holo_green_dark" + tools:text="Online" /> + +