From 39135ef637d821cb37104955b88e5a2d7d2c6adc Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 11 Apr 2018 16:29:18 +0200 Subject: [PATCH] Can delete notifications. --- .../data/helpers/NotificationsHelper.java | 23 ++++++ .../ui/fragments/NotificationsFragment.java | 72 ++++++++++++++++++- .../res/menu/menu_notification_actions.xml | 9 +++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/menu/menu_notification_actions.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/NotificationsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/NotificationsHelper.java index 13337bf..031dcec 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/NotificationsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/NotificationsHelper.java @@ -71,6 +71,29 @@ public class NotificationsHelper { } } + /** + * Mark a notification as seen + * + * @param notifID The ID of the target notification + * @return TRUE in case of success / FALSE else + */ + public boolean markSeen(int notifID){ + + //Perform a request on the server + APIRequestParameters params = new APIRequestParameters(mContext, "notifications/mark_seen"); + params.addInt("notifID", notifID); + + //Try to send the request to the server + try { + APIResponse response = new APIRequestHelper().exec(params); + return response.getResponse_code() == 200; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + /** * Intend to delete the entire list of notifications * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java index 5c8708a..5c5d6a4 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java @@ -7,9 +7,13 @@ import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; +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.ProgressBar; import android.widget.Toast; @@ -28,7 +32,7 @@ import org.communiquons.android.comunic.client.ui.adapters.NotificationsAdapter; * Created by pierre on 4/1/18. */ -public class NotificationsFragment extends Fragment { +public class NotificationsFragment extends Fragment implements View.OnCreateContextMenuListener { /** * Notifications helper @@ -244,5 +248,71 @@ public class NotificationsFragment extends Fragment { //Create notification adapter mNotificationsAdapter = new NotificationsAdapter(getActivity(), mNotificationsList); mNotificationsListView.setAdapter(mNotificationsAdapter); + + //Set context menu creator + mNotificationsListView.setOnCreateContextMenuListener(this); + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + + //Check the context menu is targeting the list view + if(v != mNotificationsListView) + return; + + //Inflate the menu + MenuInflater inflater = getActivity().getMenuInflater(); + inflater.inflate(R.menu.menu_notification_actions, menu); + } + + @Override + public boolean onContextItemSelected(MenuItem item) { + + //Fetch source item + AdapterView.AdapterContextMenuInfo src + = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); + + //Check if the action is to delete the notification + if(item.getItemId() == R.id.action_delete){ + deleteNotification(src.position); + return true; + } + + return super.onContextItemSelected(item); + } + + /** + * Delete the notification located at a specified position + * + * @param pos The position of the notification to delete + */ + private void deleteNotification(int pos){ + + //Get the ID of the notification + int notifID = mNotificationsList.get(pos).getId(); + + //Delete the notification from the list + mNotificationsList.remove(pos); + mNotificationsAdapter.notifyDataSetChanged(); + + //Delete the notification from the server + new AsyncTask(){ + + @Override + protected Boolean doInBackground(Integer... params) { + return mNotificationsHelper.markSeen(params[0]); + } + + @Override + protected void onPostExecute(Boolean aBoolean) { + if(getActivity() == null) + return; + + //Check for errors + if(!aBoolean) + Toast.makeText(getActivity(), R.string.err_delete_notification, + Toast.LENGTH_SHORT).show(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, notifID); } } diff --git a/app/src/main/res/menu/menu_notification_actions.xml b/app/src/main/res/menu/menu_notification_actions.xml new file mode 100644 index 0000000..a9b1d01 --- /dev/null +++ b/app/src/main/res/menu/menu_notification_actions.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 d47aa19..cb84942 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -164,4 +164,6 @@ posted a comment on his / her page on %1$s\'s page + Delete + An error occurred while trying to delete the notification!