-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lunkov_A@utkonos.ru
authored and
Lunkov_A@utkonos.ru
committed
Jul 11, 2020
1 parent
c4ab99d
commit c206970
Showing
9 changed files
with
178 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
54 changes: 54 additions & 0 deletions
54
ui-generator-base/src/main/java/ru/impression/ui_generator_base/CoroutineViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ru.impression.ui_generator_base | ||
|
||
import androidx.annotation.CallSuper | ||
import kotlinx.coroutines.* | ||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KMutableProperty1 | ||
import kotlin.reflect.jvm.isAccessible | ||
|
||
abstract class CoroutineViewModel : ComponentViewModel(), | ||
CoroutineScope by CoroutineScope(Dispatchers.IO) { | ||
|
||
protected fun <T> state( | ||
initialValue: Deferred<T>, | ||
immediatelyBindChanges: Boolean = false, | ||
onChanged: ((T?) -> Unit)? = null | ||
): ReadWriteProperty<ComponentViewModel, T?> = | ||
CoroutineObservableImpl(this, initialValue, immediatelyBindChanges, onChanged) | ||
|
||
protected fun <T> observable( | ||
initialValue: Deferred<T>, | ||
onChanged: ((T?) -> Unit)? = null | ||
): ReadWriteProperty<ComponentViewModel, T?> = | ||
CoroutineObservableImpl(this, initialValue, null, onChanged) | ||
|
||
internal class CoroutineObservableImpl<T>( | ||
parent: CoroutineViewModel, | ||
initialValue: Deferred<T>, | ||
immediatelyBindChanges: Boolean?, | ||
onChanged: ((T?) -> Unit)? | ||
) : ObservableImpl<T?>(parent, null, immediatelyBindChanges, onChanged) { | ||
|
||
@Volatile | ||
internal var isInitializing = true | ||
|
||
init { | ||
parent.launch { | ||
val result = initialValue.await() | ||
(parent::class.members.firstOrNull { | ||
it.isAccessible = true | ||
it is KMutableProperty1<*, *> && (it as KMutableProperty1<CoroutineViewModel, *>) | ||
.getDelegate(parent) === this@CoroutineObservableImpl | ||
} as KMutableProperty1<CoroutineViewModel, T>?) | ||
?.set(parent, result) | ||
isInitializing = false | ||
immediatelyBindChanges?.let { parent.onStateChanged(it) } | ||
} | ||
} | ||
} | ||
|
||
@CallSuper | ||
override fun onCleared() { | ||
cancel() | ||
} | ||
} |
This file contains 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