Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToastUtil 简单封装 #179

Open
luziqi123 opened this issue Sep 16, 2021 · 0 comments
Open

ToastUtil 简单封装 #179

luziqi123 opened this issue Sep 16, 2021 · 0 comments

Comments

@luziqi123
Copy link

luziqi123 commented Sep 16, 2021

简单封装
ToastUtil.error().showLong("error")
ToastUtil.warning().showLong("warning")
ToastUtil.success().showLong("success")
ToastUtil.showShort("normal")



public class ToastUtil {

    static {
        // 默认设置
        Toasty.Config.getInstance()
                .setRTL(false)
                .supportDarkTheme(true)
                .allowQueue(false)
                .apply();
    }

    // 通过这个方法获取Config 自定义设置
    public static Toasty.Config config() {
        return Toasty.Config.getInstance();
    }

    private static final RealToast toaster = new RealToast();

    public static RealToast error() {
        toaster.setType(RealToast.TYPE_ERROR);
        return toaster;
    }

    public static RealToast warning() {
        toaster.setType(RealToast.TYPE_WARNING);
        return toaster;
    }

    public static RealToast success() {
        toaster.setType(RealToast.TYPE_SUCCESS);
        return toaster;
    }


    public static void showLong(String msg) {
        toaster.setType(RealToast.TYPE_NORMAL);
        toaster.showLong(msg);
    }

    public static void showShort(String msg) {
        toaster.setType(RealToast.TYPE_NORMAL);
        toaster.showShort(msg);
    }

    public static class RealToast {
        protected RealToast() {}

        protected static final int TYPE_NORMAL = 0;
        protected static final int TYPE_ERROR = 1;
        protected static final int TYPE_WARNING = 2;
        protected static final int TYPE_SUCCESS = 3;
        int type;

        protected void setType(int type) {
            this.type = type;
        }

        public void showLong(String msg) {
            Toast toast = getToastForType(msg);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();
        }

        public void showShort(String msg) {
            Toast toast = getToastForType(msg);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.show();
        }

        private Toast getToastForType(String msg) {
            switch (type) {
                case TYPE_NORMAL:
                    return Toasty.normal(Constants.application, msg, 0);
                case TYPE_ERROR:
                    return Toasty.error(Constants.application, msg, 0);
                case TYPE_WARNING:
                    return Toasty.warning(Constants.application, msg, 0);
                case TYPE_SUCCESS:
                    return Toasty.success(Constants.application, msg, 0);
            }
            return Toasty.normal(Constants.application, msg, 0);
        }
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant