Skip to content

Commit

Permalink
fix dialog control
Browse files Browse the repository at this point in the history
close dialog when lifecycle state is DESTROYED
  • Loading branch information
Alex committed Nov 2, 2020
1 parent 9b7010d commit 289d3cc
Showing 1 changed file with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.alexdeww.reactiveviewmodel.widget

import android.app.Dialog
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MediatorLiveData
import com.alexdeww.reactiveviewmodel.core.RvmViewComponent
import io.reactivex.rxjava3.core.Maybe
Expand Down Expand Up @@ -74,38 +75,50 @@ fun <T, R> DialogControl<T, R>.bindTo(
rvmViewComponent: RvmViewComponent,
createDialog: ActionCreateDialog<T, R>
) {
val mediator = object : MediatorLiveData<DialogControl.Display>() {
private var dialog: Dialog? = null

init {
addSource(displayed.liveData) {
value = it
when {
it is DialogControl.Display.Displayed<*> -> {
@Suppress("UNCHECKED_CAST")
dialog = createDialog(it.data as T, DialogControlResult(this@bindTo))
dialog?.setOnDismissListener { this@bindTo.dismiss() }
dialog?.show()
}
it === DialogControl.Display.Absent -> closeDialog()
val liveData = DialogLiveDataMediator(
control = this,
createDialog = createDialog,
lifecycleOwner = rvmViewComponent.componentLifecycleOwner
)
rvmViewComponent.run { liveData.observe { /* empty */ } }
}

private class DialogLiveDataMediator<T, R>(
control: DialogControl<T, R>,
createDialog: ActionCreateDialog<T, R>,
private val lifecycleOwner: LifecycleOwner
) : MediatorLiveData<DialogControl.Display>() {

private var dialog: Dialog? = null

init {
addSource(control.displayed.liveData) {
value = it
when {
it is DialogControl.Display.Displayed<*> -> {
@Suppress("UNCHECKED_CAST")
dialog = createDialog(it.data as T, DialogControlResult(control))
dialog?.setOnDismissListener { control.dismiss() }
dialog?.show()
}
it === DialogControl.Display.Absent -> closeDialog()
}
}
}

override fun onInactive() {
if (rvmViewComponent.componentLifecycleOwner.lifecycle.currentState == Lifecycle.State.DESTROYED) {
closeDialog()
}
super.onInactive()
override fun onInactive() {
if (lifecycleOwner.lifecycle.currentState == Lifecycle.State.DESTROYED) {
closeDialog()
}
super.onInactive()
}

private fun closeDialog() {
dialog?.apply {
setOnDismissListener(null)
dismiss()
}
dialog = null
private fun closeDialog() {
dialog?.apply {
setOnDismissListener(null)
dismiss()
}
dialog = null
}
rvmViewComponent.run { mediator.observe { /* empty */ } }

}

0 comments on commit 289d3cc

Please sign in to comment.