package com.swago.baseswago.util

import android.app.Activity
import android.app.AlertDialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import com.swago.baseswago.R

/**
 *@date 2021/11/28 22:09
 *description:
 */
object SwagoLoading {

    private var loadingDialog: AlertDialog? = null

    private fun showLoading(activity: Activity) {
        if (loadingDialog == null) {
            loadingDialog = AlertDialog.Builder(activity).create()
            loadingDialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
            loadingDialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            loadingDialog?.window?.setDimAmount(0.5f)
            loadingDialog?.setCanceledOnTouchOutside(false)
            loadingDialog?.let {
                if (!it.isShowing) {
                    it.show()
                }
            }
        }
        loadingDialog?.window?.let {
            it.setGravity(Gravity.CENTER)
            it.setContentView(R.layout.dialog_loading)
        }
    }


    fun showLoadingDialog(activity: Activity){
        cancelLoadingDialog()
        showLoading(activity)
    }


    fun cancelLoadingDialog(){
        loadingDialog?.dismiss()
        if (loadingDialog!=null){
            loadingDialog = null
        }
    }

}