Allows to remove notifications

This commit is contained in:
tom79 2017-07-30 11:21:55 +02:00
parent c49ce6a561
commit 79fa8efcfc
15 changed files with 251 additions and 40 deletions

View File

@ -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 <http://www.gnu.org/licenses>. */
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<Void, Void, Void> {
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);
}
}

View File

@ -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

View File

@ -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<Notification> 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;

View File

@ -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;

View File

@ -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;

View File

@ -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 <http://www.gnu.org/licenses>. */
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);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 201 B

View File

@ -30,13 +30,29 @@
android:layout_height="wrap_content"
android:id="@+id/main_container"
android:orientation="horizontal">
<ImageView
android:id="@+id/notification_account_profile"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_gravity="center_horizontal|top"
android:gravity="center_horizontal|top"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/notification_account_profile"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_gravity="center_horizontal|top"
android:gravity="center_horizontal|top"
tools:ignore="ContentDescription" />
<ImageView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:id="@+id/notification_delete"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_delete"
android:layout_width="25dp"
android:layout_height="25dp"
tools:ignore="ContentDescription" />
</LinearLayout>
<LinearLayout
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"

View File

@ -209,6 +209,10 @@
<item quantity="one">et %d autre pouet à découvrir</item>
<item quantity="other">et %d autres pouets à découvrir</item>
</plurals>
<string name="delete_notification_ask">Supprimer une notification ?</string>
<string name="delete_notification_ask_all">Supprimer toutes les notifications ?</string>
<string name="delete_notification">La notification a été supprimée!</string>
<string name="delete_notification_all">Toutes les notifications ont été supprimées !</string>
<!-- HEADER -->
<string name="following">Abonnements</string>
<string name="followers">Abonnés</string>

View File

@ -214,6 +214,10 @@
<item quantity="one">and another toot to discover</item>
<item quantity="other">and %d other toots to discover</item>
</plurals>
<string name="delete_notification_ask">Delete a notification?</string>
<string name="delete_notification_ask_all">Delete all notifications?</string>
<string name="delete_notification">The notification has been deleted!</string>
<string name="delete_notification_all">All notifications have been deleted!</string>
<!-- HEADER -->
<string name="following">Following</string>
<string name="followers">Followers</string>