test
This commit is contained in:
109
app/src/main/java/com/example/scanwich/SettingsScreen.kt
Normal file
109
app/src/main/java/com/example/scanwich/SettingsScreen.kt
Normal file
@@ -0,0 +1,109 @@
|
||||
package com.example.scanwich
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Edit
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.edit
|
||||
import androidx.core.net.toUri
|
||||
|
||||
@Composable
|
||||
fun SettingsScreen(prefs: SharedPreferences, onLogout: () -> Unit, onProfileUpdated: () -> Unit) {
|
||||
var isEditing by remember { mutableStateOf(false) }
|
||||
val context = LocalContext.current
|
||||
|
||||
var stravaClientId by remember { mutableStateOf(prefs.getString("strava_client_id", "") ?: "") }
|
||||
var stravaClientSecret by remember { mutableStateOf(prefs.getString("strava_client_secret", "") ?: "") }
|
||||
val isStravaConnected = prefs.contains("strava_token")
|
||||
|
||||
if (isEditing) {
|
||||
SetupScreen(prefs) {
|
||||
isEditing = false
|
||||
onProfileUpdated()
|
||||
}
|
||||
} else {
|
||||
val targetCals = prefs.getString("target_calories", "0")
|
||||
Column(modifier = Modifier.fillMaxSize().padding(16.dp).verticalScroll(rememberScrollState())) {
|
||||
Text("Mon Profil", style = MaterialTheme.typography.headlineMedium)
|
||||
Spacer(Modifier.height(16.dp))
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
ProfileItem("Objectif", prefs.getString("goal", "") ?: "")
|
||||
ProfileItem("Cible Calorique", "$targetCals kcal")
|
||||
ProfileItem("Diabétique", if (prefs.getBoolean("is_diabetic", false)) "Oui" else "Non")
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Button(onClick = { isEditing = true }, modifier = Modifier.fillMaxWidth()) { Icon(Icons.Default.Edit, null); Text(" Modifier le profil") }
|
||||
|
||||
Spacer(Modifier.height(32.dp))
|
||||
Text("Configuration Strava", style = MaterialTheme.typography.titleMedium)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = stravaClientId,
|
||||
onValueChange = { stravaClientId = it; prefs.edit { putString("strava_client_id", it) } },
|
||||
label = { Text("Strava Client ID") },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
OutlinedTextField(
|
||||
value = stravaClientSecret,
|
||||
onValueChange = { stravaClientSecret = it; prefs.edit { putString("strava_client_secret", it) } },
|
||||
label = { Text("Strava Client Secret") },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
|
||||
Spacer(Modifier.height(8.dp))
|
||||
|
||||
if (!isStravaConnected) {
|
||||
Button(
|
||||
onClick = {
|
||||
if (stravaClientId.isBlank() || stravaClientSecret.isBlank()) {
|
||||
Toast.makeText(context, "Entrez votre ID et Secret Client", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
val intent = Intent(Intent.ACTION_VIEW, "https://www.strava.com/oauth/mobile/authorize?client_id=$stravaClientId&redirect_uri=coloricam://localhost&response_type=code&approval_prompt=auto&scope=read,activity:read_all".toUri())
|
||||
context.startActivity(intent)
|
||||
}
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Connecter Strava")
|
||||
}
|
||||
} else {
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
prefs.edit {
|
||||
remove("strava_token")
|
||||
remove("strava_refresh_token")
|
||||
}
|
||||
Toast.makeText(context, "Strava déconnecté", Toast.LENGTH_SHORT).show()
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Déconnecter Strava (Connecté)")
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(32.dp))
|
||||
Button(onClick = onLogout, colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.error), modifier = Modifier.fillMaxWidth()) { Text("Déconnexion") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ProfileItem(label: String, value: String) {
|
||||
Row(modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp), horizontalArrangement = Arrangement.SpaceBetween) {
|
||||
Text(label, fontWeight = FontWeight.Bold); Text(value)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user