4-ую починил user

This commit is contained in:
Артём Алейкин 2023-12-23 11:02:57 +04:00
parent db3bf44b27
commit 321962fb19
7 changed files with 70 additions and 90 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): Flow<User?>
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?
}

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): Flow<User?> = userDao.getById(id)
override suspend fun getUserById(id: Int): 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): Flow<User?>
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?
}

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): Flow<User?> = 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){
if (user.password.isNotEmpty() && user.password == globalUser?.password){
GlobalUser.getInstance().setUser(globalUser)
}
val user123 = GlobalUser.getInstance().getUser()
println(user123)
}
}

View File

@ -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<Bitmap>(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<Uri?>(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")
})
}
}

View File

@ -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")

View File

@ -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
}
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
))