package com.ez.ertc.meeting.utils;

import android.view.Gravity;
import android.widget.Toast;

import androidx.annotation.StringRes;

import com.ez.ertc.meeting.ErtcEzmHelper;
import com.ez.ertc.meeting.utils.safetoast.ToastCompat;

/**
 * @author xiezuoyuan
 * @time 2018/6/12 10:29
 */
public class UIUtil {
    private static ToastCompat toast = null;
    private static ToastCompat toastLong = null;

    public static void toast(final String str) {
        if (toast != null) {
            toast.cancel();
        }
        toast = ToastCompat.makeText(ErtcEzmHelper.getContext(), str, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);

        toast.show();
    }

    public static void toast(@StringRes final int resId) {

        String str = ErtcEzmHelper.getContext().getString(resId);
        if (toast != null) {
            toast.cancel();
        }
        toast = ToastCompat.makeText(ErtcEzmHelper.getContext(), str, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);

        toast.show();
    }

    /**
     * 解决连续点击后不显示的bug
     *
     * @param resId
     */
    public static void toastLong(@StringRes final int resId) {
        String str = ErtcEzmHelper.getContext().getString(resId);
        if (toastLong == null) {
            toastLong = ToastCompat.makeText(ErtcEzmHelper.getContext(), str, Toast.LENGTH_LONG);
            toastLong.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toastLong.cancel();
            toastLong = ToastCompat.makeText(ErtcEzmHelper.getContext(), str, Toast.LENGTH_LONG);
            toastLong.setGravity(Gravity.CENTER, 0, 0);
            toastLong.setText(str);
        }
        toastLong.show();
    }

}
