From 2c29b49c24138cd81a94daa1f3df12d4687f27b9 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 31 Dec 2017 14:10:36 +0100 Subject: [PATCH] Conversations can be deleted --- .../ConversationsListHelper.java | 22 +++++++ .../fragments/ConversationsListFragment.java | 65 +++++++++++++------ app/src/main/res/values/strings.xml | 1 + 3 files changed, 68 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java index ef7a604..80fd159 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -155,6 +155,28 @@ public class ConversationsListHelper { return name; } + /** + * Delete a conversation specified by its ID + * + * @param convID The ID of the conversation to delete + * @return True in case of success / false else + */ + public boolean delete(int convID){ + + //Delete the conversation on the API + APIRequestParameters params = new APIRequestParameters(mContext, "conversations/delete"); + params.addParameter("conversationID", ""+convID); + + try { + new APIRequest().exec(params); + } catch (Exception e){ + return false; + } + + //Success + return true; + } + /** * Get online (download) the list of all the conversations * 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 a9c6e7e..618075a 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 @@ -111,23 +111,8 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O //Get progress bar wheel progressBar = view.findViewById(R.id.fragment_conversationslist_progressbar); - //Get the list of conversations - new AsyncTask>(){ - @Override - protected ArrayList doInBackground(Void... params) { - - //Get the list of conversations - ArrayList list = conversationsListHelper.get(); - process_conversations_list(list); - return list; - - } - - @Override - protected void onPostExecute(ArrayList list) { - display_conversations_list(list); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + //Refresh conversations list + refresh_conversations_list(); //Set the open conversation listener try { @@ -150,6 +135,34 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O .setSelectedNavigationItem(R.id.main_bottom_navigation_conversations); } + /** + * Refresh the list of conversations + */ + private void refresh_conversations_list(){ + + //Display loading wheel + progressBar.setVisibility(View.VISIBLE); + + //Get the list of conversations + new AsyncTask>(){ + @Override + protected ArrayList doInBackground(Void... params) { + + //Get the list of conversations + ArrayList list = conversationsListHelper.get(); + process_conversations_list(list); + return list; + + } + + @Override + protected void onPostExecute(ArrayList list) { + display_conversations_list(list); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + } + /** * Process the conversation list * @@ -157,7 +170,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O * * @param list The list of conversations */ - public void process_conversations_list(ArrayList list){ + private void process_conversations_list(ArrayList list){ //Check if got the list if(list == null){ @@ -361,7 +374,19 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O * * @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(); + private void delete_conversation(final int convID){ + new AsyncTask(){ + + @Override + protected Void doInBackground(Void... params) { + conversationsListHelper.delete(convID); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + refresh_conversations_list(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e93fb9e..2d71ee9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -72,4 +72,5 @@ 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 + Could not delete conversation !