74 lines
1.8 KiB
Kotlin
74 lines
1.8 KiB
Kotlin
package com.sw.inbound.dialog
|
|
|
|
import android.app.Activity
|
|
import android.app.Dialog
|
|
import android.content.Context
|
|
import android.graphics.Color
|
|
import android.view.LayoutInflater
|
|
import androidx.core.graphics.drawable.toDrawable
|
|
import com.sw.inbound.R
|
|
import com.sw.inbound.databinding.DialogLoadingBinding
|
|
import kotlin.let
|
|
import kotlin.run
|
|
|
|
object Loading {
|
|
|
|
private var dialog: LoadingDialog? = null
|
|
|
|
fun show(activity: Activity) {
|
|
try {
|
|
if (activity.isFinishing || activity.isDestroyed) {
|
|
return
|
|
}
|
|
if (dialog?.isShowing == true) {
|
|
dialog?.dismiss()
|
|
}
|
|
if (dialog == null) {
|
|
dialog = LoadingDialog(activity)
|
|
}
|
|
dialog?.show()
|
|
} catch (e: Exception) {
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
|
|
fun dismiss() {
|
|
try {
|
|
dialog?.let {
|
|
if (it.isShowing) {
|
|
it.dismiss()
|
|
}
|
|
}
|
|
dialog = null
|
|
} catch (e: Exception) {
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
class LoadingDialog(
|
|
context: Context,
|
|
message: String = "加载中……",
|
|
private var onDismiss: () -> Unit = {}
|
|
) : Dialog(context, R.style.LoadingDialog) {
|
|
init {
|
|
val binding = DialogLoadingBinding.inflate(LayoutInflater.from(context))
|
|
binding.tvMessage.text = message
|
|
setContentView(binding.root)
|
|
setCancelable(true)
|
|
setCanceledOnTouchOutside(true)
|
|
window?.run {
|
|
setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
|
|
// setFlags(
|
|
// WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
|
// WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
|
// )
|
|
}
|
|
setOnDismissListener {
|
|
onDismiss()
|
|
}
|
|
}
|
|
}
|
|
|