Skip to content

Commit

Permalink
SharedFlow.subscribe() collects only when lifecycle is STARTED
Browse files Browse the repository at this point in the history
  • Loading branch information
Tweener committed Nov 25, 2024
1 parent dbd02e1 commit f68f903
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion czan/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ kotlin {
implementation(compose.runtime)
implementation(compose.components.resources)
implementation(libs.compose.multiplatform.material3)
implementation(libs.compose.multiplatform.lifecycle)

// Coil (Image fetcher)
implementation(libs.bundles.coil)
Expand All @@ -120,7 +121,6 @@ kotlin {
api(compose.preview)
api(compose.uiTooling)
implementation(libs.android.activity.compose)
implementation(libs.android.lifecycle.compose)

// Image fetcher
implementation(libs.ktor.client.android) // HTTPClient to use with Coil to fetch images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.tweener.czan._internal.kotlinextensions

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.flow.SharedFlow

/**
Expand All @@ -11,9 +14,11 @@ import kotlinx.coroutines.flow.SharedFlow

@Composable
inline fun <T> SharedFlow<T>.subscribe(crossinline action: (T) -> Unit) {
with(this) {
LaunchedEffect(this) {
this@with.collect { action(it) }
val lifecycleOwner = LocalLifecycleOwner.current

LaunchedEffect(this) {
lifecycleOwner.repeatOnLifecycle(state = Lifecycle.State.STARTED) {
this@subscribe.collect { action(it) }
}
}
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ shimmer = "1.3.0"
cupertinoCompose = "0.1.0-alpha04"
ktor = "3.0.1"
coil = "3.0.2"
composeLifecycle = "2.8.4"

# Tweener
tweenerBom = "2.1.1"

# Android
androidDesugarJdkLibs = "2.1.3"
androidActivityCompose = "1.9.3"
androidLifecycleCompose = "2.8.7"

[libraries]
shimmer = { group = "com.valentinilk.shimmer", name = "compose-shimmer", version.ref = "shimmer" }
cupertino-compose = { group = "io.github.alexzhirkevich", name = "cupertino-adaptive", version.ref = "cupertinoCompose" }
compose-multiplatform-material3 = { group = "org.jetbrains.compose.material3", name = "material3", version.ref = "composeMultiplatform" }
compose-multiplatform-lifecycle = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "composeLifecycle" }

# Tweener
tweener-bom = { group = "io.github.tweener", name = "kmp-bom", version.ref = "tweenerBom" }
Expand All @@ -38,7 +39,6 @@ ktor-client-ios = { group = "io.ktor", name = "ktor-client-ios", version.ref = "
# Android
android-desugarjdklibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" }
android-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidActivityCompose" }
android-lifecycle-compose = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidLifecycleCompose" }

[bundles]
coil = ["coil-compose", "coil-network-ktor"]
Expand Down

0 comments on commit f68f903

Please sign in to comment.