-
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 15, 2020
1 parent
089f556
commit fab1964
Showing
7 changed files
with
95 additions
and
63 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
20 changes: 20 additions & 0 deletions
20
ui-generator-base/src/main/java/ru/impression/ui_generator_base/ClearableCoroutineScope.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,20 @@ | ||
package ru.impression.ui_generator_base | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
interface ClearableCoroutineScope : CoroutineScope { | ||
fun clear() | ||
} | ||
|
||
class ClearableCoroutineScopeImpl(coroutineContext: CoroutineContext) : | ||
ClearableCoroutineScope { | ||
|
||
override var coroutineContext: CoroutineContext = coroutineContext + Job() | ||
|
||
override fun clear() { | ||
coroutineContext[Job]?.cancel() | ||
?.also { coroutineContext = coroutineContext.minusKey(Job) + Job() } | ||
} | ||
} |
16 changes: 6 additions & 10 deletions
16
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 |
---|---|---|
@@ -1,31 +1,27 @@ | ||
package ru.impression.ui_generator_base | ||
|
||
import androidx.annotation.CallSuper | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlin.properties.ReadWriteProperty | ||
|
||
abstract class CoroutineViewModel : ComponentViewModel(), | ||
CoroutineScope by CoroutineScope(Dispatchers.IO) { | ||
ClearableCoroutineScope by ClearableCoroutineScopeImpl(Dispatchers.IO) { | ||
|
||
protected fun <T> state( | ||
initialValue: Deferred<T>, | ||
getInitialValue: suspend () -> T, | ||
immediatelyBindChanges: Boolean = false, | ||
onChanged: ((T?) -> Unit)? = null | ||
): ReadWriteProperty<CoroutineViewModel, T?> = | ||
ObservableImpl(this, null, initialValue, immediatelyBindChanges, onChanged) | ||
ObservableImpl(this, null, getInitialValue, immediatelyBindChanges, onChanged) | ||
|
||
protected fun <T> observable( | ||
initialValue: Deferred<T>, | ||
getInitialValue: suspend () -> T, | ||
onChanged: ((T?) -> Unit)? = null | ||
): ReadWriteProperty<CoroutineViewModel, T?> = | ||
ObservableImpl(this, null, initialValue, null, onChanged) | ||
|
||
ObservableImpl(this, null, getInitialValue, null, onChanged) | ||
|
||
@CallSuper | ||
override fun onCleared() { | ||
cancel() | ||
clear() | ||
} | ||
} |
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