Yokan / rebuild
Course 1 8624561

NovelContentLinks.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/novel/NovelContentLinks.kt

implementation42 lines
1package eu.friedcutlet.yokan.ui.screens.novel23import androidx.compose.runtime.Composable4import androidx.compose.runtime.DisposableEffect5import androidx.compose.runtime.getValue6import androidx.compose.runtime.remember7import androidx.compose.runtime.rememberCoroutineScope8import androidx.compose.runtime.rememberUpdatedState9import androidx.lifecycle.Lifecycle10import androidx.lifecycle.compose.LocalLifecycleOwner11import eu.friedcutlet.yokan.services.extensions.ProfileReaderContent12import kotlinx.coroutines.Job1314/** Parsed links retain the chapter and screen which created them. */15@Composable16internal fun rememberNovelOpenLink(17    content: ProfileReaderContent,18    chapterId: Long,19    onOpenLink: (String) -> Unit,20): (String) -> Unit {21    val scope = rememberCoroutineScope()22    val lifecycle = LocalLifecycleOwner.current.lifecycle23    val binding = remember(content, chapterId, lifecycle) {24        NovelContentLinkBinding(content, Job(scope.coroutineContext[Job])) {25            lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)26        }27    }28    val currentOpenLink by rememberUpdatedState(onOpenLink)29    DisposableEffect(binding) { onDispose { binding.close() } }30    return remember(binding) {31        { link -> if (binding.active()) currentOpenLink(link) }32    }33}3435internal class NovelContentLinkBinding(36    private val content: ProfileReaderContent,37    private val job: Job,38    private val isScreenActive: () -> Boolean,39) {40    fun active(): Boolean = job.isActive && content.isAvailable && isScreenActive()41    fun close() = job.cancel()42}43

Search chapters, reference and source filenames.