aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java
blob: 3576d1cd0686a830d710e93afa54cc597a353b13 (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
package de.thedevstack.conversationsplus.utils.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.R;

/**
 * Wrapper for custom styled Toasts for Conversations+.
 */
public final class ConversationsPlusToast {

    /**
     * Creates an error toast with text and duration
     * @param text
     * @param duration
     */
    public static void makeErrorToast(final CharSequence text, final int duration) {
        Handler toastHandler = new Handler(Looper.getMainLooper());
        toastHandler.post(new ConversationsPlusToast.ToastRunnable(text, duration, true));
    }

    /**
     * Creates an error toast with text from resource and duration
     * @param resId
     * @param duration
     */
    public static void makeErrorToast(@StringRes int resId, int duration) {
        makeErrorToast(ConversationsPlusApplication.getAppContext().getString(resId), duration);
    }

    /**
     * Creates an toast with text and duration
     * @param text
     * @param duration
     */
    public static void makeToast(final CharSequence text, final int duration) {
        Handler toastHandler = new Handler(Looper.getMainLooper());
        toastHandler.post(new ConversationsPlusToast.ToastRunnable(text, duration));
    }

    /**
     * Creates an toast with text from resource and duration
     * @param resId
     * @param duration
     */
    public static void makeToast(@StringRes int resId, int duration) {
        makeToast(ConversationsPlusApplication.getAppContext().getString(resId), duration);
    }

    private ConversationsPlusToast() {
        // avoid instantiation - helper class
    }

    /**
     * Runnable to show the toast in an UI thread.
     */
    static class ToastRunnable implements Runnable {
        private boolean showErrorToast = false;
        private CharSequence text;
        private int duration;

        public ToastRunnable(CharSequence text, int duration, boolean isError) {
            this.showErrorToast = isError;
            this.text = text;
            this.duration = duration;
        }

        public ToastRunnable(CharSequence text, int duration) {
            this.text = text;
            this.duration = duration;
        }

        @Override
        public void run() {
            Context applicationContext = ConversationsPlusApplication.getAppContext();
            LayoutInflater inflater = LayoutInflater.from(applicationContext);
            View layout = inflater.inflate(R.layout.cplus_toast_container, null);

            TextViewUtil.setText(layout, R.id.cplus_toast_txt, text);

            if (this.showErrorToast) {
                // Set image view to error icon
                ImageView iv = (ImageView) layout.findViewById(R.id.cplus_toast_icon);
                iv.setImageResource(R.drawable.ic_toast_error);
            }

            Toast toast = new Toast(applicationContext);
            toast.setDuration(duration);
            toast.setView(layout);
            toast.show();
        }
    }
}