1
+ package com.example.bmi_viewmodel
2
+
3
+ import android.os.Bundle
4
+ import androidx.activity.ComponentActivity
5
+ import androidx.activity.compose.setContent
6
+ import androidx.activity.viewModels
7
+ import androidx.compose.foundation.layout.Arrangement
8
+ import androidx.compose.foundation.layout.Column
9
+ import androidx.compose.foundation.layout.fillMaxSize
10
+ import androidx.compose.foundation.layout.fillMaxWidth
11
+ import androidx.compose.foundation.layout.padding
12
+ import androidx.compose.foundation.text.KeyboardOptions
13
+ import androidx.compose.material3.MaterialTheme
14
+ import androidx.compose.material3.OutlinedTextField
15
+ import androidx.compose.material3.Surface
16
+ import androidx.compose.material3.Text
17
+ import androidx.compose.material3.Button
18
+ import androidx.compose.runtime.Composable
19
+ import androidx.compose.runtime.getValue
20
+ import androidx.compose.ui.Modifier
21
+ import androidx.compose.ui.res.stringResource
22
+ import androidx.compose.ui.text.input.KeyboardType
23
+ import androidx.compose.ui.text.style.TextAlign
24
+ import androidx.compose.ui.tooling.preview.Preview
25
+ import androidx.compose.ui.unit.dp
26
+ import androidx.compose.ui.unit.sp
27
+ import androidx.compose.runtime.remember
28
+ import androidx.compose.runtime.mutableStateOf
29
+ import androidx.compose.runtime.setValue
30
+ import androidx.compose.ui.text.font.FontWeight
31
+ import androidx.lifecycle.viewmodel.compose.viewModel
32
+ import com.example.bmi_viewmodel.ui.theme.BMIViewModelTheme
33
+
34
+ class MainActivity : ComponentActivity () {
35
+ override fun onCreate (savedInstanceState : Bundle ? ) {
36
+ val viewModel by viewModels<BodyMassViewModel >()
37
+ super .onCreate(savedInstanceState)
38
+ setContent {
39
+ BMIViewModelTheme {
40
+ // A surface container using the 'background' color from the theme
41
+ Surface (
42
+ modifier = Modifier .fillMaxSize(),
43
+ color = MaterialTheme .colorScheme.background
44
+ ) {
45
+ BodyMass (viewModel)
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ @Composable
53
+ fun BodyMass (viewModel : BodyMassViewModel ) {
54
+ var showBMI by remember { mutableStateOf(false ) }
55
+
56
+ Column (
57
+ modifier = Modifier .padding(2 .dp),
58
+ verticalArrangement = Arrangement .spacedBy(8 .dp)
59
+ ) {
60
+ Text (
61
+ text = " Body Mass Index" ,
62
+ fontSize = 24 .sp,
63
+ color = MaterialTheme .colorScheme.primary,
64
+ textAlign = TextAlign .Center ,
65
+ modifier = Modifier
66
+ .fillMaxWidth()
67
+ .padding(top = 16 .dp, bottom = 16 .dp)
68
+ )
69
+ OutlinedTextField (
70
+ value = viewModel.heightInput.value,
71
+ onValueChange = { viewModel.updateHeightInput(it)},
72
+ label = { Text (" Height (cm)" ) },
73
+ singleLine = true ,
74
+ keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
75
+ modifier = Modifier .fillMaxWidth()
76
+ )
77
+ OutlinedTextField (
78
+ value = viewModel.weightInput.value,
79
+ onValueChange = { viewModel.updateWeightInput(it)},
80
+ label = { Text (" Weight (kg)" ) },
81
+ singleLine = true ,
82
+ keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
83
+ modifier = Modifier .fillMaxWidth()
84
+ )
85
+
86
+ Button (
87
+ onClick = { showBMI = true },
88
+ modifier = Modifier
89
+ .padding(10 .dp)
90
+ .fillMaxWidth()
91
+ ) {
92
+ Text (text = " CALCULATE" )
93
+ }
94
+
95
+ if (showBMI) {
96
+ Text (
97
+ text = stringResource(R .string.body_mass_index_is, String .format(" %2f" , viewModel.getBMI().value).replace(' ,' , ' .' )) + viewModel.getBMI().value,
98
+ modifier = Modifier .padding(top = 16 .dp),
99
+ fontSize = 20 .sp
100
+ )
101
+ Text (
102
+ text = " ${viewModel.getBMIStatus().value} " ,
103
+ color = if (viewModel.getBMIStatus().value == " Normal weight" ) MaterialTheme .colorScheme.primary else MaterialTheme .colorScheme.error,
104
+ fontSize = 24 .sp,
105
+ fontWeight = FontWeight .Bold ,
106
+ modifier = Modifier
107
+ .fillMaxWidth()
108
+ .padding(top = 16 .dp, bottom = 16 .dp),
109
+ textAlign = TextAlign .Center
110
+ )
111
+ }
112
+ }
113
+ }
114
+
115
+ @Preview(showBackground = true )
116
+ @Composable
117
+ fun GreetingPreview () {
118
+ BMIViewModelTheme {
119
+ BodyMass (viewModel())
120
+ }
121
+ }
0 commit comments