Skip to content

Commit

Permalink
fix: provide LocalLifecycleOwner and emit lifecycle states
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielcafe committed Apr 30, 2022
1 parent dcb44b0 commit 437a650
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
Expand Down Expand Up @@ -39,12 +40,14 @@ public class AndroidScreenLifecycleOwner private constructor() :
if (controller.savedStateRegistry.isRestored.not()) {
controller.performRestore(null)
}
initStates.forEach { registry.currentState = it }
}

override fun onDispose(screen: Screen) {
val context = atomicContext.getAndSet(null) ?: return
if (context is Activity && context.isChangingConfigurations) return
viewModelStore.clear()
disposeStates.forEach { registry.currentState = it }
}

@Composable
Expand All @@ -54,6 +57,7 @@ public class AndroidScreenLifecycleOwner private constructor() :
return remember(this) {
ScreenLifecycleHooks(
providers = listOf(
LocalLifecycleOwner provides this,
LocalViewModelStoreOwner provides this,
LocalSavedStateRegistryOwner provides this,
)
Expand All @@ -69,6 +73,17 @@ public class AndroidScreenLifecycleOwner private constructor() :

public companion object {

private val initStates = arrayOf(
Lifecycle.State.INITIALIZED,
Lifecycle.State.CREATED,
Lifecycle.State.STARTED,
Lifecycle.State.RESUMED
)

private val disposeStates = arrayOf(
Lifecycle.State.DESTROYED
)

public fun get(screen: Screen): ScreenLifecycleOwner =
ScreenLifecycleStore.get(screen) { AndroidScreenLifecycleOwner() }
}
Expand Down

0 comments on commit 437a650

Please sign in to comment.