Last active
November 24, 2022 08:30
-
-
Save ibrahim-mrq/d3d016d446b043251dba86523ee7013d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mrq.library.helpers | |
| import android.app.Dialog | |
| import android.graphics.Color | |
| import android.graphics.drawable.ColorDrawable | |
| import android.os.Bundle | |
| import android.util.Log | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import androidx.annotation.LayoutRes | |
| import androidx.databinding.DataBindingUtil | |
| import androidx.databinding.ViewDataBinding | |
| import androidx.fragment.app.DialogFragment | |
| import kotlin.Exception | |
| 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.WRAP_CONTENT, | |
| ViewGroup.LayoutParams.WRAP_CONTENT | |
| ) | |
| 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", | |
| ) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private fun bindChangePasswordSheet() { | |
| val changePasswordSheet = BaseBottomSheet( | |
| layoutRes = R.layout.change_password_bottom_sheet, | |
| corneredTheme = R.style.AppBottomSheetDialogTheme, | |
| isCornered = true, | |
| onBind = { view, _binding -> | |
| val bind = _binding as ChangePasswordBottomSheetBinding | |
| with(bind) { | |
| // do what you want | |
| } | |
| }) | |
| dialog.isCancelable = false | |
| dialog.show(FragmentActivity().supportFragmentManager, "") | |
| // or | |
| dialog.show(childFragmentManager, "") | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment