/* 2023-03-24 File created. Copyright (c) Kari Laitinen This app shows how to use a Switch component in UI. A ViewModel is in use in this app. This is an Android app made of this single file. If you want to test this app, create an Android Compose project using the project name SwitchDemoC and the package name shown below. Then, store this file to the folder where MainActivity.kt is and make the following change to AndroidManifest.xml: android:name=".SwitchDemoCActivity" Some 'warnings': - tab size in this file is 3 spaces - underscores are used in the names invented by the programmer */ package switching.democ import android.os.Bundle import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.viewModels import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.ViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update data class SwitchesState( val upper_switch_state : Boolean = true, val lower_switch_state : Boolean = false, val switching_counter : Int = 0 ) class SwitchesViewModel : ViewModel() { private val _uiState = MutableStateFlow( SwitchesState() ) // asStateFlow() represents this mutable state flow as a read-only state flow. fun consumableState() = _uiState.asStateFlow() fun switch_was_used() { _uiState.update { currentState -> currentState.copy( upper_switch_state = ! currentState.upper_switch_state, lower_switch_state = ! currentState.lower_switch_state, switching_counter = currentState.switching_counter + 1 ) } } } class SwitchDemoCActivity : ComponentActivity() { private val switches_view_model : SwitchesViewModel by viewModels() override fun onCreate( savedInstanceState: Bundle? ) { super.onCreate(savedInstanceState) setContent { val switches_view_state = switches_view_model.consumableState().collectAsState() Column( Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { Spacer(modifier = Modifier.weight( 2F ) ) Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier.weight( 1F ) ) { Box( modifier = Modifier.weight( 4F ), contentAlignment = Alignment.Center ) { Text("Here is a Switch: ", fontSize = 20.sp ) } Box( modifier = Modifier.weight( 1F ), contentAlignment = Alignment.Center ) { Switch( checked = switches_view_state.value.upper_switch_state, onCheckedChange = { switches_view_model.switch_was_used() }) } } Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier.weight( 1F ) ) { Box( modifier = Modifier.weight( 4F ), contentAlignment = Alignment.Center ) { Text("Another Switch: ", fontSize = 20.sp ) } Box( modifier = Modifier.weight( 1F ), contentAlignment = Alignment.Center ) { Switch( checked = switches_view_state.value.lower_switch_state, onCheckedChange = { switches_view_model.switch_was_used() }) } } Box( modifier = Modifier.weight( 1F ), contentAlignment = Alignment.Center ) { Text("" + switches_view_state.value.switching_counter, fontSize = 32.sp ) } Spacer(modifier = Modifier.weight( 2F ) ) } } } }