BaseNewDialog.java 2.93 KB
package com.wd.common.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.wd.foundation.wdkit.utils.ScreenUtils;

/**
 * @author linksus
 * @version 1.0.0
 * @desc 所有dialog的基类
 * @time 8/7 0007 15:16
 */
public abstract class BaseNewDialog extends LayerDialog implements View.OnClickListener {

    private WindowManager.LayoutParams layoutParams;

    protected Window window;

    protected View layoutView;

    public BaseNewDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public BaseNewDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    private void init(Context context) {
        layoutView = LayoutInflater.from(context).inflate(getLayoutRes(), null, false);
        setCanceledOnTouchOutside(canceledOnTouchOutside());
        setContentView(layoutView);
        window = getWindow();
        if (window != null) {
            layoutParams = getWindow().getAttributes();
            layoutParams.gravity = gravity();
            layoutParams.width = (int) (ScreenUtils.getDisplaySize((Activity) context).widthPixels /
                    (pixelsWidthScale() <= 0 ? 1 : pixelsWidthScale()));
            if (pixelsHeightScale() > 0) {
                layoutParams.height = (int) (ScreenUtils.getDisplaySize((Activity) context).heightPixels /
                        pixelsHeightScale());
            }else{
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }
            if (dialogAnimation() != 0) {
                window.setWindowAnimations(dialogAnimation());
            }
            window.setAttributes(layoutParams);
        }
        attachView(layoutView);
        attachListener();
    }

    protected abstract int gravity();

    protected abstract float pixelsHeightScale();
    protected abstract float regularHeight();

    protected abstract float pixelsWidthScale();

    protected abstract boolean canceledOnTouchOutside();

    protected abstract void attachView(View view);

    protected abstract void attachListener();

    protected abstract void attachData();

    protected abstract void detachView();

    protected abstract @StyleRes
    int dialogAnimation();

    protected <T extends View> T $findViewById(@IdRes int resId) {
        return (T) layoutView.findViewById(resId);
    }

    protected abstract @LayoutRes
    int getLayoutRes();

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        detachView();
        super.dismiss();
    }

    @Override
    public void onClick(View v) {

    }
}