Skip to content

Commit

Permalink
fix: add support for SavedStateHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielcafe committed Aug 25, 2021
1 parent 6e107e7 commit 622c585
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ kotlin.code.style=official

# Maven
GROUP=cafe.adriel.voyager
VERSION_NAME=1.0.0-beta07
VERSION_NAME=1.0.0-beta08

POM_DESCRIPTION=A pragmatic navigation library for Jetpack Compose
POM_INCEPTION_YEAR=2021
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugin-maven = "0.17.0"
kotlin = "1.5.21"
koin = "3.1.2"
appCompat = "1.3.1"
lifecycle = "2.4.0-alpha03"
compose = "1.1.0-alpha02"
composeActivity = "1.3.1"
composeViewModel = "1.0.0-alpha07"
Expand All @@ -20,6 +21,8 @@ plugin-maven = { module = "com.vanniktech:gradle-maven-publish-plugin", version.

koin = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koin" }
appCompat = { module = "androidx.appcompat:appcompat", version.ref = "appCompat" }
lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle" }
lifecycle-savedState = { module = "androidx.lifecycle:lifecycle-viewmodel-savedstate", version.ref = "lifecycle" }
compose-compiler = { module = "androidx.compose.compiler:compiler", version.ref = "compose" }
compose-runtime = { module = "androidx.compose.runtime:runtime", version.ref = "compose" }
compose-runtimeSaveable = { module = "androidx.compose.runtime:runtime-saveable", version.ref = "compose" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.androidx.AndroidScreen
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import org.koin.androidx.viewmodel.ext.android.getViewModel
import org.koin.androidx.compose.getViewModel
import org.koin.core.parameter.parametersOf

data class DetailsScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.transitions.SlideTransition

class BasicNavigationActivity : ComponentActivity() {

@OptIn(ExperimentalAnimationApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -21,11 +18,7 @@ class BasicNavigationActivity : ComponentActivity() {
Log.d("Navigator", "Pop screen #${(currentScreen as BasicNavigationScreen).index}")
true
}
) { navigator ->
SlideTransition(
navigator = navigator
)
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cafe.adriel.voyager.sample.basicNavigation

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -17,7 +16,6 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.LifecycleEffect
import cafe.adriel.voyager.core.screen.Screen
Expand All @@ -32,8 +30,6 @@ data class BasicNavigationScreen(

override val key = uniqueScreenKey

private val colors = listOf(Color.LightGray, Color.Gray, Color.DarkGray)

@Composable
override fun Content() {
LifecycleEffect(
Expand All @@ -49,7 +45,7 @@ data class BasicNavigationScreen(
modifier = Modifier.run {
if (wrapContent) wrapContentHeight()
else fillMaxSize()
}.background(colors[index])
}
) {
Text(
text = "Screen #$index",
Expand Down
2 changes: 2 additions & 0 deletions voyager-androidx/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ android {
dependencies {
api projects.voyagerCore

implementation libs.lifecycle.runtime
implementation libs.lifecycle.savedState
implementation libs.compose.viewModel
implementation libs.compose.runtimeSaveable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cafe.adriel.voyager.androidx

import androidx.lifecycle.ViewModelStoreOwner
import cafe.adriel.voyager.core.hook.HookableScreen
import cafe.adriel.voyager.core.hook.ScreenHookHandler
import cafe.adriel.voyager.core.screen.Screen
Expand All @@ -9,10 +8,10 @@ import cafe.adriel.voyager.core.screen.uniqueScreenKey
public abstract class AndroidScreen :
Screen,
HookableScreen by ScreenHookHandler(),
ViewModelStoreOwner by ScreenViewModelStoreOwner() {
ScreenLifecycleOwner by ScreenLifecycleHolder() {

init {
addHooks(viewModelScreenHooks)
addHooks(screenLifecycleHooks)
}

override val key: String = uniqueScreenKey
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cafe.adriel.voyager.androidx

import androidx.compose.ui.platform.LocalSavedStateRegistryOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
import cafe.adriel.voyager.core.hook.ScreenHook

public val ScreenLifecycleOwner.screenLifecycleHooks: List<ScreenHook>
get() = listOf(
ScreenHook.OnProvide { LocalViewModelStoreOwner provides this },
ScreenHook.OnProvide { LocalSavedStateRegistryOwner provides this },
ScreenHook.OnDispose { viewModelStore.clear() }
)

public interface ScreenLifecycleOwner : LifecycleOwner, ViewModelStoreOwner, SavedStateRegistryOwner

public class ScreenLifecycleHolder : ScreenLifecycleOwner {

private val store = ViewModelStore()

private val registry = LifecycleRegistry(this)

private val controller = SavedStateRegistryController.create(this)

init {
controller.performRestore(null)
}

override fun getViewModelStore(): ViewModelStore = store

override fun getLifecycle(): Lifecycle = registry

override fun getSavedStateRegistry(): SavedStateRegistry = controller.savedStateRegistry
}

This file was deleted.

0 comments on commit 622c585

Please sign in to comment.