aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java
blob: 71f6ecc1a65a8febe56613c03459c82a6c20658a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * 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.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.ui.util;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;

import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.Snackbar.SnackbarDuration;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.enums.SnackbarType;
import com.nispok.snackbar.listeners.ActionClickListener;
import com.nispok.snackbar.listeners.EventListenerAdapter;

import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.util.FabContainer;

/**
 * Notify wrapper which allows a more easy use of different notification libraries
 */
public class Notify {

    public enum Style {
        OK (R.color.android_green_light), WARN(R.color.android_orange_light), ERROR(R.color.android_red_light);

        public final int mLineColor;

        Style(int color) {
            mLineColor = color;
        }

        public void applyToBar(Snackbar bar) {
            bar.lineColorResource(mLineColor);
        }
    }

    public static final int LENGTH_INDEFINITE = 0;
    public static final int LENGTH_LONG = 3500;
    public static final int LENGTH_SHORT = 1500;

    public static Showable create(final Activity activity, String text, int duration, Style style,
                                  final ActionListener actionListener, Integer actionResId) {
        final Snackbar snackbar = Snackbar.with(activity)
                .type(SnackbarType.MULTI_LINE)
                .text(text);

        if (duration == LENGTH_INDEFINITE) {
            snackbar.duration(SnackbarDuration.LENGTH_INDEFINITE);
        } else {
            snackbar.duration(duration);
        }

        style.applyToBar(snackbar);

        if (actionResId != null) {
            snackbar.actionLabel(actionResId);
        }
        if (actionListener != null) {
            snackbar.actionListener(new ActionClickListener() {
                @Override
                public void onActionClicked(Snackbar snackbar) {
                    actionListener.onAction();
                }
            });
        }

        if (activity instanceof FabContainer) {
            snackbar.eventListener(new EventListenerAdapter() {
                @Override
                public void onShow(Snackbar snackbar) {
                    ((FabContainer) activity).fabMoveUp(snackbar.getHeight());
                }

                @Override
                public void onDismiss(Snackbar snackbar) {
                    ((FabContainer) activity).fabRestorePosition();
                }
            });
        }

        return new Showable() {
            @Override
            public void show() {
                SnackbarManager.show(snackbar, activity);
            }

            @Override
            public void show(Fragment fragment, boolean animate) {
                snackbar.animation(animate);
                snackbar.dismissOnActionClicked(animate);
                show(fragment);
            }

            @Override
            public void show(Fragment fragment) {
                if (fragment != null) {
                    View view = fragment.getView();

                    if (view != null && view instanceof ViewGroup) {
                        SnackbarManager.show(snackbar, (ViewGroup) view);
                        return;
                    }
                }

                show();
            }

            @Override
            public void show(ViewGroup viewGroup) {
                if (viewGroup != null) {
                    SnackbarManager.show(snackbar, viewGroup);
                    return;
                }

                show();
            }
        };
    }

    public static Showable create(Activity activity, String text, int duration, Style style) {
        return create(activity, text, duration, style, null, null);
    }

    public static Showable create(Activity activity, String text, Style style) {
        return create(activity, text, LENGTH_LONG, style);
    }

    public static Showable create(Activity activity, int textResId, Style style,
            ActionListener actionListener, int actionResId) {
        return create(activity, textResId, LENGTH_LONG, style, actionListener, actionResId);
    }

    public static Showable create(Activity activity, int textResId, int duration, Style style,
                                  ActionListener actionListener, int actionResId) {
        return create(activity, activity.getString(textResId), duration, style, actionListener, actionResId);
    }

    public static Showable create(Activity activity, int textResId, int duration, Style style) {
        return create(activity, activity.getString(textResId), duration, style);
    }

    public static Showable create(Activity activity, int textResId, Style style) {
        return create(activity, activity.getString(textResId), style);
    }

    public interface Showable {

        /**
         * Shows the notification on the bottom of the Activity.
         */
        void show();

        void show(Fragment fragment, boolean animate);

        /**
         * Shows the notification on the bottom of the Fragment.
         */
        void show(Fragment fragment);

        /**
         * Shows the notification on the given ViewGroup.
         * The viewGroup should be either a RelativeLayout or FrameLayout.
         */
        void show(ViewGroup viewGroup);

    }

    public interface ActionListener {

        void onAction();

    }

}