4-ую Регистрацию починил
+ выводятся логины пользователей на карточках
This commit is contained in:
parent
1ebf64982f
commit
c1d7f33b5d
@ -112,14 +112,14 @@ fun AppNavigation(navController: NavHostController){
|
||||
}
|
||||
|
||||
class GlobalUser private constructor() {
|
||||
private var user: User? = null
|
||||
private var user = mutableStateOf<User?>(null)
|
||||
|
||||
fun setUser(user: User?) {
|
||||
this.user = user
|
||||
this.user.value = user
|
||||
}
|
||||
|
||||
fun getUser(): User? {
|
||||
return user
|
||||
return user.value
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -42,8 +42,6 @@ class UserViewModel(private val userRepository: UserRepository): ViewModel() {
|
||||
val globalUser = userRepository.getUserByLogin(user.login)
|
||||
if (user.password.isNotEmpty() && user.password == globalUser?.password){
|
||||
GlobalUser.getInstance().setUser(globalUser)
|
||||
val user123 = GlobalUser.getInstance().getUser()
|
||||
println(user123)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.myapplication.GlobalUser
|
||||
import com.example.myapplication.R
|
||||
import com.example.myapplication.components.ActiveButton
|
||||
import com.example.myapplication.components.LoginField
|
||||
@ -50,8 +51,16 @@ import kotlinx.coroutines.withContext
|
||||
@Composable
|
||||
fun Authorization(navController: NavHostController,
|
||||
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 password = remember { mutableStateOf("") }
|
||||
|
||||
@ -71,10 +80,20 @@ fun Authorization(navController: NavHostController,
|
||||
.fillMaxWidth()
|
||||
.padding(8.dp),
|
||||
contentAlignment = Alignment.Center) {
|
||||
Text("Продажа автомобилей",
|
||||
fontWeight = FontWeight.Bold,
|
||||
fontSize = 20.sp
|
||||
)
|
||||
Column {
|
||||
Text("Продажа автомобилей",
|
||||
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 ->
|
||||
login.value = newlogin
|
||||
@ -92,7 +111,7 @@ fun Authorization(navController: NavHostController,
|
||||
password = password.value,
|
||||
)
|
||||
)
|
||||
navController.navigate("mainScreen")
|
||||
message.value = "Неправильный логин или пароль."
|
||||
}
|
||||
})
|
||||
navButton(navController = navController, destination = "registration", label = "Регистрация",
|
||||
|
@ -59,16 +59,17 @@ import com.example.myapplication.R
|
||||
import com.example.myapplication.database.MobileAppDataBase
|
||||
import com.example.myapplication.database.viewmodels.CardViewModel
|
||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun MainScreen(navController: NavHostController,
|
||||
cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
||||
val context = LocalContext.current
|
||||
|
||||
cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory),
|
||||
userViewModel: UserViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
||||
val cards = cardViewModel.getAllCards.collectAsLazyPagingItems()
|
||||
|
||||
Column {
|
||||
@ -89,7 +90,7 @@ fun MainScreen(navController: NavHostController,
|
||||
) { index: Int ->
|
||||
val card: Card? = cards[index]
|
||||
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 {
|
||||
return isNotEmpty() && all { it is T }
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun CardListItem(item: Card, navController: NavHostController){
|
||||
fun CardListItem(item: Card, navController: NavHostController, userViewModel: UserViewModel){
|
||||
val context = LocalContext.current
|
||||
|
||||
val userLogin = remember {
|
||||
mutableStateOf("")
|
||||
}
|
||||
|
||||
val showDialog = remember {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
@ -145,6 +128,15 @@ fun CardListItem(item: Card, navController: NavHostController){
|
||||
mutableStateOf(false)
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit)
|
||||
{
|
||||
userViewModel.getUser(item.userId).collect{
|
||||
if (it != null) {
|
||||
userLogin.value = it.login
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -169,7 +161,7 @@ fun CardListItem(item: Card, navController: NavHostController){
|
||||
modifier = Modifier.padding(8.dp)
|
||||
){
|
||||
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,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
@ -15,6 +15,7 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.myapplication.GlobalUser
|
||||
import com.example.myapplication.components.ActiveButton
|
||||
import com.example.myapplication.components.LoginField
|
||||
import com.example.myapplication.components.PasswordField
|
||||
@ -31,6 +32,13 @@ import com.example.myapplication.ui.theme.SkyBlue
|
||||
fun Registration(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
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 password = remember { mutableStateOf("") }
|
||||
val repeatepassword = remember { mutableStateOf("") }
|
||||
@ -56,7 +64,6 @@ fun Registration(navController: NavHostController,
|
||||
)
|
||||
)
|
||||
}
|
||||
navController.navigate("authorization")
|
||||
})
|
||||
navButton(navController = navController, destination = "authorization",
|
||||
label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black)
|
||||
|
@ -89,6 +89,10 @@ fun UserSettings(navController: NavHostController,
|
||||
password = password.value
|
||||
))
|
||||
})
|
||||
ActiveButton(label = "Выход из аккаунта", backgroundColor = Color.Red, textColor = Color.White, onClickAction = {
|
||||
GlobalUser.getInstance().setUser(null)
|
||||
navController.navigate("authorization")
|
||||
})
|
||||
}
|
||||
}
|
||||
Column(modifier = Modifier
|
||||
|
Loading…
Reference in New Issue
Block a user