From 198ddfeff7324533fb2daea778891def04f8e8c2 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 19 Jan 2015 15:43:35 +0100 Subject: use Notify helper everywhere, replace supertoasts with snackbar library --- .../keychain/ui/util/Notify.java | 111 +++++++++++++++++++-- 1 file changed, 100 insertions(+), 11 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java index 551ac039d..66d6bf9e3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java @@ -19,18 +19,23 @@ package org.sufficientlysecure.keychain.ui.util; import android.app.Activity; import android.content.res.Resources; +import android.graphics.Color; -import com.github.johnpersano.supertoasts.SuperCardToast; -import com.github.johnpersano.supertoasts.SuperToast; +import com.nispok.snackbar.Snackbar; +import com.nispok.snackbar.Snackbar.SnackbarDuration; +import com.nispok.snackbar.SnackbarManager; +import com.nispok.snackbar.listeners.ActionClickListener; /** - * @author danielhass * Notify wrapper which allows a more easy use of different notification libraries */ public class Notify { public static enum Style {OK, WARN, INFO, ERROR} + public static final int LENGTH_INDEFINITE = 0; + public static final int LENGTH_LONG = 3500; + /** * Shows a simple in-layout notification with the CharSequence given as parameter * @param activity @@ -39,21 +44,94 @@ public class Notify { */ public static void showNotify(Activity activity, CharSequence text, Style style) { - SuperCardToast st = new SuperCardToast(activity); - st.setText(text); - st.setDuration(SuperToast.Duration.MEDIUM); - switch (style){ + Snackbar bar = Snackbar.with(activity) + .text(text) + .duration(SnackbarDuration.LENGTH_LONG); + + switch (style) { case OK: - st.setBackground(SuperToast.Background.GREEN); break; case WARN: - st.setBackground(SuperToast.Background.ORANGE); + bar.textColor(Color.YELLOW); break; case ERROR: - st.setBackground(SuperToast.Background.RED); + bar.textColor(Color.RED); break; } - st.show(); + + SnackbarManager.show(bar); + + } + + public static Showable createNotify (Activity activity, int resId, int duration, Style style) { + final Snackbar bar = Snackbar.with(activity) + .text(resId); + if (duration == LENGTH_INDEFINITE) { + bar.duration(SnackbarDuration.LENGTH_INDEFINITE); + } else { + bar.duration(duration); + } + + switch (style) { + case OK: + bar.actionColor(Color.GREEN); + break; + case WARN: + bar.textColor(Color.YELLOW); + break; + case ERROR: + bar.textColor(Color.RED); + break; + } + + return new Showable () { + @Override + public void show() { + SnackbarManager.show(bar); + } + }; + } + + public static Showable createNotify(Activity activity, int resId, int duration, Style style, + final ActionListener listener, int resIdAction) { + return createNotify(activity, activity.getString(resId), duration, style, listener, resIdAction); + } + + public static Showable createNotify(Activity activity, String msg, int duration, Style style, + final ActionListener listener, int resIdAction) { + final Snackbar bar = Snackbar.with(activity) + .text(msg) + .actionLabel(resIdAction) + .actionListener(new ActionClickListener() { + @Override + public void onActionClicked(Snackbar snackbar) { + listener.onAction(); + } + }); + if (duration == LENGTH_INDEFINITE) { + bar.duration(SnackbarDuration.LENGTH_INDEFINITE); + } else { + bar.duration(duration); + } + + switch (style) { + case OK: + bar.actionColor(Color.GREEN); + break; + case WARN: + bar.textColor(Color.YELLOW); + break; + case ERROR: + bar.textColor(Color.RED); + break; + } + + return new Showable () { + @Override + public void show() { + SnackbarManager.show(bar); + } + }; } @@ -67,4 +145,15 @@ public class Notify { public static void showNotify(Activity activity, int resId, Style style) throws Resources.NotFoundException { showNotify(activity, activity.getResources().getText(resId), style); } + + public interface Showable { + public void show(); + + } + + public interface ActionListener { + public void onAction(); + + } + } \ No newline at end of file -- cgit v1.2.3