4-ую все кроме регистрации

This commit is contained in:
Артём Алейкин 2023-12-23 11:29:26 +04:00
parent 321962fb19
commit 1ebf64982f
5 changed files with 13 additions and 10 deletions

View File

@ -19,7 +19,7 @@ interface UserDao {
fun getAll(): Flow<List<User>>
@Query("select * from users where users.id = :id")
fun getById(id: Int): User?
fun getById(id: Int): Flow<User?>
@Query("select * from users where users.login = :login")
suspend fun getByLogin(login: String): User?

View File

@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow
class OfflineUserRepository(private val userDao: UserDao): UserRepository {
override fun getAllUsers(): Flow<List<User>> = userDao.getAll()
override suspend fun getUserById(id: Int): User? = userDao.getById(id)
override suspend fun getUserById(id: Int): Flow<User?> = userDao.getById(id)
override suspend fun getUserByLogin(login: String): User? = userDao.getByLogin(login)

View File

@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow
interface UserRepository {
fun getAllUsers(): Flow<List<User>>
suspend fun getUserById(id: Int): User?
suspend fun getUserById(id: Int): Flow<User?>
suspend fun getUserByLogin(login: String): User?

View File

@ -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<User?> = userRepository.getUserById(id)
fun updateUser(user: User) = viewModelScope.launch {
userRepository.updateUser(user)

View File

@ -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
userId?.let {
userViewModel.getUser(userId).collect {
if (it != null) {
login.value = it.login
}
if (it != null) {
password.value = it.password
}
}
}
}