All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.com.arkivanov.decompose.extensions.compose.lifecycle.LifecycleController.kt Maven / Gradle / Ivy

There is a newer version: 3.2.2
Show newest version
package com.arkivanov.decompose.extensions.compose.lifecycle

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.window.WindowState
import com.arkivanov.decompose.ExperimentalDecomposeApi
import com.arkivanov.essenty.lifecycle.LifecycleRegistry
import com.arkivanov.essenty.lifecycle.create
import com.arkivanov.essenty.lifecycle.destroy
import com.arkivanov.essenty.lifecycle.resume
import com.arkivanov.essenty.lifecycle.stop
import kotlinx.coroutines.flow.collect

@ExperimentalDecomposeApi
@Composable
fun LifecycleController(lifecycleRegistry: LifecycleRegistry, windowState: WindowState) {
    LaunchedEffect(lifecycleRegistry, windowState) {
        snapshotFlow(windowState::isMinimized).collect { isMinimized ->
            if (isMinimized) {
                lifecycleRegistry.stop()
            } else {
                lifecycleRegistry.resume()
            }
        }
    }

    DisposableEffect(lifecycleRegistry) {
        lifecycleRegistry.create()
        onDispose(lifecycleRegistry::destroy)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy