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; package org.communiquons.android.comunic.client.fragments;
import android.app.AlertDialog;
import android.app.Fragment; import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; 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.Friend;
import org.communiquons.android.comunic.client.data.friendsList.FriendUser; 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.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.FriendsUtils;
import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask; import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask;
@ -47,6 +54,16 @@ public class FriendsListFragment extends Fragment {
*/ */
DatabaseHelper mDbHelper; DatabaseHelper mDbHelper;
/**
* The current list of friends
*/
ArrayList<FriendUser> friendsList;
/**
* Friend list operations object
*/
FriendsList flist;
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@ -58,6 +75,9 @@ public class FriendsListFragment extends Fragment {
//Create database helper //Create database helper
mDbHelper = new DatabaseHelper(mContext); mDbHelper = new DatabaseHelper(mContext);
//Create friendlist operation object
flist = new FriendsList(mDbHelper, mContext);
//Retain the fragment //Retain the fragment
//setRetainInstance(true); //setRetainInstance(true);
@ -86,7 +106,7 @@ public class FriendsListFragment extends Fragment {
new GetFriendsListTask(mDbHelper){ new GetFriendsListTask(mDbHelper){
@Override @Override
protected void onPostExecute(ArrayList<Friend> friendsList) { protected void onPostExecute(final ArrayList<Friend> friendsList) {
//Remote progress bar //Remote progress bar
display_progress_bar(false); display_progress_bar(false);
@ -97,28 +117,13 @@ public class FriendsListFragment extends Fragment {
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
return; return;
} }
else else {
//Update the friends list
update_friends_list(friendsList);
}
}.execute();
}
/**
* Set the the list of friend attached to the list adapter
*
* @param friendsList The friends list to apply
*/
private void update_friends_list(final ArrayList<Friend> friendsList){
//Get user informations
new GetUsersInfos(mContext, mDbHelper). new GetUsersInfos(mContext, mDbHelper).
getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() { getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() {
@Override @Override
public void callback(ArrayMap<Integer, UserInfo> info) { public void callback(ArrayMap<Integer, UserInfo> info) {
//Check for errors //Check for errors
if(info == null){ if (info == null) {
Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos, Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
return; return;
@ -130,12 +135,88 @@ public class FriendsListFragment extends Fragment {
info info
); );
//Set the adapter //Refresh friends list
FriendsAdapter friendsAdapter = new FriendsAdapter(getActivity(), friendsUserList); apply_friends_list(friendsUserList);
ListView listView = rootView.findViewById(R.id.fragment_friendslist_listview);
listView.setAdapter(friendsAdapter);
} }
}); });
}
}
}.execute();
}
/**
* Set the the list of friend attached to the list adapter
*
* @param friendsList The friends list to apply
*/
private void apply_friends_list(final ArrayList<FriendUser> friendsList){
//Save the list of friends
this.friendsList = friendsList;
//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();
} }

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="fragment_friendslist_err_get_userinfos">Couldn\'t get information about your friends!</string>
<string name="user_status_online">Online</string> <string name="user_status_online">Online</string>
<string name="user_status_offline">Offline</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> </resources>