CookieBar.java 5.65 KB
package com.wd.common.notify;

import android.app.Activity;
import android.view.Gravity;
import android.view.ViewGroup;

import androidx.annotation.ColorRes;
import androidx.annotation.StringRes;

/**
 * CookieBar is a lightweight library for showing a brief message at the top or bottom of the screen.
 * <p>
 * <pre>
 * new CookieBar
 *      .Builder(MainActivity.this)
 *      .setTitle("TITLE")
 *      .setMessage("MESSAGE")
 *      .setAction(new OnActionClickListener() {})
 *      .show();
 * </pre>
 * <p>
 */
public class CookieBar {

    private static final String TAG = "cookie";

    private Cookie cookieView;
    private Activity context;

    private CookieBar() {
    }

    private CookieBar(Activity context, Params params) {
        this.context = context;
        cookieView = new Cookie(context);
        cookieView.setParams(params);
    }

    public void show() {
        if (cookieView != null) {
            final ViewGroup decorView = (ViewGroup) context.getWindow().getDecorView();
            final ViewGroup content = decorView.findViewById(android.R.id.content);
            if (cookieView.getParent() == null) {
                if (cookieView.getLayoutGravity() == Gravity.BOTTOM) {
                    content.addView(cookieView);
                } else {
                    decorView.addView(cookieView);
                }

            }
        }
    }

    /**
     * 提醒弹框消失
     */
    public void dismiss() {
        if (cookieView != null) {
            cookieView.destroy();
        }
    }

    public static class Builder {

        private Params params = new Params();

        public Activity activity;

        public Builder(Activity activity) {
            this.activity = activity;
        }

        public Builder setTitle(String title) {
            params.title = title;
            return this;
        }

        public Builder setTitle(@StringRes int resId) {
            params.title = activity.getString(resId);
            return this;
        }

        public Builder setMessage(String message) {
            params.message = message;
            return this;
        }

        public Builder setMessage(@StringRes int resId) {
            params.message = activity.getString(resId);
            return this;
        }

        public Builder setImgUrl(String imgUrl) {
            params.imgUrl = imgUrl;
            return this;
        }

        public Builder setPushScene(int pushScene) {
            params.pushScene = pushScene;
            return this;
        }

        public Builder setLiveStatus(String liveStatus) {
            params.liveStatus = liveStatus;
            return this;
        }

        public Builder setOrder(boolean isOrder) {
            params.isOrder = isOrder;
            return this;
        }

        public Builder setPlanStartTime(long planStartTimeLong) {
            params.planStartTimeLong = planStartTimeLong;
            return this;
        }

        public Builder setTargetId(String targetId) {
            params.targetId = targetId;
            return this;
        }

        public Builder setTargetRelIdId(String targetRelIdId) {
            params.targetRelIdId = targetRelIdId;
            return this;
        }

        public Builder setDuration(long duration) {
            params.duration = duration;
            return this;
        }

        public Builder setTitleColor(@ColorRes int titleColor) {
            params.titleColor = titleColor;
            return this;
        }

        public Builder setMessageColor(@ColorRes int messageColor) {
            params.messageColor = messageColor;
            return this;
        }

        public Builder setBackgroundColor(@ColorRes int backgroundColor) {
            params.backgroundColor = backgroundColor;
            return this;
        }

        public Builder setActionListener(OnActionClickListener onActionClickListener) {
            params.onActionClickListener = onActionClickListener;
            return this;
        }
        
        public Builder setDismissListener(OnDismissListener onDismissListener){
            params.onDismissListener = onDismissListener;
            return this;
        }

        public Builder setLayoutGravity(int layoutGravity) {
            params.layoutGravity = layoutGravity;
            return this;
        }

        public CookieBar create() {
            CookieBar cookie = new CookieBar(activity, params);
            return cookie;
        }

        public CookieBar show() {
            final CookieBar cookie = create();
            cookie.show();
            return cookie;
        }
    }

    public static class Params {

        public String title;

        public String message;

        public String imgUrl;

        public OnActionClickListener onActionClickListener;
        
        public OnDismissListener onDismissListener;

        public int backgroundColor;

        public int titleColor;

        public int messageColor;

        public long duration = 2000;

        public int layoutGravity = Gravity.TOP;

        /**
         * 推送场景:pushScene:1:直播提醒;2:用户订阅;3:普通站内通知 0是普通推送 ,5是自定义定位权限提醒
         */
        public int pushScene;

        /**
         * 直播状态"liveStatus":"wait" ,"running"
         *
         */
        public String liveStatus;

        /**
         * 是否预约,true已预约
         */
        public boolean isOrder;

        /**
         * 开播时间戳
         */
        public long planStartTimeLong;

        /**
         * 直播Id
         */
        public String  targetId;

        /**
         * 直播关系Id
         */
        public String  targetRelIdId;




    }

}