4-ую Регистрацию починил

+ выводятся логины пользователей на карточках
This commit is contained in:
Артём Алейкин 2023-12-27 19:42:52 +04:00
parent 1ebf64982f
commit c1d7f33b5d
6 changed files with 61 additions and 41 deletions

View File

@ -112,14 +112,14 @@ fun AppNavigation(navController: NavHostController){
} }
class GlobalUser private constructor() { class GlobalUser private constructor() {
private var user: User? = null private var user = mutableStateOf<User?>(null)
fun setUser(user: User?) { fun setUser(user: User?) {
this.user = user this.user.value = user
} }
fun getUser(): User? { fun getUser(): User? {
return user return user.value
} }
companion object { companion object {

View File

@ -42,8 +42,6 @@ class UserViewModel(private val userRepository: UserRepository): ViewModel() {
val globalUser = userRepository.getUserByLogin(user.login) val globalUser = userRepository.getUserByLogin(user.login)
if (user.password.isNotEmpty() && user.password == globalUser?.password){ if (user.password.isNotEmpty() && user.password == globalUser?.password){
GlobalUser.getInstance().setUser(globalUser) GlobalUser.getInstance().setUser(globalUser)
val user123 = GlobalUser.getInstance().getUser()
println(user123)
} }
} }

View File

@ -30,6 +30,7 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.myapplication.GlobalUser
import com.example.myapplication.R import com.example.myapplication.R
import com.example.myapplication.components.ActiveButton import com.example.myapplication.components.ActiveButton
import com.example.myapplication.components.LoginField import com.example.myapplication.components.LoginField
@ -50,8 +51,16 @@ import kotlinx.coroutines.withContext
@Composable @Composable
fun Authorization(navController: NavHostController, fun Authorization(navController: NavHostController,
userViewModel: UserViewModel = viewModel( factory = MobileAppViewModelProvider.Factory )){ userViewModel: UserViewModel = viewModel( factory = MobileAppViewModelProvider.Factory )){
val context = LocalContext.current
val users = userViewModel.getAllUsers.collectAsState(emptyList()).value val message = remember { mutableStateOf("") }
val isAuthorizated = remember { mutableStateOf(false) }
if(GlobalUser.getInstance().getUser() != null && !isAuthorizated.value) {
isAuthorizated.value = !isAuthorizated.value
message.value = ""
navController.navigate("mainScreen")
}
val login = remember { mutableStateOf("") } val login = remember { mutableStateOf("") }
val password = remember { mutableStateOf("") } val password = remember { mutableStateOf("") }
@ -71,10 +80,20 @@ fun Authorization(navController: NavHostController,
.fillMaxWidth() .fillMaxWidth()
.padding(8.dp), .padding(8.dp),
contentAlignment = Alignment.Center) { contentAlignment = Alignment.Center) {
Text("Продажа автомобилей", Column {
fontWeight = FontWeight.Bold, Text("Продажа автомобилей",
fontSize = 20.sp fontWeight = FontWeight.Bold,
) fontSize = 20.sp
)
if (message.value.isNotEmpty()) {
Text(
message.value,
fontWeight = FontWeight.Bold,
fontSize = 20.sp,
color = Color.Red
)
}
}
} }
PlaceholderInputFieldAuth(label = "Логин", isSingleLine = true, onTextChanged = { newlogin -> PlaceholderInputFieldAuth(label = "Логин", isSingleLine = true, onTextChanged = { newlogin ->
login.value = newlogin login.value = newlogin
@ -92,7 +111,7 @@ fun Authorization(navController: NavHostController,
password = password.value, password = password.value,
) )
) )
navController.navigate("mainScreen") message.value = "Неправильный логин или пароль."
} }
}) })
navButton(navController = navController, destination = "registration", label = "Регистрация", navButton(navController = navController, destination = "registration", label = "Регистрация",

View File

@ -59,16 +59,17 @@ import com.example.myapplication.R
import com.example.myapplication.database.MobileAppDataBase import com.example.myapplication.database.MobileAppDataBase
import com.example.myapplication.database.viewmodels.CardViewModel import com.example.myapplication.database.viewmodels.CardViewModel
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
import com.example.myapplication.database.viewmodels.UserViewModel
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@Composable @Composable
fun MainScreen(navController: NavHostController, fun MainScreen(navController: NavHostController,
cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) { cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory),
val context = LocalContext.current userViewModel: UserViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
val cards = cardViewModel.getAllCards.collectAsLazyPagingItems() val cards = cardViewModel.getAllCards.collectAsLazyPagingItems()
Column { Column {
@ -89,7 +90,7 @@ fun MainScreen(navController: NavHostController,
) { index: Int -> ) { index: Int ->
val card: Card? = cards[index] val card: Card? = cards[index]
if (card != null) { if (card != null) {
CardListItem(item = card, navController = navController) CardListItem(item = card, navController = navController, userViewModel)
} }
} }
} }
@ -107,36 +108,18 @@ fun MainScreen(navController: NavHostController,
} }
} }
@Composable
fun <T : Any> DataListScroll(navController: NavHostController, dataList: List<T>){
LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxWidth()
){
item {
when {
dataList.isListOf<Card>() -> addNewListItem(navController, "editcard")
}
}
items(dataList){ item ->
when(item){
is Card -> CardListItem(item = item, navController = navController)
}
}
}
}
inline fun <reified T> List<*>.isListOf(): Boolean { inline fun <reified T> List<*>.isListOf(): Boolean {
return isNotEmpty() && all { it is T } return isNotEmpty() && all { it is T }
} }
@Composable @Composable
fun CardListItem(item: Card, navController: NavHostController){ fun CardListItem(item: Card, navController: NavHostController, userViewModel: UserViewModel){
val context = LocalContext.current val context = LocalContext.current
val userLogin = remember {
mutableStateOf("")
}
val showDialog = remember { val showDialog = remember {
mutableStateOf(false) mutableStateOf(false)
} }
@ -145,6 +128,15 @@ fun CardListItem(item: Card, navController: NavHostController){
mutableStateOf(false) mutableStateOf(false)
} }
LaunchedEffect(Unit)
{
userViewModel.getUser(item.userId).collect{
if (it != null) {
userLogin.value = it.login
}
}
}
Card( Card(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -169,7 +161,7 @@ fun CardListItem(item: Card, navController: NavHostController){
modifier = Modifier.padding(8.dp) modifier = Modifier.padding(8.dp)
){ ){
Text( Text(
text = "Название: ${item.name} \nРасположение: ${item.location} \nПробег: ${item.mileage} \nЦена: ${item.price}", text = "Название: ${item.name} \nРасположение: ${item.location} \nПробег: ${item.mileage} \nЦена: ${item.price} \nПродавец: ${userLogin.value}",
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )

View File

@ -15,6 +15,7 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.myapplication.GlobalUser
import com.example.myapplication.components.ActiveButton import com.example.myapplication.components.ActiveButton
import com.example.myapplication.components.LoginField import com.example.myapplication.components.LoginField
import com.example.myapplication.components.PasswordField import com.example.myapplication.components.PasswordField
@ -31,6 +32,13 @@ import com.example.myapplication.ui.theme.SkyBlue
fun Registration(navController: NavHostController, fun Registration(navController: NavHostController,
userViewModel: UserViewModel = viewModel( userViewModel: UserViewModel = viewModel(
factory = MobileAppViewModelProvider.Factory)) { factory = MobileAppViewModelProvider.Factory)) {
val isRegistrated = remember { mutableStateOf(false) }
if(GlobalUser.getInstance().getUser() != null && !isRegistrated.value) {
isRegistrated.value = !isRegistrated.value
navController.navigate("mainScreen")
}
val login = remember { mutableStateOf("") } val login = remember { mutableStateOf("") }
val password = remember { mutableStateOf("") } val password = remember { mutableStateOf("") }
val repeatepassword = remember { mutableStateOf("") } val repeatepassword = remember { mutableStateOf("") }
@ -56,7 +64,6 @@ fun Registration(navController: NavHostController,
) )
) )
} }
navController.navigate("authorization")
}) })
navButton(navController = navController, destination = "authorization", navButton(navController = navController, destination = "authorization",
label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black) label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black)

View File

@ -89,6 +89,10 @@ fun UserSettings(navController: NavHostController,
password = password.value password = password.value
)) ))
}) })
ActiveButton(label = "Выход из аккаунта", backgroundColor = Color.Red, textColor = Color.White, onClickAction = {
GlobalUser.getInstance().setUser(null)
navController.navigate("authorization")
})
} }
} }
Column(modifier = Modifier Column(modifier = Modifier