package com.zxhy.hfilemanagermaster.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import androidx.appcompat.app.AlertDialog
import com.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.DialogLoadingBinding

fun Context.showLoadingDialog(): Dialog {
    val binding = DialogLoadingBinding.inflate(LayoutInflater.from(this))
    val dialog = AlertDialog.Builder(this).setView(binding.root).create()
    dialog.show()
    dialog.setCanceledOnTouchOutside(false)

    //修改dialog的尺寸
    val lp = dialog.window?.attributes
    lp?.width = this.resources.getDimensionPixelOffset(R.dimen.dp_255)
    lp?.height = this.resources.getDimensionPixelOffset(R.dimen.dp_177)
    dialog.window?.attributes = lp
    dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
//    dialog.window?.setDimAmount(0f)

    // 添加旋转动画
    val rotateAnimation = RotateAnimation(
        0f, 360f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f
    )
    rotateAnimation.duration = 1000
    rotateAnimation.interpolator = LinearInterpolator()
    rotateAnimation.repeatCount = Animation.INFINITE
    binding.ivLoading.startAnimation(rotateAnimation)

    return dialog

}