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