From 95844e8ba2209e2b8bb9f8cc3f247ecdecdd6f1b Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 31 Dec 2017 12:10:29 +0100 Subject: [PATCH] Added delete conversation confirmation popup --- .../fragments/ConversationFragment.java | 10 ++- .../fragments/ConversationsListFragment.java | 71 +++++++++++++++++++ .../menu_fragment_conserationslist_item.xml | 7 ++ app/src/main/res/values/strings.xml | 8 ++- 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/menu/menu_fragment_conserationslist_item.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java index 3a11fea..d4ed38a 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java @@ -257,7 +257,7 @@ public class ConversationFragment extends Fragment new Thread(refreshRunnable).start(); //Update conversation title - getActivity().setTitle(R.string.fragement_conversation_title); + getActivity().setTitle(R.string.fragment_conversation_title); //Update the bottom navigation menu ((MainActivity) getActivity()) @@ -369,6 +369,14 @@ public class ConversationFragment extends Fragment */ private void onGotConversationInfos(ConversationsInfo infos){ + //Check for errors + if(infos == null){ + Toast.makeText(getActivity(), R.string.fragment_conversation_err_getconvinfos, + Toast.LENGTH_SHORT).show(); + return; + } + + //Save conversation informations conversationInfo = infos; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java index 4fe29cc..a9c6e7e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java @@ -1,14 +1,19 @@ package org.communiquons.android.comunic.client.fragments; import android.app.Activity; +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; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; @@ -265,10 +270,44 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O //Add click listener conversationsListView.setOnItemClickListener(this); + conversationsListView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + MenuInflater inflater = getActivity().getMenuInflater(); + inflater.inflate(R.menu.menu_fragment_conserationslist_item, menu); + } + }); + //Remove progress bar display_progress_bar(false); } + @Override + public boolean onContextItemSelected(MenuItem item) { + + //Fetch source item + AdapterView.AdapterContextMenuInfo src + = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); + + //Get conversation ID + int convID = convList.size() > src.position ? convList.get(src.position).getID() : -1; + + if(convID != -1) { + + //Check which action was chosen + switch (item.getItemId()) { + + case R.id.menu_fragment_conversationslist_item_delete: + confirmDeleteConversation(convID); + return true; + + } + + } + + return super.onContextItemSelected(item); + } + /** * Handles the click on a conversation to open it * @@ -293,4 +332,36 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O private void display_progress_bar(boolean show){ progressBar.setVisibility(show ? View.VISIBLE : View.GONE); } + + /** + * Display a popup window to ask user to confirm the deletion of a conversation + * + * @param convID The ID of the conversation to delete + */ + private void confirmDeleteConversation(final int convID){ + + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.popup_deleteconversation_title) + .setMessage(R.string.popup_deleteconversation_messsage) + .setNegativeButton(R.string.popup_deleteconversation_cancel, null) + + .setPositiveButton(R.string.popup_deleteconversation_confirm, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + delete_conversation(convID); + } + }) + + .show(); + + } + + /** + * Delete a conversation + * + * @param convID The ID of the conversation to delete + */ + private void delete_conversation(int convID){ + Toast.makeText(getActivity(), "Delete conversation: " + convID, Toast.LENGTH_SHORT).show(); + } } diff --git a/app/src/main/res/menu/menu_fragment_conserationslist_item.xml b/app/src/main/res/menu/menu_fragment_conserationslist_item.xml new file mode 100644 index 0000000..f60ce17 --- /dev/null +++ b/app/src/main/res/menu/menu_fragment_conserationslist_item.xml @@ -0,0 +1,7 @@ + + + + + \ 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 33d0e69..e93fb9e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -65,5 +65,11 @@ Friends Conversations About me - Title + Title + Could not get information about the conversation ! + Delete + Remove conversation + Are you sure do you want to remove this conversation ? All the messages you posted will be deleted. If you are the owner of the conversation, all the messages will be removed. + Delete + Cancel