Skip to content

Commit 6d4b6fd

Browse files
author
Alex
committed
update DialogControl.kt
1 parent baae48f commit 6d4b6fd

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

reactiveviewmodel/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ afterEvaluate {
4545
release(MavenPublication) {
4646
from components.release
4747
groupId = 'com.alexdeww.reactiveviewmodel'
48-
version = '2.4.8'
48+
version = '2.4.9'
4949
}
5050
}
5151
}

reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/RvmViewComponent.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.alexdeww.reactiveviewmodel.core
22

3+
import android.app.Dialog
34
import android.widget.CompoundButton
45
import android.widget.EditText
56
import android.widget.RatingBar
@@ -63,10 +64,10 @@ interface RvmViewComponent {
6364
)
6465

6566
fun <T : Any, R : Any> DialogControl<T, R>.bindTo(
66-
createDialog: ActionCreateDialog<T, R>
67+
dialogFactory: DialogFactory<T, R, Dialog>
6768
) = bindTo(
6869
rvmViewComponent = this@RvmViewComponent,
69-
createDialog = createDialog
70+
dialogFactory = dialogFactory
7071
)
7172

7273
fun InputControl.bindTo(

reactiveviewmodel/src/main/java/com/alexdeww/reactiveviewmodel/widget/DialogControl.kt

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.alexdeww.reactiveviewmodel.widget
22

33
import android.app.Dialog
4+
import androidx.lifecycle.LiveData
45
import androidx.lifecycle.MediatorLiveData
56
import androidx.lifecycle.Observer
67
import com.alexdeww.reactiveviewmodel.core.RvmViewComponent
@@ -71,51 +72,88 @@ fun <T : Any, R : Any> dialogControl(): DialogControl<T, R> = DialogControl()
7172

7273
fun <T : Any> dialogControlWithResult(): DialogControl<T, DialogResult> = DialogControl()
7374

74-
typealias ActionCreateDialog<T, R> = (data: T, dc: DialogControlResult<R>) -> Dialog
75+
fun interface DialogFactory<in T : Any, R : Any, out D : Any> {
76+
fun createDialog(data: T, dc: DialogControlResult<R>): D
77+
}
7578

76-
fun <T : Any, R : Any> DialogControl<T, R>.bindTo(
79+
fun <T : Any, R : Any, D : Any> DialogControl<T, R>.bindToEx(
7780
rvmViewComponent: RvmViewComponent,
78-
createDialog: ActionCreateDialog<T, R>
81+
dialogFactory: DialogFactory<T, R, D>,
82+
dialogLiveDataProvider: (control: DialogControl<T, R>, dialogFactory: DialogFactory<T, R, D>) -> DialogLiveDataMediator<T, R, D>
7983
) {
80-
val liveData = DialogLiveDataMediator(
81-
control = this,
82-
createDialog = createDialog
83-
)
84+
val liveData = dialogLiveDataProvider(this, dialogFactory)
8485
rvmViewComponent.run { liveData.observe { /* empty */ } }
8586
}
8687

87-
private class DialogLiveDataMediator<T : Any, R : Any>(
88+
fun <T : Any, R : Any> DialogControl<T, R>.bindTo(
89+
rvmViewComponent: RvmViewComponent,
90+
dialogFactory: DialogFactory<T, R, Dialog>
91+
) = bindToEx(rvmViewComponent, dialogFactory, ::DialogLiveDataMediatorDialog)
92+
93+
abstract class DialogLiveDataMediator<T : Any, R : Any, D : Any>(
8894
control: DialogControl<T, R>,
89-
createDialog: ActionCreateDialog<T, R>
95+
dialogFactory: DialogFactory<T, R, D>
9096
) : MediatorLiveData<DialogControl.Display<T>>() {
9197

92-
private var dialog: Dialog? = null
98+
private var dialog: D? = null
9399

94100
init {
95101
addSource(control.displayed.liveData) { displayData ->
96102
value = displayData
97103
when (displayData) {
98104
is DialogControl.Display.Displayed -> {
99-
dialog = createDialog(displayData.data, DialogControlResult(control))
100-
dialog?.setOnDismissListener { control.dismiss() }
101-
dialog?.show()
105+
dialog = dialogFactory
106+
.createDialog(
107+
data = displayData.data,
108+
dc = DialogControlResult(control)
109+
).also {
110+
setupOnDismiss(it) { control.dismiss() }
111+
showDialog(it)
112+
}
102113
}
103114
DialogControl.Display.Absent -> closeDialog()
104115
}
105116
}
106117
}
107118

108-
override fun removeObserver(observer: Observer<in DialogControl.Display<T>>) {
119+
final override fun <S : Any?> addSource(source: LiveData<S>, onChanged: Observer<in S>) {
120+
super.addSource(source, onChanged)
121+
}
122+
123+
final override fun removeObserver(observer: Observer<in DialogControl.Display<T>>) {
109124
super.removeObserver(observer)
110125
closeDialog()
111126
}
112127

128+
protected abstract fun setupOnDismiss(dialog: D, dismissAction: () -> Unit)
129+
130+
protected abstract fun showDialog(dialog: D)
131+
132+
protected abstract fun onCloseDialog(dialog: D)
133+
113134
private fun closeDialog() {
114-
dialog?.apply {
115-
setOnDismissListener(null)
116-
dismiss()
117-
}
135+
dialog?.let { onCloseDialog(it) }
118136
dialog = null
119137
}
120138

121139
}
140+
141+
private class DialogLiveDataMediatorDialog<T : Any, R : Any>(
142+
control: DialogControl<T, R>,
143+
dialogFactory: DialogFactory<T, R, Dialog>
144+
) : DialogLiveDataMediator<T, R, Dialog>(control, dialogFactory) {
145+
146+
override fun setupOnDismiss(dialog: Dialog, dismissAction: () -> Unit) {
147+
dialog.setOnDismissListener { dismissAction() }
148+
}
149+
150+
override fun showDialog(dialog: Dialog) {
151+
dialog.show()
152+
}
153+
154+
override fun onCloseDialog(dialog: Dialog) {
155+
dialog.setOnDismissListener(null)
156+
dialog.dismiss()
157+
}
158+
159+
}

0 commit comments

Comments
 (0)