Аккаунт редактируется.

This commit is contained in:
ElEgEv 2023-11-27 19:12:50 +04:00
parent 8ff030127a
commit 434cb03cac
7 changed files with 57 additions and 20 deletions

Binary file not shown.

View File

@ -20,7 +20,9 @@ class UserEditViewModel(
var userUiState by mutableStateOf(UserUiState())
private set
private val userUid: Long = checkNotNull(savedStateHandle["id"])
//private val userUid: Long = checkNotNull(savedStateHandle["id"])
private val userUid: Long = 100L
init {
viewModelScope.launch {

View File

@ -26,7 +26,19 @@ data class User (
email: String,
password: String,
balance: Int
) : this(100L, nickname, email, password, balance)
) : this(0L, nickname, email, password, balance)
companion object {
fun getUser(index: Long = 0L): User {
return User(
index,
"3tankista73",
"egor@mail.ru",
"1234567890!",
10000000
)
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true

View File

@ -32,6 +32,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -55,10 +56,14 @@ import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ru.ulstu.`is`.pmu.R
import ru.ulstu.`is`.pmu.composeui.navigation.Screen
import ru.ulstu.`is`.pmu.tank.composeui.edit.UserDetails
import ru.ulstu.`is`.pmu.tank.composeui.edit.UserEditViewModel
import ru.ulstu.`is`.pmu.tank.composeui.edit.UserUiState
import ru.ulstu.`is`.pmu.tank.composeui.edit.toUiState
import ru.ulstu.`is`.pmu.tank.database.AppDatabase
import ru.ulstu.`is`.pmu.tank.model.User
import ru.ulstu.`is`.pmu.ui.AppViewModelProvider
@ -68,19 +73,32 @@ import ru.ulstu.`is`.pmu.ui.theme.CustomRed
import ru.ulstu.`is`.pmu.ui.theme.CustomYellow
import ru.ulstu.`is`.pmu.ui.theme.PmudemoTheme
@OptIn(ExperimentalMaterial3Api::class)
//теперь стартует здесь
@Composable
fun Account(
navController: NavController,
viewModel: UserEditViewModel = viewModel(factory = AppViewModelProvider.Factory)
navController: NavController,
viewModel: UserEditViewModel = viewModel(factory = AppViewModelProvider.Factory)
){
val coroutineScope = rememberCoroutineScope()
Account(
userUiState = viewModel.userUiState,
onClick = {
coroutineScope.launch {
viewModel.saveUser()
navController.popBackStack()
}
},
onUpdate = viewModel::updateUiState
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun Account(
userUiState: UserUiState,
onClick: () -> Unit,
onUpdate: (UserDetails) -> Unit
) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
val currentScreen = currentDestination?.route?.let { Screen.getItem(it) }
val userUiState = viewModel.userUiState
//работа с БД
//для работы текстовых полей
var nickname by remember { mutableStateOf(userUiState.userDetails.nickname) }
@ -90,7 +108,9 @@ fun Account(
Column(
verticalArrangement = Arrangement.spacedBy(35.dp),
modifier = Modifier.fillMaxHeight(1f).padding(0.dp, 15.dp)
modifier = Modifier
.fillMaxHeight(1f)
.padding(0.dp, 15.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
@ -124,21 +144,21 @@ fun Account(
Column {
TextField(
value = userUiState.userDetails.nickname,
onValueChange = { nickname = it },
onValueChange = { onUpdate(userUiState.userDetails.copy(nickname = it)) },
modifier = Modifier
.width(200.dp),
)
Spacer(Modifier.height(10.dp))
TextField(
value = userUiState.userDetails.password,
onValueChange = { password = it },
onValueChange = { onUpdate(userUiState.userDetails.copy(password = it)) },
modifier = Modifier
.width(200.dp),
)
Spacer(Modifier.height(10.dp))
TextField(
value = userUiState.userDetails.balance.toString(),
onValueChange = { balance = it },
onValueChange = { onUpdate(userUiState.userDetails.copy(balance = it.toInt())) },
modifier = Modifier
.width(200.dp),
colors = TextFieldDefaults.textFieldColors(
@ -165,7 +185,7 @@ fun Account(
containerColor = CustomRed,
contentColor = Color.White
),
onClick = { navController.navigate(Screen.TankList.route) }) {
onClick = { onClick }) {
//"${student.firstName} ${student.lastName}"
Text(text = stringResource(id = R.string.save_account_button), fontSize = 20.sp, fontWeight = FontWeight.Bold)
}
@ -177,13 +197,16 @@ fun Account(
@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun AccountPreview() {
fun AccountEditPreview() {
PmudemoTheme {
Surface(
color = CustomDark
) {
val navController = rememberNavController()
Account(navController)
Account(
userUiState = User.getUser().toUiState(true),
onClick = {},
onUpdate = {}
)
}
}
}