diff --git a/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt b/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt index c518f94..216b86e 100644 --- a/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt +++ b/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt @@ -19,7 +19,7 @@ interface UserDao { fun getAll(): Flow> @Query("select * from users where users.id = :id") - fun getById(id: Int): Flow + fun getById(id: Int): User? @Query("select * from users where users.login = :login") suspend fun getByLogin(login: String): User? @@ -35,4 +35,5 @@ interface UserDao { @Query("SELECT * FROM users WHERE login = :login AND password = :password") fun getUserByLoginAndPassword(login: String, password: String): User? + } \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/database/repositories/OfflineUserRepository.kt b/app/src/main/java/com/example/myapplication/database/repositories/OfflineUserRepository.kt index 0d8d3ee..634384f 100644 --- a/app/src/main/java/com/example/myapplication/database/repositories/OfflineUserRepository.kt +++ b/app/src/main/java/com/example/myapplication/database/repositories/OfflineUserRepository.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow class OfflineUserRepository(private val userDao: UserDao): UserRepository { override fun getAllUsers(): Flow> = userDao.getAll() - override suspend fun getUserById(id: Int): Flow = userDao.getById(id) + override suspend fun getUserById(id: Int): User? = userDao.getById(id) override suspend fun getUserByLogin(login: String): User? = userDao.getByLogin(login) diff --git a/app/src/main/java/com/example/myapplication/database/repositories/UserRepository.kt b/app/src/main/java/com/example/myapplication/database/repositories/UserRepository.kt index 3a97f54..13535fa 100644 --- a/app/src/main/java/com/example/myapplication/database/repositories/UserRepository.kt +++ b/app/src/main/java/com/example/myapplication/database/repositories/UserRepository.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow interface UserRepository { fun getAllUsers(): Flow> - suspend fun getUserById(id: Int): Flow + suspend fun getUserById(id: Int): User? suspend fun getUserByLogin(login: String): User? @@ -18,5 +18,4 @@ interface UserRepository { suspend fun deleteUser(user: User) suspend fun getUserByLoginAndPassword(login: String, password: String): User? - } \ No newline at end of file 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 3bafbc3..0bc6389 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 @@ -12,7 +12,7 @@ import kotlinx.coroutines.runBlocking class UserViewModel(private val userRepository: UserRepository): ViewModel() { val getAllUsers = userRepository.getAllUsers() - suspend fun getUser(id: Int): Flow = userRepository.getUserById(id) + suspend fun getUser(id: Int): User? = userRepository.getUserById(id) fun updateUser(user: User) = viewModelScope.launch { userRepository.updateUser(user) @@ -40,10 +40,10 @@ class UserViewModel(private val userRepository: UserRepository): ViewModel() { fun authUser(user: User) = viewModelScope.launch { val globalUser = userRepository.getUserByLogin(user.login) - globalUser?.let { - if (user.password.isNotEmpty() && user.password == globalUser.password){ - GlobalUser.getInstance().setUser(globalUser) - } + 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/editcard.kt b/app/src/main/java/com/example/myapplication/screens/editcard.kt index 6cbafe7..5265b24 100644 --- a/app/src/main/java/com/example/myapplication/screens/editcard.kt +++ b/app/src/main/java/com/example/myapplication/screens/editcard.kt @@ -38,6 +38,7 @@ import androidx.compose.ui.unit.dp 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 @@ -46,11 +47,14 @@ import com.example.myapplication.components.navBar import com.example.myapplication.components.navButton import com.example.myapplication.database.MobileAppDataBase import com.example.myapplication.database.entities.Card +import com.example.myapplication.database.entities.User import com.example.myapplication.database.viewmodels.CardViewModel import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider +import com.example.myapplication.database.viewmodels.UserViewModel import com.example.myapplication.ui.theme.SkyBlue import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import kotlin.math.log @Composable fun EditCardScreen(navController: NavHostController, @@ -103,41 +107,6 @@ fun EditCardScreen(navController: NavHostController, } } - val edit = remember { mutableStateOf(false) } - if (edit.value){ - LaunchedEffect(Unit) { - withContext(Dispatchers.IO) { - cardId?.let { - MobileAppDataBase.getInstance(context).cardDao() - .update( - Card( - id = cardId, - name = name.value, - location = location.value, - price = price.value, - mileage = mileage.value, - image = image.value, - userId = 1) - ) - - } ?: run { - MobileAppDataBase.getInstance(context).cardDao() - .insert( - Card( - name = name.value, - location = location.value, - price = price.value, - mileage = mileage.value, - image = image.value, - userId = 1) - ) - } - } - } - edit.value = !edit.value - navController.navigate("mainScreen") - } - Column( modifier = Modifier .fillMaxSize() @@ -174,34 +143,52 @@ fun EditCardScreen(navController: NavHostController, price.value = newPrice.toIntOrNull() ?: 10000 }) ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = { - edit.value = !edit.value + cardId?.let { + cardViewModel.updateCard( + Card( + id = cardId, + image = image.value, + name = name.value, + location = location.value, + mileage = mileage.value, + price = price.value, + userId = GlobalUser.getInstance().getUser()?.id ?: 1 + ) + ) + } ?: run { + cardViewModel.insertCard( + Card( + image = image.value, + name = name.value, + location = location.value, + mileage = mileage.value, + price = price.value, + userId = GlobalUser.getInstance().getUser()?.id ?: 1 + ) + ) + } + navController.navigate("mainScreen") }) navButton(navController = navController, destination = "mainScreen", label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black) } } @Composable -fun EditUserScreen(navController: NavHostController){ +fun EditUserScreen(navController: NavHostController, + userViewModel: UserViewModel = viewModel( + factory = MobileAppViewModelProvider.Factory)){ val context = LocalContext.current - val photo = remember { mutableStateOf(BitmapFactory.decodeResource(context.resources, R.drawable.login)) } - val name = remember { mutableStateOf("") } + var userId = remember { mutableStateOf(0) } + val login = remember { mutableStateOf("") } val password = remember { mutableStateOf("") } - val imageData = remember { mutableStateOf(null) } - val launcher = - rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) {uri: Uri? -> - imageData.value = uri - } - imageData.value?.let { - if (Build.VERSION.SDK_INT < 28) { - photo.value = MediaStore.Images - .Media.getBitmap(context.contentResolver, imageData.value) - } else { - val source = ImageDecoder - .createSource(context.contentResolver, imageData.value!!) - photo.value = ImageDecoder.decodeBitmap(source) + LaunchedEffect(Unit) { + GlobalUser.getInstance().getUser()?.let { user -> + userId.value = user!!.id!! + login.value = user!!.login + password.value = user!!.password } } @@ -211,27 +198,23 @@ fun EditUserScreen(navController: NavHostController){ .padding(bottom = 8.dp), verticalArrangement = Arrangement.Bottom ) { - Image( - bitmap = photo.value.asImageBitmap(), - contentDescription = "editplaceholder", - contentScale = ContentScale.Crop, - modifier = Modifier - .size(384.dp) - .padding(8.dp) - .align(Alignment.CenterHorizontally)) - ActiveButton(label = "Выбрать фото", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = { - launcher.launch("image/*") - }) - PlaceholderInputField(label = "Никнейм", isSingleLine = true, - startValue = name.value, onTextChanged = { newName -> - name.value = newName + PlaceholderInputField(label = "Логин", isSingleLine = true, + startValue = login.value, onTextChanged = { newName -> + login.value = newName }) PlaceholderInputField(label = "Пароль", isSingleLine = true, startValue = password.value, onTextChanged = { newPassword -> password.value = newPassword }) ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = { - //edit.value = !edit.value + userViewModel.updateUser( + User( + id = userId.value, + login = login.value, + password = password.value, + ) + ) + navController.navigate("userSettings") }) } } \ No newline at end of file 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 9ea65af..22512e0 100644 --- a/app/src/main/java/com/example/myapplication/screens/mainScreen.kt +++ b/app/src/main/java/com/example/myapplication/screens/mainScreen.kt @@ -68,8 +68,8 @@ import kotlinx.coroutines.withContext fun MainScreen(navController: NavHostController, cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) { val context = LocalContext.current + val cards = cardViewModel.getAllCards.collectAsLazyPagingItems() - //val cards = cardViewModel.getCardByUserId(GlobalUser.getInstance().getUser()?.id!!).collectAsLazyPagingItems() Column { Box(modifier = Modifier @@ -77,8 +77,8 @@ fun MainScreen(navController: NavHostController, .fillMaxHeight(0.9f)) { Column() { - LazyColumn( - modifier = Modifier.weight(1f) + LazyVerticalGrid( + columns = GridCells.Fixed(1) ) { item { addNewListItem(navController, "editcard") 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 513ded3..e34aed6 100644 --- a/app/src/main/java/com/example/myapplication/screens/user.kt +++ b/app/src/main/java/com/example/myapplication/screens/user.kt @@ -49,18 +49,14 @@ fun UserSettings(navController: NavHostController, val context = LocalContext.current val login = remember { mutableStateOf("") } val password = remember { mutableStateOf("") } - val adsCount = remember { mutableStateOf(0) } - val userId = GlobalUser.getInstance().getUser()?.id!! + val userId = GlobalUser.getInstance().getUser()?.id?: 1 - LaunchedEffect(Unit) { - userId?.let { - userViewModel.getUser(userId).collect { - if (it != null) { - login.value = it.login - } - if (it != null) { - password.value = it.password - } + userId?.let { + LaunchedEffect(Unit) { + withContext(Dispatchers.IO) { + val user = MobileAppDataBase.getInstance(context).userDao().getById(userId!!) + login.value = user!!.login + password.value = user!!.password } } } @@ -85,6 +81,7 @@ fun UserSettings(navController: NavHostController, ActiveButton(label = "Сохранить изменения", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = { userViewModel.updateUser(User( + id = userId, login = login.value, password = password.value ))