Ready to implement friend deletion

This commit is contained in:
Pierre 2017-11-19 15:36:37 +01:00
parent c6cbdc20f0
commit e8fd331c7d
4 changed files with 167 additions and 27 deletions

View File

@ -0,0 +1,45 @@
package org.communiquons.android.comunic.client.data.friendsList;
import android.content.Context;
import org.communiquons.android.comunic.client.data.DatabaseHelper;
/**
* Friends list functions
*
* Some of the functions specified here are utilities (such as delete a friend)
*
* @author Pierre HUBERT
* Created by pierre on 11/19/17.
*/
public class FriendsList {
private FriendsListDbHelper fdbHelper;
private Context mContext;
/**
* Public application constructor
*
* @param dbHelper Database helper
* @param mContext the context of the application
*/
public FriendsList(DatabaseHelper dbHelper, Context mContext){
this.fdbHelper = new FriendsListDbHelper(dbHelper);
this.mContext = mContext;
}
/**
* Remove a friend from the list
*
* @param friend The friend to delete
*/
public void remove(Friend friend){
//Remove the friend online
//TODO : Remove the friend form online
//Remove the friend from the local database
//TODO : Remove the friend from the local database
}
}

View File

@ -1,13 +1,19 @@
package org.communiquons.android.comunic.client.fragments;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.ArrayMap;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
@ -18,6 +24,7 @@ import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
import org.communiquons.android.comunic.client.data.friendsList.Friend;
import org.communiquons.android.comunic.client.data.friendsList.FriendUser;
import org.communiquons.android.comunic.client.data.friendsList.FriendsAdapter;
import org.communiquons.android.comunic.client.data.friendsList.FriendsList;
import org.communiquons.android.comunic.client.data.friendsList.FriendsUtils;
import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask;
@ -47,6 +54,16 @@ public class FriendsListFragment extends Fragment {
*/
DatabaseHelper mDbHelper;
/**
* The current list of friends
*/
ArrayList<FriendUser> friendsList;
/**
* Friend list operations object
*/
FriendsList flist;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@ -58,6 +75,9 @@ public class FriendsListFragment extends Fragment {
//Create database helper
mDbHelper = new DatabaseHelper(mContext);
//Create friendlist operation object
flist = new FriendsList(mDbHelper, mContext);
//Retain the fragment
//setRetainInstance(true);
@ -86,7 +106,7 @@ public class FriendsListFragment extends Fragment {
new GetFriendsListTask(mDbHelper){
@Override
protected void onPostExecute(ArrayList<Friend> friendsList) {
protected void onPostExecute(final ArrayList<Friend> friendsList) {
//Remote progress bar
display_progress_bar(false);
@ -97,9 +117,29 @@ public class FriendsListFragment extends Fragment {
Toast.LENGTH_LONG).show();
return;
}
else
//Update the friends list
update_friends_list(friendsList);
else {
new GetUsersInfos(mContext, mDbHelper).
getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() {
@Override
public void callback(ArrayMap<Integer, UserInfo> info) {
//Check for errors
if (info == null) {
Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos,
Toast.LENGTH_SHORT).show();
return;
}
//Merge the user informations list and friends List into FriendInfo list
ArrayList<FriendUser> friendsUserList = FriendsUtils.merge_friends_user_infos_list(
friendsList,
info
);
//Refresh friends list
apply_friends_list(friendsUserList);
}
});
}
}
}.execute();
@ -110,32 +150,73 @@ public class FriendsListFragment extends Fragment {
*
* @param friendsList The friends list to apply
*/
private void update_friends_list(final ArrayList<Friend> friendsList){
private void apply_friends_list(final ArrayList<FriendUser> friendsList){
//Get user informations
new GetUsersInfos(mContext, mDbHelper).
getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() {
@Override
public void callback(ArrayMap<Integer, UserInfo> info) {
//Check for errors
if(info == null){
Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos,
Toast.LENGTH_SHORT).show();
return;
}
//Save the list of friends
this.friendsList = friendsList;
//Merge the user informations list and friends List into FriendInfo list
ArrayList<FriendUser> friendsUserList = FriendsUtils.merge_friends_user_infos_list(
friendsList,
info
);
//Set the adapter
FriendsAdapter friendsAdapter = new FriendsAdapter(getActivity(), friendsList);
ListView listView = rootView.findViewById(R.id.fragment_friendslist_listview);
listView.setAdapter(friendsAdapter);
//Register the view for the context menu
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_fragment_friendslist_item, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//Get the friend position in the list
int friendPos = info.position;
switch (item.getItemId()){
case R.id.menu_fragment_friendslist_delete_friend:
delete_friend(friendPos);
return true;
}
//If it is not for us, it is for someone else
return super.onContextItemSelected(item);
}
/**
* Ask user to confirm friend deletion, the friend is specified by its position in the list
*
* @param pos the position of the friend to delete
*/
private void delete_friend(final int pos){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.popup_deletefriend_title)
.setMessage(R.string.popup_deletefriend_message);
builder.setPositiveButton(R.string.popup_deletefriend_button_confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Delete the friend from the list
flist.remove(friendsList.get(pos).getFriend());
//Refresh the current friend list
refresh_friend_list();
}
});
builder.setNegativeButton(R.string.popup_deletefriend_button_cancel, null);
builder.show();
//Set the adapter
FriendsAdapter friendsAdapter = new FriendsAdapter(getActivity(), friendsUserList);
ListView listView = rootView.findViewById(R.id.fragment_friendslist_listview);
listView.setAdapter(friendsAdapter);
}
});
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Delete the friend -->
<item
android:id="@+id/menu_fragment_friendslist_delete_friend"
android:title="@string/action_friends_delete" />
</menu>

View File

@ -20,4 +20,9 @@
<string name="fragment_friendslist_err_get_userinfos">Couldn\'t get information about your friends!</string>
<string name="user_status_online">Online</string>
<string name="user_status_offline">Offline</string>
<string name="action_friends_delete">Delete friend</string>
<string name="popup_deletefriend_title">Remove friend</string>
<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>
</resources>