|
@@ -0,0 +1,309 @@
|
|
|
+package com.example.osnov_android.views
|
|
|
+
|
|
|
+import android.widget.Toast
|
|
|
+import androidx.compose.foundation.BorderStroke
|
|
|
+import androidx.compose.foundation.Image
|
|
|
+import androidx.compose.foundation.background
|
|
|
+import androidx.compose.foundation.border
|
|
|
+import androidx.compose.foundation.clickable
|
|
|
+import androidx.compose.foundation.layout.Box
|
|
|
+import androidx.compose.foundation.layout.Column
|
|
|
+import androidx.compose.foundation.layout.Spacer
|
|
|
+import androidx.compose.foundation.layout.fillMaxHeight
|
|
|
+import androidx.compose.foundation.layout.fillMaxSize
|
|
|
+import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
+import androidx.compose.foundation.layout.height
|
|
|
+import androidx.compose.foundation.layout.padding
|
|
|
+import androidx.compose.foundation.layout.size
|
|
|
+import androidx.compose.foundation.layout.width
|
|
|
+import androidx.compose.foundation.shape.CircleShape
|
|
|
+import androidx.compose.foundation.shape.RoundedCornerShape
|
|
|
+import androidx.compose.material.icons.Icons
|
|
|
+import androidx.compose.material.icons.filled.Face
|
|
|
+import androidx.compose.material.icons.filled.KeyboardArrowDown
|
|
|
+import androidx.compose.material.icons.filled.KeyboardArrowUp
|
|
|
+import androidx.compose.material3.Button
|
|
|
+import androidx.compose.material3.ButtonColors
|
|
|
+import androidx.compose.material3.CircularProgressIndicator
|
|
|
+import androidx.compose.material3.DropdownMenu
|
|
|
+import androidx.compose.material3.DropdownMenuItem
|
|
|
+import androidx.compose.material3.Icon
|
|
|
+import androidx.compose.material3.OutlinedTextField
|
|
|
+import androidx.compose.material3.Text
|
|
|
+import androidx.compose.material3.TextFieldDefaults
|
|
|
+import androidx.compose.runtime.Composable
|
|
|
+import androidx.compose.runtime.getValue
|
|
|
+import androidx.compose.runtime.mutableStateOf
|
|
|
+import androidx.compose.runtime.remember
|
|
|
+import androidx.compose.runtime.setValue
|
|
|
+import androidx.compose.ui.Alignment
|
|
|
+import androidx.compose.ui.Modifier
|
|
|
+import androidx.compose.ui.draw.clip
|
|
|
+import androidx.compose.ui.geometry.Size
|
|
|
+import androidx.compose.ui.graphics.Color
|
|
|
+import androidx.compose.ui.layout.ContentScale
|
|
|
+import androidx.compose.ui.layout.onGloballyPositioned
|
|
|
+import androidx.compose.ui.platform.LocalContext
|
|
|
+import androidx.compose.ui.platform.LocalDensity
|
|
|
+import androidx.compose.ui.text.TextStyle
|
|
|
+import androidx.compose.ui.text.font.FontWeight
|
|
|
+import androidx.compose.ui.unit.dp
|
|
|
+import androidx.compose.ui.unit.sp
|
|
|
+import androidx.compose.ui.unit.toSize
|
|
|
+import androidx.navigation.NavController
|
|
|
+import coil.compose.rememberAsyncImagePainter
|
|
|
+import com.example.osnov_android.MainActivityViewModel
|
|
|
+import com.example.osnov_android.models.UserState
|
|
|
+
|
|
|
+@Composable
|
|
|
+fun Updatedatauser(viewModel: MainActivityViewModel, navController: NavController) {
|
|
|
+ val background = Color(0xFF1B1A20)
|
|
|
+ val user by viewModel.user
|
|
|
+ val name = remember { mutableStateOf(user.last().name) }
|
|
|
+ val nameFlag = remember { mutableStateOf(true) }
|
|
|
+ val flag = remember { mutableStateOf(false) }
|
|
|
+ val userState by viewModel.userState
|
|
|
+ val context = LocalContext.current
|
|
|
+ var expanded by remember {
|
|
|
+ mutableStateOf(false)
|
|
|
+ }
|
|
|
+ val list = listOf("Мужской", "Женский")
|
|
|
+
|
|
|
+ var selectedItem by remember {
|
|
|
+ mutableStateOf(
|
|
|
+ if (user.last().id_gender == 1) {
|
|
|
+ "Мужской"
|
|
|
+ } else {
|
|
|
+ "Женский"
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ var textFiledSize by remember {
|
|
|
+ mutableStateOf(Size.Zero)
|
|
|
+ }
|
|
|
+ val icon = if (expanded) {
|
|
|
+ Icons.Filled.KeyboardArrowUp
|
|
|
+ } else {
|
|
|
+ Icons.Filled.KeyboardArrowDown
|
|
|
+ }
|
|
|
+
|
|
|
+ Column(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxSize()
|
|
|
+ .fillMaxHeight()
|
|
|
+ .background(background)
|
|
|
+ .padding(top = 50.dp),
|
|
|
+ horizontalAlignment = Alignment.CenterHorizontally
|
|
|
+ )
|
|
|
+ {
|
|
|
+ Text(
|
|
|
+ text = "ПРОФИЛЬ",
|
|
|
+ fontWeight = FontWeight.Light,
|
|
|
+ fontSize = 24.sp,
|
|
|
+ color = Color(0xFFFFFFFF),
|
|
|
+ letterSpacing = 10.sp
|
|
|
+ )
|
|
|
+ Spacer(Modifier.height(30.dp))
|
|
|
+ Box(modifier = Modifier.height(200.dp).width(200.dp),)
|
|
|
+ {
|
|
|
+ Image(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .size(200.dp)
|
|
|
+ .clip(CircleShape),
|
|
|
+ painter = rememberAsyncImagePainter(viewModel.users.value.last().image),
|
|
|
+ contentDescription = "",
|
|
|
+ contentScale = ContentScale.Crop,
|
|
|
+ alignment = Alignment.Center
|
|
|
+ )
|
|
|
+ }
|
|
|
+ Spacer(Modifier.height(30.dp))
|
|
|
+ Column()
|
|
|
+ {
|
|
|
+ Text(
|
|
|
+ text = "Изменить имя",
|
|
|
+ fontWeight = FontWeight.Medium,
|
|
|
+ fontSize = 16.sp,
|
|
|
+ color = Color(0xFFFFFFFF),
|
|
|
+ letterSpacing = 2.sp
|
|
|
+ )
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(Color(0xFFAAABA7))
|
|
|
+ )
|
|
|
+ {
|
|
|
+ OutlinedTextField(
|
|
|
+ value = name.value,
|
|
|
+ onValueChange = { name.value = it },
|
|
|
+ textStyle = TextStyle(
|
|
|
+ color = Color.Black,
|
|
|
+ fontSize = 15.sp
|
|
|
+ ),
|
|
|
+ colors = TextFieldDefaults.colors(
|
|
|
+ focusedContainerColor = Color(0XFFF5F5F9),
|
|
|
+ focusedIndicatorColor = Color(0xFFEBEBEB),
|
|
|
+ focusedTextColor = Color(0XFF578FFF),
|
|
|
+ disabledIndicatorColor = Color.White,
|
|
|
+ unfocusedIndicatorColor = Color.White,
|
|
|
+ cursorColor = Color(0XFF578FFF),
|
|
|
+ focusedSupportingTextColor = Color(0xFF00000)
|
|
|
+ ),
|
|
|
+ isError = nameFlag.value,
|
|
|
+ modifier = Modifier
|
|
|
+ .background(Color(0XFFF5F5F9))
|
|
|
+ .height(height = 50.dp)
|
|
|
+ .fillMaxWidth(0.5f),
|
|
|
+ placeholder = {
|
|
|
+ Text(
|
|
|
+ modifier = Modifier.fillMaxWidth(),
|
|
|
+ text = "Имя",
|
|
|
+ fontSize = 15.sp,
|
|
|
+ color = Color(0XFF939396)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ Spacer(Modifier.height(30.dp))
|
|
|
+ Column()
|
|
|
+ {
|
|
|
+ Text(
|
|
|
+ text = "Изменить пол",
|
|
|
+ fontWeight = FontWeight.Medium,
|
|
|
+ fontSize = 16.sp,
|
|
|
+ color = Color(0xFFFFFFFF),
|
|
|
+ letterSpacing = 2.sp
|
|
|
+ )
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(Color(0xFFAAABA7))
|
|
|
+ ) {
|
|
|
+
|
|
|
+ OutlinedTextField(value = selectedItem,
|
|
|
+ onValueChange = {
|
|
|
+ selectedItem = it
|
|
|
+ },
|
|
|
+ leadingIcon = {
|
|
|
+ Icon(
|
|
|
+ Icons.Filled.Face,
|
|
|
+ contentDescription = "Пол",
|
|
|
+ )
|
|
|
+ },
|
|
|
+ colors = TextFieldDefaults.colors(
|
|
|
+ unfocusedTextColor = Color.Black,
|
|
|
+ focusedTextColor = Color.Black,
|
|
|
+ disabledTextColor = Color.Black,
|
|
|
+ disabledContainerColor = Color(0XffE4E4E7),
|
|
|
+ focusedContainerColor = Color(0XffE4E4E7),
|
|
|
+ unfocusedContainerColor = Color(0XffE4E4E7)
|
|
|
+
|
|
|
+ ),
|
|
|
+ enabled = false,
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth(0.5f)
|
|
|
+ .height(height = 56.dp)
|
|
|
+ .width(width = 320.dp)
|
|
|
+ .clickable { expanded = !expanded }
|
|
|
+ .onGloballyPositioned { coordinates ->
|
|
|
+ textFiledSize = coordinates.size.toSize()
|
|
|
+ }
|
|
|
+ .border(BorderStroke(0.dp, Color.Transparent)),
|
|
|
+ trailingIcon = {
|
|
|
+ Icon(
|
|
|
+ icon,
|
|
|
+ contentDescription = ""
|
|
|
+ )
|
|
|
+ },
|
|
|
+ placeholder = {
|
|
|
+ Text(
|
|
|
+ modifier = Modifier.fillMaxWidth(),
|
|
|
+ text = "Пол",
|
|
|
+ fontWeight = FontWeight.Normal,
|
|
|
+ fontSize = 15.sp,
|
|
|
+ color = Color.Black
|
|
|
+ )
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+ DropdownMenu(
|
|
|
+ expanded = expanded,
|
|
|
+ onDismissRequest = { expanded = false },
|
|
|
+ modifier = Modifier
|
|
|
+ .width(with(LocalDensity.current) { textFiledSize.width.toDp() })
|
|
|
+ .background(Color(0XffE4E4E7))
|
|
|
+ .border(BorderStroke(0.dp, Color.Transparent))
|
|
|
+ ) {
|
|
|
+ list.forEach { label ->
|
|
|
+ DropdownMenuItem(text = {
|
|
|
+ Text(
|
|
|
+ text = label,
|
|
|
+ fontSize = 15.sp,
|
|
|
+ fontWeight = FontWeight.Normal,
|
|
|
+ color = Color.Black
|
|
|
+
|
|
|
+ )
|
|
|
+ }, onClick = {
|
|
|
+ selectedItem = label
|
|
|
+ expanded = false
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Spacer(Modifier.width(30.dp))
|
|
|
+
|
|
|
+ Button(
|
|
|
+ onClick = {
|
|
|
+ if (selectedItem == "Мужской") {
|
|
|
+ viewModel.updateUser(name.value, 1)
|
|
|
+ } else {
|
|
|
+ viewModel.updateUser(name.value, 2)
|
|
|
+ }
|
|
|
+ flag.value = true
|
|
|
+ },
|
|
|
+ Modifier
|
|
|
+ .height(100.dp)
|
|
|
+ .width(250.dp)
|
|
|
+ .padding(top = 20.dp),
|
|
|
+ shape = RoundedCornerShape(20),
|
|
|
+ colors = ButtonColors(
|
|
|
+ contentColor = Color.White,
|
|
|
+ containerColor = Color(0xFF7DAD01),
|
|
|
+ disabledContentColor = Color.White,
|
|
|
+ disabledContainerColor = Color.LightGray
|
|
|
+ )
|
|
|
+ )
|
|
|
+ {
|
|
|
+ Text(
|
|
|
+ text = "Изменить данные",
|
|
|
+ fontSize = 24.sp,
|
|
|
+ fontWeight = FontWeight.Light
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (flag.value) {
|
|
|
+ when (userState) {
|
|
|
+ is UserState.Loading -> {
|
|
|
+ Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
|
|
+ CircularProgressIndicator()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ is UserState.Success -> {
|
|
|
+ Toast.makeText(context, "Данные изменены", Toast.LENGTH_SHORT).show()
|
|
|
+ flag.value = false
|
|
|
+ }
|
|
|
+
|
|
|
+ is UserState.Error -> {
|
|
|
+ val message = (userState as UserState.Error).message
|
|
|
+ Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
|
|
|
+ flag.value = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|