From c1d7f33b5d98b934eda423ed597768a2a4b83126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Wed, 27 Dec 2023 19:42:52 +0400 Subject: [PATCH] =?UTF-8?q?4-=D1=83=D1=8E=20=D0=A0=D0=B5=D0=B3=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BF=D0=BE=D1=87=D0=B8?= =?UTF-8?q?=D0=BD=D0=B8=D0=BB=20+=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D1=8F?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=B9=20=D0=BD=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82?= =?UTF-8?q?=D0=BE=D1=87=D0=BA=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/myapplication/MainActivity.kt | 6 +-- .../database/viewmodels/UserViewModel.kt | 2 - .../myapplication/screens/authorization.kt | 33 ++++++++++--- .../myapplication/screens/mainScreen.kt | 48 ++++++++----------- .../myapplication/screens/registration.kt | 9 +++- .../com/example/myapplication/screens/user.kt | 4 ++ 6 files changed, 61 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/example/myapplication/MainActivity.kt b/app/src/main/java/com/example/myapplication/MainActivity.kt index 8a5a961..b47de8d 100644 --- a/app/src/main/java/com/example/myapplication/MainActivity.kt +++ b/app/src/main/java/com/example/myapplication/MainActivity.kt @@ -112,14 +112,14 @@ fun AppNavigation(navController: NavHostController){ } class GlobalUser private constructor() { - private var user: User? = null + private var user = mutableStateOf(null) fun setUser(user: User?) { - this.user = user + this.user.value = user } fun getUser(): User? { - return user + return user.value } companion object { diff --git a/app/src/main/java/com/example/myapplication/database/viewmodels/UserViewModel.kt b/app/src/main/java/com/example/myapplication/database/viewmodels/UserViewModel.kt index 8c8eeda..f934a34 100644 --- a/app/src/main/java/com/example/myapplication/database/viewmodels/UserViewModel.kt +++ b/app/src/main/java/com/example/myapplication/database/viewmodels/UserViewModel.kt @@ -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) } } diff --git a/app/src/main/java/com/example/myapplication/screens/authorization.kt b/app/src/main/java/com/example/myapplication/screens/authorization.kt index 472278a..189747c 100644 --- a/app/src/main/java/com/example/myapplication/screens/authorization.kt +++ b/app/src/main/java/com/example/myapplication/screens/authorization.kt @@ -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 = "Регистрация", diff --git a/app/src/main/java/com/example/myapplication/screens/mainScreen.kt b/app/src/main/java/com/example/myapplication/screens/mainScreen.kt index 22512e0..02c8f40 100644 --- a/app/src/main/java/com/example/myapplication/screens/mainScreen.kt +++ b/app/src/main/java/com/example/myapplication/screens/mainScreen.kt @@ -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 DataListScroll(navController: NavHostController, dataList: List){ - LazyColumn( - horizontalAlignment = Alignment.CenterHorizontally, - modifier = Modifier - .fillMaxWidth() - ){ - item { - when { - dataList.isListOf() -> addNewListItem(navController, "editcard") - - } - } - items(dataList){ item -> - when(item){ - is Card -> CardListItem(item = item, navController = navController) - } - } - } -} - inline fun 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 ) diff --git a/app/src/main/java/com/example/myapplication/screens/registration.kt b/app/src/main/java/com/example/myapplication/screens/registration.kt index a04c872..cf3aa95 100644 --- a/app/src/main/java/com/example/myapplication/screens/registration.kt +++ b/app/src/main/java/com/example/myapplication/screens/registration.kt @@ -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) diff --git a/app/src/main/java/com/example/myapplication/screens/user.kt b/app/src/main/java/com/example/myapplication/screens/user.kt index ecaf85b..e801b38 100644 --- a/app/src/main/java/com/example/myapplication/screens/user.kt +++ b/app/src/main/java/com/example/myapplication/screens/user.kt @@ -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