Skip to content

Commit

Permalink
Update AndroidX lifecycle to 2.6.0-beta01
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-udy committed Feb 9, 2023
1 parent d945979 commit fedf352
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 56 deletions.
2 changes: 1 addition & 1 deletion enro-core/src/main/java/dev/enro/core/NavigationHandle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class TypedNavigationHandleImpl<T : NavigationKey>(
override val key: T get() = navigationHandle.key as? T
?: throw EnroException.IncorrectlyTypedNavigationHandle("TypedNavigationHandle failed to cast key of type ${navigationHandle.key::class.java.simpleName} to ${type.simpleName}")

override fun getLifecycle(): Lifecycle = navigationHandle.lifecycle
override val lifecycle: Lifecycle get() = navigationHandle.lifecycle

override fun executeInstruction(navigationInstruction: NavigationInstruction) = navigationHandle.executeInstruction(navigationInstruction)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,26 @@ internal class ComposableDestinationContextReference(
private val savedState: Bundle? =
parentSavedStateRegistry.consumeRestoredStateForKey(instruction.instructionId)
private val savedStateController = SavedStateRegistryController.create(this)
private val viewModelStore: ViewModelStore = ViewModelStore()

override val viewModelStore: ViewModelStore = ViewModelStore()

@SuppressLint("StaticFieldLeak")
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
override val lifecycle: Lifecycle get() = lifecycleRegistry

private var defaultViewModelFactory: Pair<Int, ViewModelProvider.Factory> =
0 to ViewModelProvider.NewInstanceFactory()

override val defaultViewModelProviderFactory: ViewModelProvider.Factory
get() = defaultViewModelFactory.second

override val defaultViewModelCreationExtras: CreationExtras
get() = MutableCreationExtras().apply {
set(ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY, navigationController.application)
set(SAVED_STATE_REGISTRY_OWNER_KEY, this@ComposableDestinationContextReference)
set(VIEW_MODEL_STORE_OWNER_KEY, this@ComposableDestinationContextReference)
}

init {
destination.contextReference = this
destination.enableSavedStateHandles()
Expand Down Expand Up @@ -86,27 +97,6 @@ internal class ComposableDestinationContextReference(
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
}

override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}

override fun getViewModelStore(): ViewModelStore {
return viewModelStore
}

override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory {
return defaultViewModelFactory.second
}

override fun getDefaultViewModelCreationExtras(): CreationExtras {
return MutableCreationExtras().apply {
set(ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY, navigationController.application)
set(SAVED_STATE_REGISTRY_OWNER_KEY, this@ComposableDestinationContextReference)
set(VIEW_MODEL_STORE_OWNER_KEY, this@ComposableDestinationContextReference)
}
}


override val savedStateRegistry: SavedStateRegistry get() =
savedStateController.savedStateRegistry

Expand Down Expand Up @@ -225,21 +215,17 @@ abstract class ComposableDestination: LifecycleOwner,
override val savedStateRegistry: SavedStateRegistry
get() = contextReference.savedStateRegistry

override fun getLifecycle(): Lifecycle {
return contextReference.lifecycle
}
override val lifecycle: Lifecycle
get() = contextReference.lifecycle

override fun getViewModelStore(): ViewModelStore {
return contextReference.viewModelStore
}
override val viewModelStore: ViewModelStore
get() = contextReference.viewModelStore

override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory {
return contextReference.defaultViewModelProviderFactory
}
override val defaultViewModelProviderFactory: ViewModelProvider.Factory
get() = contextReference.defaultViewModelProviderFactory

override fun getDefaultViewModelCreationExtras(): CreationExtras {
return contextReference.defaultViewModelCreationExtras
}
override val defaultViewModelCreationExtras: CreationExtras
get() = contextReference.defaultViewModelCreationExtras

@Composable
abstract fun Render()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ internal class PreviewNavigationHandle(
handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
}
}
override val lifecycle: Lifecycle
get() = lifecycleRegistry

override fun executeInstruction(navigationInstruction: NavigationInstruction) {

}

override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.enro.core.internal.handle

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.addCallback
import androidx.fragment.app.FragmentActivity
Expand Down Expand Up @@ -31,11 +32,11 @@ internal open class NavigationHandleViewModel(
internal var childContainers = listOf<ChildContainer>()
internal var internalOnCloseRequested: () -> Unit = { close() }

private val lifecycle = LifecycleRegistry(this)
@SuppressLint("StaticFieldLeak")
private val lifecycleRegistry = LifecycleRegistry(this)

override fun getLifecycle(): Lifecycle {
return lifecycle
}
override val lifecycle: Lifecycle
get() = lifecycleRegistry

internal var navigationContext: NavigationContext<*>? = null
set(value) {
Expand All @@ -46,15 +47,15 @@ internal open class NavigationHandleViewModel(
executePendingInstruction()

if (lifecycle.currentState == Lifecycle.State.INITIALIZED) {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
}
}

private fun registerLifecycleObservers(context: NavigationContext<out Any>) {
context.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (event == Lifecycle.Event.ON_DESTROY || event == Lifecycle.Event.ON_CREATE) return
lifecycle.handleLifecycleEvent(event)
lifecycleRegistry.handleLifecycleEvent(event)
}
})
context.lifecycle.onEvent(Lifecycle.Event.ON_DESTROY) {
Expand Down Expand Up @@ -106,7 +107,7 @@ internal open class NavigationHandleViewModel(
}

override fun onCleared() {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
}
}

Expand Down
11 changes: 3 additions & 8 deletions enro-test/src/main/java/dev/enro/test/TestNavigationHandle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class TestNavigationHandle<T : NavigationKey>(

internal var internalOnCloseRequested: () -> Unit = { close() }

override fun getLifecycle(): Lifecycle {
return navigationHandle.lifecycle
}
override val lifecycle: Lifecycle
get() = navigationHandle.lifecycle

val instructions: List<NavigationInstruction>
get() = navigationHandle::class.java.getDeclaredField("instructions").let {
Expand All @@ -59,7 +58,7 @@ fun <T : NavigationKey> createTestNavigationHandle(
private val instructions = mutableListOf<NavigationInstruction>()

@SuppressLint("VisibleForTests")
private val lifecycle = LifecycleRegistry.createUnsafe(this).apply {
override val lifecycle = LifecycleRegistry.createUnsafe(this).apply {
currentState = Lifecycle.State.RESUMED
}

Expand All @@ -76,10 +75,6 @@ fun <T : NavigationKey> createTestNavigationHandle(
navigationHandle.get()?.internalOnCloseRequested?.invoke()
}
}

override fun getLifecycle(): Lifecycle {
return lifecycle
}
}))
return navigationHandle.get()!!
}
Expand Down
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ dependencyResolutionManagement {
library("androidx-activity", "androidx.activity:activity-ktx:1.6.1")
library("compose-activity", "androidx.activity:activity-compose:1.6.1")

library("androidx-lifecycle", "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")
library("compose-viewmodel", "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1")
library("androidx-lifecycle", "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.0-beta01")
library("compose-viewmodel", "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.0-beta01")

library("compose-compiler", "androidx.compose.compiler:compiler:1.4.2")
library("compose-foundation", "androidx.compose.foundation:foundation:1.3.1")
Expand Down

0 comments on commit fedf352

Please sign in to comment.