From 79fa8efcfcdfeed28330f78517bc5abe1efe3823 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sun, 30 Jul 2017 11:21:55 +0200 Subject: [PATCH] Allows to remove notifications --- .../PostNotificationsAsyncTask.java | 58 ++++++++ .../fr/gouv/etalab/mastodon/client/API.java | 34 +++++ .../drawers/NotificationsListAdapter.java | 131 +++++++++++++----- .../mastodon/drawers/StatusListAdapter.java | 1 - .../mastodon/drawers/TagsListAdapter.java | 2 +- .../OnPostNotificationsActionInterface.java | 27 ++++ app/src/main/res/drawable-hdpi/ic_delete.png | Bin 0 -> 160 bytes app/src/main/res/drawable-ldpi/ic_delete.png | Bin 0 -> 220 bytes app/src/main/res/drawable-mdpi/ic_delete.png | Bin 0 -> 131 bytes app/src/main/res/drawable-xhdpi/ic_delete.png | Bin 0 -> 160 bytes .../main/res/drawable-xxhdpi/ic_delete.png | Bin 0 -> 213 bytes .../main/res/drawable-xxxhdpi/ic_delete.png | Bin 0 -> 201 bytes .../main/res/layout/drawer_notification.xml | 30 +++- app/src/main/res/values-fr/strings.xml | 4 + app/src/main/res/values/strings.xml | 4 + 15 files changed, 251 insertions(+), 40 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostNotificationsAsyncTask.java create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPostNotificationsActionInterface.java create mode 100644 app/src/main/res/drawable-hdpi/ic_delete.png create mode 100644 app/src/main/res/drawable-ldpi/ic_delete.png create mode 100644 app/src/main/res/drawable-mdpi/ic_delete.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_delete.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_delete.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_delete.png diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostNotificationsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostNotificationsAsyncTask.java new file mode 100644 index 000000000..4a3f0d90d --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostNotificationsAsyncTask.java @@ -0,0 +1,58 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Thomas Schneider; if not, + * see . */ +package fr.gouv.etalab.mastodon.asynctasks; + +import android.content.Context; +import android.os.AsyncTask; + +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.client.Entities.Notification; +import fr.gouv.etalab.mastodon.interfaces.OnPostNotificationsActionInterface; + + +/** + * Created by Thomas on 29/07/2017. + * Posts to delete one or all notifications + */ + +public class PostNotificationsAsyncTask extends AsyncTask { + + private Context context; + private OnPostNotificationsActionInterface listener; + private APIResponse apiResponse; + private Notification notification; + + public PostNotificationsAsyncTask(Context context, Notification notification, OnPostNotificationsActionInterface onPostNotificationsActionInterface){ + this.context = context; + this.listener = onPostNotificationsActionInterface; + this.notification = notification; + } + + @Override + protected Void doInBackground(Void... params) { + if( notification != null) + apiResponse = new API(context).postNoticationAction(notification.getId()); + else //Delete all notifications + apiResponse = new API(context).postNoticationAction(null); + return null; + } + + @Override + protected void onPostExecute(Void result) { + listener.onPostNotificationsAction(apiResponse, notification); + } + +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index ed7d211ad..c3bc8f6a1 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -950,6 +950,40 @@ public class API { } + /** + * Posts a status + * @param notificationId String, the current notification id, if null all notifications are deleted + * @return APIResponse + */ + public APIResponse postNoticationAction(String notificationId){ + + String action; + RequestParams requestParams = new RequestParams(); + if( notificationId == null) + action = "/notifications/clear"; + else { + requestParams.add("id",notificationId); + action = "/notifications/dismiss"; + } + post(action, 30000, requestParams, new JsonHttpResponseHandler() { + @Override + public void onSuccess(int statusCode, Header[] headers, JSONObject response) { + } + + @Override + public void onSuccess(int statusCode, Header[] headers, JSONArray response) { + } + + @Override + public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response) { + setError(statusCode, error); + error.printStackTrace(); + } + }); + return apiResponse; + } + + /** * Retrieves notifications for the authenticated account since an id*synchronously* * @param since_id String since max diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java index 54e5b5657..b0a8a2d4d 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java @@ -38,15 +38,19 @@ import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer; +import java.util.ArrayList; import java.util.List; import fr.gouv.etalab.mastodon.activities.ShowAccountActivity; import fr.gouv.etalab.mastodon.activities.ShowConversationActivity; import fr.gouv.etalab.mastodon.activities.TootActivity; import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask; +import fr.gouv.etalab.mastodon.asynctasks.PostNotificationsAsyncTask; import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Error; import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; +import fr.gouv.etalab.mastodon.interfaces.OnPostNotificationsActionInterface; import mastodon.etalab.gouv.fr.mastodon.R; import fr.gouv.etalab.mastodon.client.Entities.Notification; import fr.gouv.etalab.mastodon.client.Entities.Status; @@ -59,7 +63,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; * Created by Thomas on 24/04/2017. * Adapter for Status */ -public class NotificationsListAdapter extends BaseAdapter implements OnPostActionInterface { +public class NotificationsListAdapter extends BaseAdapter implements OnPostActionInterface, OnPostNotificationsActionInterface { private Context context; private List notifications; @@ -118,6 +122,7 @@ public class NotificationsListAdapter extends BaseAdapter implements OnPostActio holder.status_date = (TextView) convertView.findViewById(R.id.status_date); holder.status_reply = (ImageView) convertView.findViewById(R.id.status_reply); holder.status_privacy = (ImageView) convertView.findViewById(R.id.status_privacy); + holder.notification_delete = (ImageView) convertView.findViewById(R.id.notification_delete); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); @@ -143,6 +148,36 @@ public class NotificationsListAdapter extends BaseAdapter implements OnPostActio final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + //Manages theme for icon colors + int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); + if( theme == Helper.THEME_DARK){ + changeDrawableColor(context, R.drawable.ic_reply,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_action_more,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_action_globe,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_action_lock_open,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_action_lock_closed,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_local_post_office,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_retweet_black,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_retweet,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_fav_black,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_photo,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_remove_red_eye,R.color.dark_text); + changeDrawableColor(context, R.drawable.ic_delete,R.color.dark_text); + }else { + changeDrawableColor(context, R.drawable.ic_reply,R.color.black); + changeDrawableColor(context, R.drawable.ic_action_more,R.color.black); + changeDrawableColor(context, R.drawable.ic_action_globe,R.color.black); + changeDrawableColor(context, R.drawable.ic_action_lock_open,R.color.black); + changeDrawableColor(context, R.drawable.ic_action_lock_closed,R.color.black); + changeDrawableColor(context, R.drawable.ic_local_post_office,R.color.black); + changeDrawableColor(context, R.drawable.ic_retweet_black,R.color.black); + changeDrawableColor(context, R.drawable.ic_retweet,R.color.black); + changeDrawableColor(context, R.drawable.ic_fav_black,R.color.black); + changeDrawableColor(context, R.drawable.ic_photo,R.color.black); + changeDrawableColor(context, R.drawable.ic_remove_red_eye,R.color.black); + changeDrawableColor(context, R.drawable.ic_delete,R.color.black); + } + final Status status = notification.getStatus(); if( status != null ){ if( status.getMedia_attachments() == null || status.getMedia_attachments().size() < 1) @@ -167,35 +202,6 @@ public class NotificationsListAdapter extends BaseAdapter implements OnPostActio holder.status_reblog_count.setText(String.valueOf(status.getReblogs_count())); holder.status_date.setText(Helper.dateDiff(context, status.getCreated_at())); - //Manages theme for icon colors - - int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); - if( theme == Helper.THEME_DARK){ - changeDrawableColor(context, R.drawable.ic_reply,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_action_more,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_action_globe,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_action_lock_open,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_action_lock_closed,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_local_post_office,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_retweet_black,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_retweet,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_fav_black,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_photo,R.color.dark_text); - changeDrawableColor(context, R.drawable.ic_remove_red_eye,R.color.dark_text); - }else { - changeDrawableColor(context, R.drawable.ic_reply,R.color.black); - changeDrawableColor(context, R.drawable.ic_action_more,R.color.black); - changeDrawableColor(context, R.drawable.ic_action_globe,R.color.black); - changeDrawableColor(context, R.drawable.ic_action_lock_open,R.color.black); - changeDrawableColor(context, R.drawable.ic_action_lock_closed,R.color.black); - changeDrawableColor(context, R.drawable.ic_local_post_office,R.color.black); - changeDrawableColor(context, R.drawable.ic_retweet_black,R.color.black); - changeDrawableColor(context, R.drawable.ic_retweet,R.color.black); - changeDrawableColor(context, R.drawable.ic_fav_black,R.color.black); - changeDrawableColor(context, R.drawable.ic_photo,R.color.black); - changeDrawableColor(context, R.drawable.ic_remove_red_eye,R.color.black); - } - //Adds attachment -> disabled, to enable them uncomment the line below //loadAttachments(status, holder); holder.notification_status_container.setVisibility(View.VISIBLE); @@ -300,7 +306,12 @@ public class NotificationsListAdapter extends BaseAdapter implements OnPostActio } }); - + holder.notification_delete.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + displayConfirmationNotificationDialog(notification); + } + }); holder.notification_account_displayname.setText(Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true)); holder.notification_account_username.setText( String.format("@%s",notification.getAccount().getUsername())); //Profile picture @@ -356,6 +367,44 @@ public class NotificationsListAdapter extends BaseAdapter implements OnPostActio .show(); } + /** + * Display a validation message for notification deletion + * @param notification Notification + */ + private void displayConfirmationNotificationDialog(final Notification notification){ + final ArrayList seletedItems = new ArrayList(); + AlertDialog dialog = new AlertDialog.Builder(context) + .setTitle(R.string.delete_notification_ask) + .setMultiChoiceItems(new String[]{context.getString(R.string.delete_notification_ask_all)}, null, new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) { + if (isChecked) { + //noinspection unchecked + seletedItems.add(indexSelected); + } else { + if (seletedItems.contains(indexSelected)) + seletedItems.remove(Integer.valueOf(indexSelected)); + } + + } + }).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + if (seletedItems.size() > 0) + new PostNotificationsAsyncTask(context, null, NotificationsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + new PostNotificationsAsyncTask(context, notification, NotificationsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + dialog.dismiss(); + } + }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }).create(); + dialog.show(); + } + /** * Favourites/Unfavourites a status * @param status Status @@ -396,6 +445,25 @@ public class NotificationsListAdapter extends BaseAdapter implements OnPostActio } } + @Override + public void onPostNotificationsAction(APIResponse apiResponse, Notification notification) { + if(apiResponse.getError() != null){ + Toast.makeText(context, R.string.toast_error,Toast.LENGTH_LONG).show(); + return; + } + if( notification != null){ + notifications.remove(notification); + notificationsListAdapter.notifyDataSetChanged(); + Toast.makeText(context,R.string.delete_notification,Toast.LENGTH_LONG).show(); + }else{ + notifications.clear(); + notifications = new ArrayList<>(); + notificationsListAdapter.notifyDataSetChanged(); + Toast.makeText(context,R.string.delete_notification_all,Toast.LENGTH_LONG).show(); + } + + } + private class ViewHolder { TextView notification_status_content; @@ -403,6 +471,7 @@ public class NotificationsListAdapter extends BaseAdapter implements OnPostActio TextView notification_account_username; TextView notification_account_displayname; ImageView notification_account_profile; + ImageView notification_delete; TextView status_favorite_count; TextView status_reblog_count; TextView status_date; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index b2eecc25b..a61615199 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -74,7 +74,6 @@ import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.Entities.Attachment; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; - import static fr.gouv.etalab.mastodon.activities.MainActivity.currentLocale; import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/TagsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/TagsListAdapter.java index f8bc00aff..f0c3401c2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/TagsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/TagsListAdapter.java @@ -19,7 +19,7 @@ import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.BaseAdapter;; +import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPostNotificationsActionInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPostNotificationsActionInterface.java new file mode 100644 index 000000000..7c6ba311a --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPostNotificationsActionInterface.java @@ -0,0 +1,27 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Thomas Schneider; if not, + * see . */ +package fr.gouv.etalab.mastodon.interfaces; + + +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.client.Entities.Notification; + +/** + * Created by Thomas on 29/07/2017. + * Interface when deleting a notification + */ +public interface OnPostNotificationsActionInterface { + void onPostNotificationsAction(APIResponse apiResponse, Notification notification); +} diff --git a/app/src/main/res/drawable-hdpi/ic_delete.png b/app/src/main/res/drawable-hdpi/ic_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..f40c83bd947beb220949275c4bf91d42b0f39638 GIT binary patch literal 160 zcmeAS@N?(olHy`uVBq!ia0vp^(jd&i0wmS%+S~(D>7Fi*Ar-fh6C^e%v?emjEE5RI zIdFlY%~;~!;scTah0Jem30;`7_%O?xTQ(AW4_f7R%sF{*L4$D7gKu-0<=u2IZOZm literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-ldpi/ic_delete.png b/app/src/main/res/drawable-ldpi/ic_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..0b30f712d451035b4c279d3b01dd813b492bfc24 GIT binary patch literal 220 zcmV<203-j2P)Lh7cys z#j9bIR)PU0m}7~V=DzlSs);F1xZ#e6o|4bnZ`mP_F>+jR4bG$9=)*Up*yD+}QQ&}H z_(lm`Z1KX!keq4%uc2LSLs`|KG`8$E*1Dg}1CoJGKNV;*nfPv#X zV`M}F^Wl1(3yh%?nVgx$Ry5>=G2Ku~nbDA$$lR97ySQtEhSsrwkdBod^Aj1pD$XZA dSX{@&#Nhj^z|W0S-V$gkgQu&X%Q~loCIDNYD1ZO} literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_delete.png b/app/src/main/res/drawable-xhdpi/ic_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..eec14f009a0cff5693eac90425c72e361118508a GIT binary patch literal 160 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k0wldT1B8K8x~Gd{NX4z>1PRtD418@|ZEPwL zj3SDsXUOvWzxaUfQE%3P3q9OsJf_VnxTHE7To{vV%(5=5a9qsRD`m2T=a>718T*e& zmh>%fOz$v0(e{u#F4@j7LNNNzX@e(^b-sO;T;d*W{DhG~R^h;>n2Zy_Kr0zMUHx3v IIVCg!000Ix9smFU literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_delete.png b/app/src/main/res/drawable-xxhdpi/ic_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..8377c1da7f8a820163f250d0a77fe8a5bab6c536 GIT binary patch literal 213 zcmeAS@N?(olHy`uVBq!ia0vp^W+2SL0wmRZ7KH(+Ii4<#Ar-gY-q_38U?9TsVDlwI z7nuWx7-d{_Sfe)@FMSZyw`A{rW|yAx^*eVPsl*Eg?P1{ge3Yfbgm>Knp`IBgveT!| zUJ<+_?Q6zlvmD)X?D?P}btR>><|_r0+YVPAMLM(ohD{UV>|o?(#v6dW%i_#5a> N22WQ%mvv4FO#pt%Q;q-t literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxxhdpi/ic_delete.png b/app/src/main/res/drawable-xxxhdpi/ic_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..3a25d9b62232d819e51323a7d0c2c53148cc77ac GIT binary patch literal 201 zcmeAS@N?(olHy`uVBq!ia0vp^9w5xY0wn)GsXhawCV09yhE&{od*dvxgM*0c!`+M6 z&7?$LxhRD!;Myo1-1uWBi{J~4QQ+xaHjvJRbkAg$P z3--Nv)eMYGEF1zr9x~&L=7pq(XY2yf8BOg1av7)E1+4mLv^@OLo{;BKdPPRxKiY&Y nHPd_cF*NT{x~h`WqT;m@bD0n9fAzEs=r#sVS3j3^P6 - + + + + + et %d autre pouet à découvrir et %d autres pouets à découvrir + Supprimer une notification ? + Supprimer toutes les notifications ? + La notification a été supprimée! + Toutes les notifications ont été supprimées ! Abonnements Abonnés diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c6600597c..d6e8fe587 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -214,6 +214,10 @@ and another toot to discover and %d other toots to discover + Delete a notification? + Delete all notifications? + The notification has been deleted! + All notifications have been deleted! Following Followers