1

I have 2 alerts in sequence, where the last one does not close with dismiss, could you help me please? Below my code snippet. I have a custom view

fun MaterialDialog.Builder.alertChangedIcon (action: () -> Unit) {
    this.apply {
        customView(R.layout.change_icon_dialog, false)
        canceledOnTouchOutside(false)
        build().run {
            val btnPosition = this.findViewById(R.id.yesBtnView) as Button
            btnPosition.setOnClickListener {
                this.dismiss()
                action.invoke()
            }
        }
        show()
    }
}

fun MaterialDialog.Builder.alertIconInfoChanged(action: () -> Unit) {
    this.apply {
        customView(R.layout.title_subtitle_two_buttons_dialog_prime,false)
        canceledOnTouchOutside(false)
        build().run {
            val title = this.findViewById(R.id.alertTitleView) as TextView
            val subtitle = this.findViewById(R.id.alertSubtitleView) as TextView
            val positiveButton = this.findViewById(R.id.yesBtnView) as Button
            val negativeButton = this.findViewById(R.id.noBtnView) as Button

            title.text = context.getString(R.string.happy_birthday)
            subtitle.text = context.getString(R.string.message_change_icon)
            negativeButton.let {
                it.text = context.getString(R.string.ok)
                it.setOnClickListener {
                    this.dismiss()
                    action.invoke()
                }
            }
            positiveButton.gone(false)
        }
        show()
    }
}

And use as follows in my view:

 override fun showAlertChangedIcon(action: () -> Unit) {
    MaterialDialog.Builder(rootView.context).alertIconInfoChanged {
        MaterialDialog.Builder(rootView.context).alertChangedIcon {
            action.invoke()
        }
    }
}

And in my controller I have the functions that direct screens

.subscribe({ response ->
                    when (response) {
                        is Result.Success -> {
                            viewContract.showAlertChangedIcon {
                                when {
                                    ...
                                    }
                                    else -> {
                                        ...
                                    }
                                }
                            }
Gabi
  • 11
  • 2

1 Answers1

0

I suspect it has to do with the labeled returns.

When you call run here:

fun MaterialDialog.Builder.alertChangedIcon (action: () -> Unit) {
    this.apply {
        ...
        build().run {          <----- when you call here
            ...
            btnPosition.setOnClickListener {
                this.dismiss() <----- "this" may be the builder of the dialog,
                                      and not the dialog itself
                ...

Try replacing this.dismiss() with the labeled return this@apply.dismiss() so you tell Kotlin exactly what to dismiss.

I hope this helps!

ndriqa
  • 362
  • 2
  • 13
  • Thanks for the answer ndriqa! But my this is already the MaterialDialog. And the strange thing is that it falls into the breakpoint of dismiss, it just doesn't rule out – Gabi Aug 23 '22 at 14:28