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 + } } } }