mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2026-09-16 05:04:51 +00:00
Compare commits
10 Commits
0.20.14
...
22d7ac3e22
| Author | SHA1 | Date | |
|---|---|---|---|
| 22d7ac3e22 | |||
| 9b30c3a155 | |||
| 915bac64b1 | |||
| 9d2b50e55d | |||
| bde100f63d | |||
| 05b035a13d | |||
| eefb56bed7 | |||
| fcc0dc4189 | |||
| 47ff20317f | |||
| 1558b9103d |
12
CHANGELOG.md
12
CHANGELOG.md
@@ -1,5 +1,17 @@
|
||||
# Changelog
|
||||
|
||||
## 0.20.16
|
||||
|
||||
* `Coroutines`:
|
||||
* Add `SpecialStateFlow`
|
||||
|
||||
## 0.20.15
|
||||
|
||||
* `Versions`:
|
||||
* `Kotlin`: `1.9.20` -> `1.9.21`
|
||||
* `KSLog`: `1.3.0` -> `1.3.1`
|
||||
* `Compose`: `1.5.10` -> `1.5.11`
|
||||
|
||||
## 0.20.14
|
||||
|
||||
* `Versions`:
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package dev.inmo.micro_utils.coroutines.compose
|
||||
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
|
||||
import dev.inmo.micro_utils.coroutines.doInUI
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
class FlowState<T>(
|
||||
private val state: MutableState<T>,
|
||||
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main)
|
||||
) : MutableState<T>,
|
||||
SpecialMutableStateFlow.Default<T>(state.value, internalScope) {
|
||||
override var value: T
|
||||
get() = super.value
|
||||
set(value) {
|
||||
tryEmit(value)
|
||||
}
|
||||
|
||||
override fun component1(): T = state.component1()
|
||||
|
||||
override fun component2(): (T) -> Unit = state.component2()
|
||||
|
||||
private val stateState = derivedStateOf {
|
||||
if (value != state.value) {
|
||||
value = state.value
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun onChange(value: T) {
|
||||
super.onChange(value)
|
||||
|
||||
if (state.value != value) {
|
||||
doInUI {
|
||||
state.value = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> MutableState<T>.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope)
|
||||
@@ -0,0 +1,59 @@
|
||||
package dev.inmo.micro_utils.coroutines
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.FlowCollector
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
interface SpecialMutableStateFlow<T> : StateFlow<T>, FlowCollector<T>, MutableSharedFlow<T> {
|
||||
open class Default<T>(
|
||||
initialValue: T,
|
||||
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : SpecialMutableStateFlow<T> {
|
||||
protected val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
||||
replay = 0,
|
||||
extraBufferCapacity = 1,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||
)
|
||||
protected val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
||||
replay = 1,
|
||||
extraBufferCapacity = 1,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||
)
|
||||
|
||||
protected var _value: T = initialValue
|
||||
override val value: T
|
||||
get() = _value
|
||||
protected open suspend fun onChange(value: T) {
|
||||
_value = value
|
||||
publicSharedFlow.emit(value)
|
||||
}
|
||||
protected val job = internalSharedFlow.subscribeSafelyWithoutExceptions(internalScope) {
|
||||
if (_value != it) {
|
||||
onChange(it)
|
||||
}
|
||||
}
|
||||
|
||||
override val replayCache: List<T>
|
||||
get() = publicSharedFlow.replayCache
|
||||
override val subscriptionCount: StateFlow<Int>
|
||||
get() = publicSharedFlow.subscriptionCount
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
override fun resetReplayCache() = publicSharedFlow.resetReplayCache()
|
||||
|
||||
override fun tryEmit(value: T): Boolean {
|
||||
return internalSharedFlow.tryEmit(value)
|
||||
}
|
||||
|
||||
override suspend fun emit(value: T) {
|
||||
internalSharedFlow.emit(value)
|
||||
}
|
||||
|
||||
override suspend fun collect(collector: FlowCollector<T>) = publicSharedFlow.collect(collector)
|
||||
}
|
||||
}
|
||||
@@ -15,5 +15,5 @@ crypto_js_version=4.1.1
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.20.14
|
||||
android_code_version=220
|
||||
version=0.20.16
|
||||
android_code_version=222
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
[versions]
|
||||
|
||||
kt = "1.9.20"
|
||||
kt = "1.9.21"
|
||||
kt-serialization = "1.6.1"
|
||||
kt-coroutines = "1.7.3"
|
||||
|
||||
kslog = "1.3.0"
|
||||
kslog = "1.3.1"
|
||||
|
||||
jb-compose = "1.5.10"
|
||||
jb-exposed = "0.44.1"
|
||||
jb-compose = "1.5.11"
|
||||
jb-exposed = "0.45.0"
|
||||
jb-dokka = "1.9.10"
|
||||
|
||||
korlibs = "4.0.10"
|
||||
@@ -21,8 +21,8 @@ koin = "3.5.0"
|
||||
|
||||
okio = "3.6.0"
|
||||
|
||||
ksp = "1.9.20-1.0.14"
|
||||
kotlin-poet = "1.14.2"
|
||||
ksp = "1.9.21-1.0.15"
|
||||
kotlin-poet = "1.15.1"
|
||||
|
||||
versions = "0.50.0"
|
||||
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package dev.inmo.micro_utils.matrix
|
||||
|
||||
class MatrixBuilder<T> {
|
||||
open class MatrixBuilder<T> {
|
||||
private val mutMatrix: MutableList<List<T>> = ArrayList()
|
||||
val matrix: Matrix<T>
|
||||
get() = mutMatrix
|
||||
|
||||
fun row(t: List<T>) = mutMatrix.add(t)
|
||||
fun add(t: List<T>) = mutMatrix.add(t)
|
||||
operator fun List<T>.unaryPlus() = row(this)
|
||||
operator fun plus(t: List<T>) = add(t)
|
||||
operator fun T.unaryPlus() = add(listOf(this))
|
||||
}
|
||||
|
||||
fun <T> MatrixBuilder<T>.row(block: RowBuilder<T>.() -> Unit) = +RowBuilder<T>().also(block).row
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package dev.inmo.micro_utils.matrix
|
||||
|
||||
class RowBuilder<T> {
|
||||
open class RowBuilder<T> {
|
||||
private val mutRow: MutableList<T> = ArrayList()
|
||||
val row: Row<T>
|
||||
get() = mutRow
|
||||
|
||||
fun column(t: T) = mutRow.add(t)
|
||||
fun add(t: T) = mutRow.add(t)
|
||||
operator fun T.unaryPlus() = column(this)
|
||||
fun column(t: T) = mutRow.add(t)
|
||||
}
|
||||
|
||||
fun <T> row(block: RowBuilder<T>.() -> Unit): List<T> = RowBuilder<T>().also(block).row
|
||||
|
||||
Reference in New Issue
Block a user