test
This commit is contained in:
188
app/src/main/java/com/example/scanwich/SetupScreen.kt
Normal file
188
app/src/main/java/com/example/scanwich/SetupScreen.kt
Normal file
@@ -0,0 +1,188 @@
|
||||
package com.example.scanwich
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.core.content.edit
|
||||
|
||||
@Composable
|
||||
fun SetupScreen(prefs: SharedPreferences, onComplete: () -> Unit) {
|
||||
var age by remember { mutableStateOf(prefs.getInt("age", 25).toString()) }
|
||||
var heightCm by remember { mutableStateOf(prefs.getString("height_cm", "170") ?: "170") }
|
||||
var weight by remember { mutableStateOf(prefs.getString("weight_display", "70") ?: "70") }
|
||||
var isLbs by remember { mutableStateOf(prefs.getBoolean("is_lbs", false)) }
|
||||
var gender by remember { mutableStateOf(prefs.getString("gender", "Homme") ?: "Homme") }
|
||||
var activityLevel by remember { mutableStateOf(prefs.getString("activity_level", "Sédentaire") ?: "Sédentaire") }
|
||||
var goal by remember { mutableStateOf(prefs.getString("goal", "Maintenir le poids") ?: "Maintenir le poids") }
|
||||
var isDiabetic by remember { mutableStateOf(prefs.getBoolean("is_diabetic", false)) }
|
||||
|
||||
val activityLevels = listOf("Sédentaire", "Légèrement actif", "Modérément actif", "Très actif", "Extrêmement actif")
|
||||
val goals = listOf("Maintenir le poids", "Perdre du poids")
|
||||
|
||||
val activityMultipliers = mapOf(
|
||||
"Sédentaire" to 1.2,
|
||||
"Légèrement actif" to 1.375,
|
||||
"Modérément actif" to 1.55,
|
||||
"Très actif" to 1.725,
|
||||
"Extrêmement actif" to 1.9
|
||||
)
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(24.dp)
|
||||
.verticalScroll(rememberScrollState()),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text("Configuration du profil", style = MaterialTheme.typography.headlineLarge)
|
||||
Spacer(Modifier.height(24.dp))
|
||||
|
||||
Text("Votre objectif :", style = MaterialTheme.typography.titleMedium, modifier = Modifier.align(Alignment.Start))
|
||||
goals.forEach { g ->
|
||||
Row(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { goal = g }
|
||||
.padding(vertical = 4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
RadioButton(selected = goal == g, onClick = { goal = g })
|
||||
Text(g)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text("Genre : ", Modifier.width(80.dp))
|
||||
RadioButton(selected = gender == "Homme", onClick = { gender = "Homme" })
|
||||
Text("Homme")
|
||||
Spacer(Modifier.width(16.dp))
|
||||
RadioButton(selected = gender == "Femme", onClick = { gender = "Femme" })
|
||||
Text("Femme")
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = age,
|
||||
onValueChange = { age = it },
|
||||
label = { Text("Âge") },
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = heightCm,
|
||||
onValueChange = { heightCm = it },
|
||||
label = { Text("Taille (cm)") },
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
|
||||
OutlinedTextField(
|
||||
value = weight,
|
||||
onValueChange = { weight = it },
|
||||
label = { Text(if (isLbs) "Poids (lbs)" else "Poids (kg)") },
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
Spacer(Modifier.width(16.dp))
|
||||
Switch(checked = isLbs, onCheckedChange = { isLbs = it })
|
||||
Text(if (isLbs) "lbs" else "kg")
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
Text("Niveau d'activité :", style = MaterialTheme.typography.titleMedium, modifier = Modifier.align(Alignment.Start))
|
||||
activityLevels.forEach { level ->
|
||||
Row(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { activityLevel = level }
|
||||
.padding(vertical = 4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
RadioButton(selected = activityLevel == level, onClick = { activityLevel = level })
|
||||
Text(level)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
|
||||
Checkbox(checked = isDiabetic, onCheckedChange = { isDiabetic = it })
|
||||
Text("Je suis diabétique")
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(32.dp))
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
val ageInt = age.toIntOrNull() ?: 0
|
||||
val height = heightCm.toDoubleOrNull() ?: 0.0
|
||||
var weightKg = weight.toDoubleOrNull() ?: 0.0
|
||||
val weightDisplay = weight
|
||||
if (isLbs) weightKg *= 0.453592
|
||||
|
||||
val bmr = if (gender == "Homme") {
|
||||
(10 * weightKg) + (6.25 * height) - (5 * ageInt) + 5
|
||||
} else {
|
||||
(10 * weightKg) + (6.25 * height) - (5 * ageInt) - 161
|
||||
}
|
||||
|
||||
val multiplier = activityMultipliers[activityLevel] ?: 1.2
|
||||
var targetCals = (bmr * multiplier).toInt()
|
||||
|
||||
if (goal == "Perdre du poids") targetCals -= 500
|
||||
|
||||
val targetCarbs = (targetCals * 0.5 / 4).toInt()
|
||||
val targetProtein = (targetCals * 0.2 / 4).toInt()
|
||||
val targetFat = (targetCals * 0.3 / 9).toInt()
|
||||
|
||||
prefs.edit {
|
||||
putString("target_calories", targetCals.toString())
|
||||
putString("target_carbs", targetCarbs.toString())
|
||||
putString("target_protein", targetProtein.toString())
|
||||
putString("target_fat", targetFat.toString())
|
||||
putString("weight_kg", weightKg.toString())
|
||||
putString("weight_display", weightDisplay)
|
||||
putBoolean("is_lbs", isLbs)
|
||||
putString("height_cm", heightCm)
|
||||
putBoolean("is_diabetic", isDiabetic)
|
||||
putInt("age", ageInt)
|
||||
putString("gender", gender)
|
||||
putString("activity_level", activityLevel)
|
||||
putString("goal", goal)
|
||||
}
|
||||
onComplete()
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(56.dp),
|
||||
enabled = age.isNotBlank() && heightCm.isNotBlank() && weight.isNotBlank()
|
||||
) {
|
||||
Text("Sauvegarder le profil")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user