Вроде сделал что то типо шуточной привязки

This commit is contained in:
Кашин Максим 2023-11-24 19:51:29 +04:00
parent e63c1c1df0
commit ee980fe775
10 changed files with 88 additions and 10 deletions

View File

@ -57,6 +57,8 @@ dependencies {
implementation("androidx.navigation:navigation-compose:2.7.4")
implementation("androidx.compose.material:material:1.5.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")
implementation("androidx.room:room-ktx:2.6.0")
kapt("androidx.room:room-compiler:2.6.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2")

View File

@ -20,7 +20,7 @@ fun MainScreen(bicycleDao: BicycleDao, userDao: UserDao) {
OfflineBicycleRepository(bicycleDao)
}
val offlineUserRepository = remember {
OfflineUserRepository(userDao)
OfflineUserRepository(userDao, bicycleDao)
}
Scaffold(

View File

@ -40,8 +40,8 @@ fun ScreenInfo() {
}
@Composable
fun ScreenProfile(userViewModel: UserViewModel, navHostController: NavHostController) {
RegisteryOrLogin(userViewModel, navHostController)
fun ScreenProfile(userViewModel: UserViewModel, navHostController: NavHostController, bicycleViewModel: BicycleViewModel) {
RegisteryOrLogin(userViewModel, navHostController, bicycleViewModel)
}
@Composable

View File

@ -29,7 +29,7 @@ fun SlideGraph(
NavHost(navController = navHostController, startDestination = "Profile") {
composable("Profile") {
ScreenProfile(userViewModel, navHostController = navHostController)
ScreenProfile(userViewModel, bicycleViewModel = bicycleViewModel, navHostController = navHostController)
}
composable("Info") {
ScreenInfo()

View File

@ -37,6 +37,7 @@ import androidx.navigation.NavHostController
import com.example.labwork.R
import com.example.labwork.models.User
import com.example.labwork.ui.theme.LightBluePolitech
import com.example.labwork.viewmodel.BicycleViewModel
import com.example.labwork.viewmodel.UserViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -47,7 +48,8 @@ import kotlinx.coroutines.launch
fun LoginPage(
navController: NavController,
navHostController: NavHostController,
userViewModel: UserViewModel
userViewModel: UserViewModel,
bicycleViewModel: BicycleViewModel
) {
var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
@ -57,7 +59,7 @@ fun LoginPage(
if (acceptLogin == true) {
user?.let { userProfile ->
ProfileForm(item = userProfile, userViewModel = userViewModel, navHostController = navHostController)
ProfileForm(item = userProfile, userViewModel = userViewModel, navHostController = navHostController, bicycleViewModel = bicycleViewModel)
}
}

View File

@ -5,18 +5,23 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.material3.DropdownMenu
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@ -27,12 +32,20 @@ import com.example.labwork.database.DAO.UserDao
import com.example.labwork.models.Bicycle
import com.example.labwork.models.User
import com.example.labwork.ui.theme.LightBluePolitech
import com.example.labwork.viewmodel.BicycleViewModel
import com.example.labwork.viewmodel.UserViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@Composable
fun ProfileForm(item: User, userViewModel: UserViewModel, navHostController: NavHostController) {
fun ProfileForm(item: User, userViewModel: UserViewModel, navHostController: NavHostController, bicycleViewModel: BicycleViewModel) {
val bicycles by bicycleViewModel.bicycles.collectAsState(emptyList())
var selectedBicycleId by remember { mutableStateOf<Int?>(null) }
val selectedBicycle = bicycles.find { it.id == selectedBicycleId }
val expandedDropdown = remember { mutableStateOf(false) }
var email by remember { mutableStateOf(item.email) }
var name by remember { mutableStateOf(item.name) }
var password by remember { mutableStateOf(item.password) }
@ -70,6 +83,38 @@ fun ProfileForm(item: User, userViewModel: UserViewModel, navHostController: Nav
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White)
)
Button(
onClick = { expandedDropdown.value = true },
modifier = Modifier.padding(9.dp),
shape = RoundedCornerShape(15.dp),
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
) {
Text(
text = AnnotatedString(selectedBicycle?.brand ?: "Выберите велосипед"),
color = Color.White,
fontSize = 10.sp,
textAlign = TextAlign.Center
)
}
DropdownMenu(
expanded = expandedDropdown.value,
onDismissRequest = { expandedDropdown.value = false }
) {
bicycles.forEach { bicycle ->
DropdownMenuItem(
onClick = {
selectedBicycleId = bicycle.id
expandedDropdown.value = false
println(item.id)
println(bicycle.id)
}
) {
Text(text = "${bicycle.brand} ${bicycle.model}")
}
}
}
Button(
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
modifier = Modifier
@ -77,6 +122,7 @@ fun ProfileForm(item: User, userViewModel: UserViewModel, navHostController: Nav
.padding(9.dp),
onClick = {
userViewModel.updateUser(User(item.id!!, email, name, password))
userViewModel.updateBicycleUserId(selectedBicycleId!!, item.id) // Обновление userId для Bicycle
navHostController.navigate("ListProduct")
},
shape = RoundedCornerShape(15.dp)

View File

@ -38,15 +38,16 @@ import com.example.labwork.R
import com.example.labwork.database.DAO.BicycleDao
import com.example.labwork.database.DAO.UserDao
import com.example.labwork.ui.theme.LightBluePolitech
import com.example.labwork.viewmodel.BicycleViewModel
import com.example.labwork.viewmodel.UserViewModel
@Composable
fun RegisteryOrLogin(userViewModel: UserViewModel, navHostController: NavHostController) {
fun RegisteryOrLogin(userViewModel: UserViewModel, navHostController: NavHostController, bicycleViewModel: BicycleViewModel) {
val navController = rememberNavController()
NavHost(navController, startDestination = "login") {
composable("login") {
LoginPage(navController = navController, userViewModel = userViewModel, navHostController = navHostController)
LoginPage(navController = navController, userViewModel = userViewModel, navHostController = navHostController, bicycleViewModel = bicycleViewModel)
}
composable("register") {
RegisteryPage(navController = navController, userViewModel = userViewModel, navHostController = navHostController)

View File

@ -1,9 +1,11 @@
package com.example.labwork.repository
import com.example.labwork.database.DAO.BicycleDao
import com.example.labwork.database.DAO.UserDao
import com.example.labwork.models.Bicycle
import com.example.labwork.models.User
class OfflineUserRepository(private val userDao: UserDao) : UserRepository {
class OfflineUserRepository(private val userDao: UserDao, private val bicycleDao: BicycleDao) : UserRepository {
override suspend fun getAllUsers(): List<User> {
return userDao.getAllUsers()
}
@ -31,5 +33,13 @@ class OfflineUserRepository(private val userDao: UserDao) : UserRepository {
override suspend fun getUserByEmailAndPassword(email: String, password: String): User? {
return userDao.getUserByEmailAndPassword(email, password)
}
suspend fun getBicycleById(bicycleId: Int): Bicycle? {
return bicycleDao.getBicycleById(bicycleId)
}
suspend fun updateBicycle(bicycle: Bicycle) {
bicycleDao.updateBicycle(bicycle)
}
}

View File

@ -37,6 +37,11 @@ class BicycleViewModel(private val offlineBicycleRepository: OfflineBicycleRepos
(bicycles as MutableStateFlow).value = fetchedBicycles
}
}
fun fetchAllBicycles() {
viewModelScope.launch {
_bicycles.value = offlineBicycleRepository.getAllBicycles()
}
}
fun getAllBicycles() {
viewModelScope.launch {
_bicycles.value = offlineBicycleRepository.getAllBicycles()
@ -66,4 +71,6 @@ class BicycleViewModel(private val offlineBicycleRepository: OfflineBicycleRepos
offlineBicycleRepository.deleteBicycle(bicycle)
}
}
}

View File

@ -52,5 +52,15 @@ class UserViewModel(private val offlineUserRepository: OfflineUserRepository) :
suspend fun getUserByEmail(email: String): User? {
return offlineUserRepository.getUserByEmail(email)
}
fun updateBicycleUserId(bicycleId: Int, userId: Int) {
viewModelScope.launch {
val bicycle = offlineUserRepository.getBicycleById(bicycleId)
if (bicycle != null) {
val updatedBicycle = bicycle.copy(userId = userId)
offlineUserRepository.updateBicycle(updatedBicycle)
}
}
}
}