package com.ydl.ydlcommon.utils.remind

import android.support.annotation.StringRes
import android.text.TextUtils
import android.widget.Toast
import com.ydl.ydlcommon.base.BaseApp

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/01/29
 *
 * 提示信息的封装类，使用单例 toast，防止重复弹出提示
 */
class ToastHelper private constructor() {

    private var toast: Toast? = null

    companion object {
        private fun getInstance(): ToastHelper {
            return Holder.INSTANCE
        }

        fun show(text: String) {
            getInstance().show(text)
        }

        fun show(@StringRes resId: Int) {
            getInstance().show(resId)
        }
    }

    private fun show(text: CharSequence) {
        if (TextUtils.isEmpty(text)) return
        if (toast == null) toast = Toast.makeText(BaseApp.getApp(), text, Toast.LENGTH_SHORT)
        else toast?.setText(text)
        toast?.show()
    }

    private fun show(@StringRes resId: Int) {
        if (toast == null) toast = Toast.makeText(BaseApp.getApp(), resId, Toast.LENGTH_SHORT)
        else toast?.setText(resId)
        toast?.show()
    }

    private object Holder {
        val INSTANCE = ToastHelper()
    }
}