mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Friend can be delete and friendship request accepted
This commit is contained in:
parent
e8fd331c7d
commit
b89221ab94
@ -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<FriendUser> {
|
||||
|
||||
/**
|
||||
* 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<FriendUser> friendsList){
|
||||
public FriendsAdapter(FriendsListFragment friendsListFragment,
|
||||
Activity context, ArrayList<FriendUser> 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<FriendUser> {
|
||||
);
|
||||
|
||||
//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<FriendUser> {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Integer, Void, Void>(){
|
||||
@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<Friend, Void, Void>(){
|
||||
@Override
|
||||
protected Void doInBackground(Friend... params) {
|
||||
flist.respondRequest(params[0], true);
|
||||
return null;
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide (or display) progress bar
|
||||
*
|
||||
|
@ -1,45 +1,52 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingStart="8dp"
|
||||
android:paddingEnd="8dp">
|
||||
android:orientation="horizontal"
|
||||
android:paddingEnd="8dp"
|
||||
android:paddingStart="8dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/fragment_friendslist_item_accountimage"
|
||||
android:layout_width="@dimen/account_image_default_width"
|
||||
android:layout_height="@dimen/account_image_default_height"
|
||||
android:src="@drawable/default_account_image"
|
||||
android:contentDescription="User account image"
|
||||
android:layout_gravity="center"/>
|
||||
android:layout_gravity="center"
|
||||
android:contentDescription="@string/user_image_description"
|
||||
android:src="@drawable/default_account_image" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingStart="8dp"
|
||||
android:paddingEnd="4dp"
|
||||
android:layout_gravity="center"
|
||||
>
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:paddingEnd="4dp"
|
||||
android:paddingStart="8dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/fragment_friendslist_item_fullname"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="Full user name"
|
||||
android:textSize="16sp"/>
|
||||
android:textSize="16sp"
|
||||
tools:text="Full user name" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/fragment_friendslist_item_status"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/user_status_offline"
|
||||
tools:text="Online"
|
||||
android:textColor="@android:color/holo_green_dark"/>
|
||||
android:textColor="@android:color/holo_green_dark"
|
||||
tools:text="Online" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- Action button on user -->
|
||||
<Button
|
||||
android:id="@+id/fragment_friendslist_item_action"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
tools:text="Action" />
|
||||
|
||||
</LinearLayout>
|
@ -25,4 +25,6 @@
|
||||
<string name="popup_deletefriend_message">Are you sure do you want to remove this friend from your friend list ? You will have to make a new friendship request to get this friend back !</string>
|
||||
<string name="popup_deletefriend_button_confirm">Delete</string>
|
||||
<string name="popup_deletefriend_button_cancel">Cancel</string>
|
||||
<string name="action_friends_accept_request">Accept</string>
|
||||
<string name="user_image_description">User Image Account</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user