From 1ebf64982f0fdf6985c484780b259d5cab5e93ec 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: Sat, 23 Dec 2023 11:29:26 +0400 Subject: [PATCH] =?UTF-8?q?4-=D1=83=D1=8E=20=D0=B2=D1=81=D0=B5=20=D0=BA?= =?UTF-8?q?=D1=80=D0=BE=D0=BC=D0=B5=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/myapplication/database/dao/UserDao.kt | 2 +- .../repositories/OfflineUserRepository.kt | 2 +- .../database/repositories/UserRepository.kt | 2 +- .../database/viewmodels/UserViewModel.kt | 2 +- .../com/example/myapplication/screens/user.kt | 15 +++++++++------ 5 files changed, 13 insertions(+), 10 deletions(-) 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 216b86e..055a188 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): User? + fun getById(id: Int): Flow @Query("select * from users where users.login = :login") suspend fun getByLogin(login: String): User? 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 634384f..0d8d3ee 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): User? = userDao.getById(id) + override suspend fun getUserById(id: Int): Flow = 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 13535fa..603cf25 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): User? + suspend fun getUserById(id: Int): Flow suspend fun getUserByLogin(login: String): User? 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 0bc6389..8c8eeda 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): User? = userRepository.getUserById(id) + suspend fun getUser(id: Int): Flow = userRepository.getUserById(id) fun updateUser(user: User) = viewModelScope.launch { userRepository.updateUser(user) 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 e34aed6..ecaf85b 100644 --- a/app/src/main/java/com/example/myapplication/screens/user.kt +++ b/app/src/main/java/com/example/myapplication/screens/user.kt @@ -51,12 +51,15 @@ fun UserSettings(navController: NavHostController, val password = remember { mutableStateOf("") } val userId = GlobalUser.getInstance().getUser()?.id?: 1 - userId?.let { - LaunchedEffect(Unit) { - withContext(Dispatchers.IO) { - val user = MobileAppDataBase.getInstance(context).userDao().getById(userId!!) - login.value = user!!.login - password.value = user!!.password + LaunchedEffect(Unit) { + userId?.let { + userViewModel.getUser(userId).collect { + if (it != null) { + login.value = it.login + } + if (it != null) { + password.value = it.password + } } } }