Skip to content

Instantly share code, notes, and snippets.

@nfaycel
Forked from qamarelsafadi/BaseDialog.kt
Created April 4, 2023 10:09
Show Gist options
  • Save nfaycel/8f7efc2d1f722e19d36b581376654bbb to your computer and use it in GitHub Desktop.
Save nfaycel/8f7efc2d1f722e19d36b581376654bbb to your computer and use it in GitHub Desktop.
This class is generic class for creating your custom dialogs, it will helps you to use one class for all ur custom dialogs.
/*
* Created by Qamar A. Safadi on 6/13/22, 9:50 PM
* Copyright (c) 2022 .
*/
package com.example.qamar.ui.base
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
open class BaseDialog<T>() : DialogFragment() where T : ViewDataBinding {
private var layoutRes: Int = 0
var onBind: (binding: T) -> Unit = { _ -> }
var onCancelListener: () -> Unit = {}
constructor(
@LayoutRes layoutRes: Int,
onBind: (binding: T) -> Unit,
onCancelListener: () -> Unit = {}
) : this() {
this.layoutRes = layoutRes
this.onBind = onBind
this.onCancelListener = onCancelListener
}
lateinit var binding: ViewDataBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return object : Dialog(requireActivity(), theme) {
override fun onBackPressed() {
onCancelListener()
}
}
}
override fun onStart() {
super.onStart()
dialog?.let {
it.setOnCancelListener {
onCancelListener()
}
it.window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
it.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return try {
binding = DataBindingUtil.inflate(inflater, layoutRes, container, false)
binding.root
} catch (e: Exception) {
Log.e(
"BaseDialogException",
"Inflating Error, You had forget to convert your layout to data binding layout"
)
null
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
try {
onBind(binding as T)
} catch (e: Exception) {
Log.e(
"BaseDialogException",
"Casting error, please make sure you're passing the right binding class",
)
}
}
}
}
private lateinit var successDialog: BaseDialog<DialogSuccessBinding>
private fun bindSuccessDialog() {
successDialog = BaseDialog(
R.layout.dialog_success,
onBind = { binding ->
with(binding) {
doneBtn.onClick {
successDialog.dismiss()
}
}
},
onCancelListener = {}) // you can ignore calling onCancelListener if you want your dialog not cancelable
successDialog.show(childFragmentManager, "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment