4-ую починил user
This commit is contained in:
parent
db3bf44b27
commit
321962fb19
@ -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): Flow<User?>
|
fun getById(id: Int): 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?
|
||||||
@ -35,4 +35,5 @@ interface UserDao {
|
|||||||
|
|
||||||
@Query("SELECT * FROM users WHERE login = :login AND password = :password")
|
@Query("SELECT * FROM users WHERE login = :login AND password = :password")
|
||||||
fun getUserByLoginAndPassword(login: String, password: String): User?
|
fun getUserByLoginAndPassword(login: String, password: String): User?
|
||||||
|
|
||||||
}
|
}
|
@ -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): 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)
|
override suspend fun getUserByLogin(login: String): User? = userDao.getByLogin(login)
|
||||||
|
|
||||||
|
@ -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): Flow<User?>
|
suspend fun getUserById(id: Int): User?
|
||||||
|
|
||||||
suspend fun getUserByLogin(login: String): User?
|
suspend fun getUserByLogin(login: String): User?
|
||||||
|
|
||||||
@ -18,5 +18,4 @@ interface UserRepository {
|
|||||||
suspend fun deleteUser(user: User)
|
suspend fun deleteUser(user: User)
|
||||||
|
|
||||||
suspend fun getUserByLoginAndPassword(login: String, password: String): User?
|
suspend fun getUserByLoginAndPassword(login: String, password: String): User?
|
||||||
|
|
||||||
}
|
}
|
@ -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): Flow<User?> = userRepository.getUserById(id)
|
suspend fun getUser(id: Int): User? = userRepository.getUserById(id)
|
||||||
|
|
||||||
fun updateUser(user: User) = viewModelScope.launch {
|
fun updateUser(user: User) = viewModelScope.launch {
|
||||||
userRepository.updateUser(user)
|
userRepository.updateUser(user)
|
||||||
@ -40,10 +40,10 @@ class UserViewModel(private val userRepository: UserRepository): ViewModel() {
|
|||||||
|
|
||||||
fun authUser(user: User) = viewModelScope.launch {
|
fun authUser(user: User) = viewModelScope.launch {
|
||||||
val globalUser = userRepository.getUserByLogin(user.login)
|
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)
|
||||||
GlobalUser.getInstance().setUser(globalUser)
|
val user123 = GlobalUser.getInstance().getUser()
|
||||||
}
|
println(user123)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ import androidx.compose.ui.unit.dp
|
|||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
|
import com.example.myapplication.GlobalUser
|
||||||
import com.example.myapplication.R
|
import com.example.myapplication.R
|
||||||
import com.example.myapplication.components.ActiveButton
|
import com.example.myapplication.components.ActiveButton
|
||||||
import com.example.myapplication.components.LoginField
|
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.components.navButton
|
||||||
import com.example.myapplication.database.MobileAppDataBase
|
import com.example.myapplication.database.MobileAppDataBase
|
||||||
import com.example.myapplication.database.entities.Card
|
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.CardViewModel
|
||||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
||||||
|
import com.example.myapplication.database.viewmodels.UserViewModel
|
||||||
import com.example.myapplication.ui.theme.SkyBlue
|
import com.example.myapplication.ui.theme.SkyBlue
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlin.math.log
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun EditCardScreen(navController: NavHostController,
|
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(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
@ -174,34 +143,52 @@ fun EditCardScreen(navController: NavHostController,
|
|||||||
price.value = newPrice.toIntOrNull() ?: 10000
|
price.value = newPrice.toIntOrNull() ?: 10000
|
||||||
})
|
})
|
||||||
ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = {
|
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)
|
navButton(navController = navController, destination = "mainScreen", label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun EditUserScreen(navController: NavHostController){
|
fun EditUserScreen(navController: NavHostController,
|
||||||
|
userViewModel: UserViewModel = viewModel(
|
||||||
|
factory = MobileAppViewModelProvider.Factory)){
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.login)) }
|
var userId = remember { mutableStateOf(0) }
|
||||||
val name = remember { mutableStateOf("") }
|
val login = remember { mutableStateOf("") }
|
||||||
val password = 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 {
|
LaunchedEffect(Unit) {
|
||||||
val source = ImageDecoder
|
GlobalUser.getInstance().getUser()?.let { user ->
|
||||||
.createSource(context.contentResolver, imageData.value!!)
|
userId.value = user!!.id!!
|
||||||
photo.value = ImageDecoder.decodeBitmap(source)
|
login.value = user!!.login
|
||||||
|
password.value = user!!.password
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,27 +198,23 @@ fun EditUserScreen(navController: NavHostController){
|
|||||||
.padding(bottom = 8.dp),
|
.padding(bottom = 8.dp),
|
||||||
verticalArrangement = Arrangement.Bottom
|
verticalArrangement = Arrangement.Bottom
|
||||||
) {
|
) {
|
||||||
Image(
|
PlaceholderInputField(label = "Логин", isSingleLine = true,
|
||||||
bitmap = photo.value.asImageBitmap(),
|
startValue = login.value, onTextChanged = { newName ->
|
||||||
contentDescription = "editplaceholder",
|
login.value = newName
|
||||||
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,
|
PlaceholderInputField(label = "Пароль", isSingleLine = true,
|
||||||
startValue = password.value, onTextChanged = { newPassword ->
|
startValue = password.value, onTextChanged = { newPassword ->
|
||||||
password.value = newPassword
|
password.value = newPassword
|
||||||
})
|
})
|
||||||
ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = {
|
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")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -68,8 +68,8 @@ import kotlinx.coroutines.withContext
|
|||||||
fun MainScreen(navController: NavHostController,
|
fun MainScreen(navController: NavHostController,
|
||||||
cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
val cards = cardViewModel.getAllCards.collectAsLazyPagingItems()
|
val cards = cardViewModel.getAllCards.collectAsLazyPagingItems()
|
||||||
//val cards = cardViewModel.getCardByUserId(GlobalUser.getInstance().getUser()?.id!!).collectAsLazyPagingItems()
|
|
||||||
|
|
||||||
Column {
|
Column {
|
||||||
Box(modifier = Modifier
|
Box(modifier = Modifier
|
||||||
@ -77,8 +77,8 @@ fun MainScreen(navController: NavHostController,
|
|||||||
.fillMaxHeight(0.9f)) {
|
.fillMaxHeight(0.9f)) {
|
||||||
Column()
|
Column()
|
||||||
{
|
{
|
||||||
LazyColumn(
|
LazyVerticalGrid(
|
||||||
modifier = Modifier.weight(1f)
|
columns = GridCells.Fixed(1)
|
||||||
) {
|
) {
|
||||||
item {
|
item {
|
||||||
addNewListItem(navController, "editcard")
|
addNewListItem(navController, "editcard")
|
||||||
|
@ -49,18 +49,14 @@ fun UserSettings(navController: NavHostController,
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val login = remember { mutableStateOf("") }
|
val login = remember { mutableStateOf("") }
|
||||||
val password = remember { mutableStateOf("") }
|
val password = remember { mutableStateOf("") }
|
||||||
val adsCount = remember { mutableStateOf(0) }
|
val userId = GlobalUser.getInstance().getUser()?.id?: 1
|
||||||
val userId = GlobalUser.getInstance().getUser()?.id!!
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
userId?.let {
|
||||||
userId?.let {
|
LaunchedEffect(Unit) {
|
||||||
userViewModel.getUser(userId).collect {
|
withContext(Dispatchers.IO) {
|
||||||
if (it != null) {
|
val user = MobileAppDataBase.getInstance(context).userDao().getById(userId!!)
|
||||||
login.value = it.login
|
login.value = user!!.login
|
||||||
}
|
password.value = user!!.password
|
||||||
if (it != null) {
|
|
||||||
password.value = it.password
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,6 +81,7 @@ fun UserSettings(navController: NavHostController,
|
|||||||
ActiveButton(label = "Сохранить изменения", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction =
|
ActiveButton(label = "Сохранить изменения", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction =
|
||||||
{
|
{
|
||||||
userViewModel.updateUser(User(
|
userViewModel.updateUser(User(
|
||||||
|
id = userId,
|
||||||
login = login.value,
|
login = login.value,
|
||||||
password = password.value
|
password = password.value
|
||||||
))
|
))
|
||||||
|
Loading…
Reference in New Issue
Block a user