Skip to content

Instantly share code, notes, and snippets.

View hrach's full-sized avatar

Jan Škrášek hrach

View GitHub Profile
@hrach
hrach / await.kt
Created July 30, 2021 22:08
Difference between MutableSharedFlow() & Channel()
class Foo1 {
private val unpause = MutableSharedFlow<Unit>()
suspend fun await() {
delay(1000)
unpause.first()
}
fun unpause() {
unpause.tryEmit(Unit)
}
}
@hrach
hrach / SavedMutableStateFlow.kt
Created November 1, 2021 13:04
savedMutableStateFlow
fun <T> ViewModel<*>.savedMutableStateFlow(
initialValue: T,
key: String? = null,
): ReadOnlyProperty<ViewModel<*>, MutableStateFlow<T>> {
var mutableStateFlow: MutableStateFlow<T>? = null
return ReadOnlyProperty { _, property ->
if (mutableStateFlow != null) {
return@ReadOnlyProperty mutableStateFlow!!
}
@hrach
hrach / 1-Destinations.kt
Created October 2, 2022 15:02
Navigation Compose Typed article
import com.kiwi.navigationcompose.typed.Destination
import kotlinx.serialization.Serializable
sealed interface Destinations : Destination {
@Serializable
object BookingList: Destinations
@Serializable
data class BookingDetail(
val bookingId: Long,
@hrach
hrach / BaseViewModel.kt
Last active February 18, 2025 17:24
Tests for Kotlin Flow ViewModel tips - https://hrach.dev/posts/kotlin-flow-tips-for-viewmodel/
package dev.hrach.examples
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.serialization.saved
import androidx.lifecycle.viewModelScope
import androidx.savedstate.serialization.serializers.MutableStateFlowSerializer
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow