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(): T { | |
| val context = LocalContext.current | |
| return retain { | |
| val annotation = | |
| T::class.java.getAnnotation(RetainedEntryPoint::class.java) | |
| ?: error("${T::class} must be annotated with @RetainedEntryPoint") | |
| val entryPointClass = annotation.value.java | |
| val entryPoint = EntryPoints.get(context, entryPointClass) as RetainedViewModelEntryPoint<T> |
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 | |
| } |
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 CustomRetainedViewModel @Inject constructor( | |
| private val helloRepository: HelloRepository | |
| ) : RetainedViewModel() { |
NewerOlder