Newer
Older
Endoscope / app / src / main / java / com / casic / endoscope / widgets / PreviewImageDialog.kt
package com.casic.endoscope.widgets

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import com.bumptech.glide.Glide
import com.casic.endoscope.R
import com.casic.endoscope.databinding.DialogPreviewImageBinding
import com.pengxh.kt.lite.extensions.binding
import com.pengxh.kt.lite.extensions.getScreenHeight
import com.pengxh.kt.lite.extensions.getScreenWidth

class PreviewImageDialog private constructor(builder: Builder) : Dialog(
    builder.context, R.style.UserDefinedDialogStyle
) {
    private val imagePath = builder.imagePath

    class Builder {
        lateinit var context: Context
        lateinit var imagePath: String

        fun setContext(context: Context): Builder {
            this.context = context
            return this
        }

        fun setImagePath(imagePath: String): Builder {
            this.imagePath = imagePath
            return this
        }

        fun build(): PreviewImageDialog {
            return PreviewImageDialog(this)
        }
    }

    private val binding: DialogPreviewImageBinding by binding()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.initDialogLayoutParams()
        setContentView(R.layout.dialog_preview_image)
        setCancelable(true)
        setCanceledOnTouchOutside(true)

        if (imagePath.isBlank()) {
            return
        }

        Glide.with(context).load(imagePath).into(binding.photoView)
    }

    private fun Dialog.initDialogLayoutParams() {
        val window = this.window ?: return
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window.decorView.setBackgroundColor(Color.TRANSPARENT)
        val params = window.attributes
        params.width = ((context.getScreenWidth() * 0.65).toInt())
        params.height = ((context.getScreenHeight() * 0.58).toInt())
        window.attributes = params
    }
}