-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51803a0
commit 2fd7081
Showing
20 changed files
with
196 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
sample/src/main/java/cafe/adriel/voyager/sample/androidNavigation/ListViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cafe.adriel.voyager.sample.androidNavigation | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import java.util.UUID | ||
|
||
class ListViewModel(private val handle: SavedStateHandle) : ViewModel() { | ||
|
||
init { | ||
handle["items"] = (0..100).map { "Item #$it | ${UUID.randomUUID().toString().substringBefore('-')}" } | ||
} | ||
|
||
val items: List<String> | ||
get() = handle["items"] ?: error("Items not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
voyager-androidx/src/main/java/cafe/adriel/voyager/androidx/AndroidScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package cafe.adriel.voyager.androidx | ||
|
||
import cafe.adriel.voyager.core.lifecycle.ScreenLifecycleOwner | ||
import cafe.adriel.voyager.core.lifecycle.ScreenLifecycleProvider | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.core.screen.ScreenKey | ||
import cafe.adriel.voyager.core.screen.uniqueScreenKey | ||
|
||
public abstract class AndroidScreen : Screen, ScreenLifecycleOwner by ScreenLifecycleHolder() { | ||
public abstract class AndroidScreen : Screen, ScreenLifecycleProvider { | ||
|
||
override val key: String = uniqueScreenKey | ||
override val key: ScreenKey = uniqueScreenKey | ||
|
||
override fun getLifecycleOwner(): ScreenLifecycleOwner = ScreenLifecycleHolder.get(key) | ||
} |
77 changes: 77 additions & 0 deletions
77
voyager-androidx/src/main/java/cafe/adriel/voyager/androidx/ScreenLifecycleHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cafe.adriel.voyager.androidx | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalContext | ||
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.lifecycle.ScreenHooks | ||
import cafe.adriel.voyager.core.lifecycle.ScreenLifecycleOwner | ||
import cafe.adriel.voyager.core.screen.ScreenKey | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
public class ScreenLifecycleHolder private constructor( | ||
private val key: ScreenKey | ||
) : ScreenLifecycleOwner, | ||
LifecycleOwner, | ||
ViewModelStoreOwner, | ||
SavedStateRegistryOwner { | ||
|
||
private val registry = LifecycleRegistry(this) | ||
|
||
private val store = ViewModelStore() | ||
|
||
private val controller = SavedStateRegistryController.create(this) | ||
|
||
private val Context.canDispose: Boolean | ||
get() = (this as? Activity)?.isChangingConfigurations?.not() ?: true | ||
|
||
init { | ||
controller.performRestore(null) | ||
} | ||
|
||
@Composable | ||
override fun getHooks(): ScreenHooks { | ||
val context = LocalContext.current | ||
|
||
return ScreenHooks( | ||
providers = listOf( | ||
LocalViewModelStoreOwner provides this, | ||
LocalSavedStateRegistryOwner provides this, | ||
), | ||
disposer = { | ||
if (context.canDispose) { | ||
viewModelStore.clear() | ||
remove(key) | ||
} | ||
} | ||
) | ||
} | ||
|
||
override fun getLifecycle(): Lifecycle = registry | ||
|
||
override fun getViewModelStore(): ViewModelStore = store | ||
|
||
override fun getSavedStateRegistry(): SavedStateRegistry = controller.savedStateRegistry | ||
|
||
internal companion object { | ||
|
||
private val holders = ConcurrentHashMap<ScreenKey, ScreenLifecycleHolder>() | ||
|
||
internal fun get(key: ScreenKey) = | ||
holders.getOrPut(key) { ScreenLifecycleHolder(key) } | ||
|
||
private fun remove(key: ScreenKey) { | ||
holders -= key | ||
} | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
voyager-androidx/src/main/java/cafe/adriel/voyager/androidx/ScreenLifecycleOwner.kt
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
voyager-core/src/main/java/cafe/adriel/voyager/core/hook/Hookable.kt
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
voyager-core/src/main/java/cafe/adriel/voyager/core/hook/ScreenHook.kt
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
voyager-core/src/main/java/cafe/adriel/voyager/core/lifecycle/ScreenHooks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cafe.adriel.voyager.core.lifecycle | ||
|
||
import androidx.compose.runtime.ProvidedValue | ||
|
||
public data class ScreenHooks( | ||
val providers: List<ProvidedValue<*>> = emptyList(), | ||
val disposer: () -> Unit = {} | ||
) { | ||
|
||
internal companion object { | ||
val Empty = ScreenHooks() | ||
} | ||
} |
Oops, something went wrong.