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>> fun getAll(): Flow<List<User>>
@Query("select * from users where users.id = :id") @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") @Query("select * from users where users.login = :login")
suspend fun getByLogin(login: String): User? suspend fun getByLogin(login: String): User?

View File

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

View File

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

View File

@ -12,7 +12,7 @@ import kotlinx.coroutines.runBlocking
class UserViewModel(private val userRepository: UserRepository): ViewModel() { class UserViewModel(private val userRepository: UserRepository): ViewModel() {
val getAllUsers = userRepository.getAllUsers() 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 { fun updateUser(user: User) = viewModelScope.launch {
userRepository.updateUser(user) userRepository.updateUser(user)

View File

@ -51,12 +51,15 @@ fun UserSettings(navController: NavHostController,
val password = remember { mutableStateOf("") } val password = remember { mutableStateOf("") }
val userId = GlobalUser.getInstance().getUser()?.id?: 1 val userId = GlobalUser.getInstance().getUser()?.id?: 1
userId?.let { LaunchedEffect(Unit) {
LaunchedEffect(Unit) { userId?.let {
withContext(Dispatchers.IO) { userViewModel.getUser(userId).collect {
val user = MobileAppDataBase.getInstance(context).userDao().getById(userId!!) if (it != null) {
login.value = user!!.login login.value = it.login
password.value = user!!.password }
if (it != null) {
password.value = it.password
}
} }
} }
} }