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 new file mode 100644 index 0000000..a28c90a --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java @@ -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 + } +} 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 751cad5..86822fe 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 @@ -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 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 friendsList) { + protected void onPostExecute(final ArrayList 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 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 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 friendsList){ + private void apply_friends_list(final ArrayList friendsList){ - //Get user informations - new GetUsersInfos(mContext, mDbHelper). - getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() { - @Override - public void callback(ArrayMap 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 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); - } - }); } diff --git a/app/src/main/res/menu/menu_fragment_friendslist_item.xml b/app/src/main/res/menu/menu_fragment_friendslist_item.xml new file mode 100644 index 0000000..0cec786 --- /dev/null +++ b/app/src/main/res/menu/menu_fragment_friendslist_item.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d79dd0..e723c23 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,4 +20,9 @@ Couldn\'t get information about your friends! Online Offline + Delete friend + Remove friend + 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 ! + Delete + Cancel