Это из документации гугла data class ExampleUiState( val dataToDisplayOnScreen: List<Example> = emptyList(), val userMessages: List<Message> = emptyList(), val loading: Boolean = false ) class ExampleViewModel( private val repository: MyRepository, private val savedState: SavedStateHandle ) : ViewModel() { var uiState by mutableStateOf(ExampleUiState()) private set // Business logic fun somethingRelatedToBusinessLogic() { /* ... */ } } @Composable fun ExampleScreen(viewModel: ExampleViewModel = viewModel()) { val uiState = viewModel.uiState /* ... */ ExampleReusableComponent( someData = uiState.dataToDisplayOnScreen, onDoSomething = { viewModel.somethingRelatedToBusinessLogic() } ) } @Composable fun ExampleReusableComponent(someData: Any, onDoSomething: () -> Unit) { /* ... */ Button(onClick = onDoSomething) { Text("Do something") } }
Можно
Обсуждают сегодня