This file contains hidden or 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
| class RetainDecorator<T : Any>( | |
| retainedValuesStoreRegistry: RetainedValuesStoreRegistry, | |
| ) : NavEntryDecorator<T>( | |
| decorate = { entry -> | |
| logDebug { "Retaining ${entry.contentKey}" } | |
| retainedValuesStoreRegistry.LocalRetainedValuesStoreProvider(entry.contentKey) { | |
| entry.Content() | |
| } | |
| }, |
This file contains hidden or 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
| @Composable | |
| fun GameScreen( | |
| gameViewModel: GameViewModel = viewModel() | |
| ) { | |
| // ... | |
| } |
This file contains hidden or 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
| @Composable | |
| fun FirstScreen() { | |
| val customRetainedViewModel = retain { | |
| CustomRetainedViewModel() | |
| } | |
| val state by customRetainedViewModel.state.collectAsStateWithLifecycle() | |
| // .... | |
| } |
This file contains hidden or 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
| @Composable | |
| fun FirstScreen() { | |
| val customRetainedViewModel = rememberRetainedViewModel<CustomRetainedViewModel>() | |
| val state by customRetainedViewModel.state.collectAsStateWithLifecycle() | |
| // .... rest of the owl | |
| } |
This file contains hidden or 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
| @Composable | |
| inline fun <reified T : RetainedViewModel> rememberRetainedViewModel(noinline factory: (Context) -> T): T { | |
| val context = LocalContext.current | |
| return retain { factory(context) } | |
| } | |
| // ..compose layer | |
| val customRetainedViewModel = rememberRetainedViewModel { context -> | |
| EntryPoints.get(context, CustomRetainedViewModelEntryPoint::class.java).customRetainedViewModel() | |
| } |
This file contains hidden or 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
| @EntryPoint | |
| @InstallIn(ActivityComponent::class) | |
| interface CustomRetainedViewModelEntryPoint : RetainedViewModelEntryPoint<CustomRetainedViewModel> | |
| @RetainedEntryPoint(CustomRetainedViewModelEntryPoint::class) | |
| class CustomRetainedViewModel @Inject constructor( | |
| // ... | |
| ) : RetainedViewModel() { | |
| // ... | |
| } |
This file contains hidden or 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
| @Target(AnnotationTarget.CLASS) | |
| @Retention(AnnotationRetention.RUNTIME) | |
| annotation class RetainedEntryPoint( | |
| val value: KClass<out Any>, | |
| ) |
This file contains hidden or 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
| interface RetainedViewModelEntryPoint<T : RetainedViewModel> { | |
| fun create(): T | |
| } | |
| @EntryPoint | |
| @InstallIn(ActivityComponent::class) | |
| interface CustomRetainedViewModelEntryPoint : RetainedViewModelEntryPoint<CustomRetainedViewModel> |
This file contains hidden or 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
| @EntryPoint | |
| @InstallIn(ActivityComponent::class) | |
| interface CustomRetainedViewModelEntryPoint { | |
| fun customRetainedViewModel(): CustomRetainedViewModel | |
| } | |
| @Composable | |
| fun rememberCustomRetainedViewModel(): CustomRetainedViewModel { | |
| val context = LocalContext.current | |
| return retain { | |
| val entryPoint = EntryPoints.get(context, CustomRetainedViewModelEntryPoint::class.java) |
This file contains hidden or 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
| interface RetainedViewModelEntryPoint<T : RetainedViewModel> { | |
| fun create(): T | |
| } |
NewerOlder