Compare commits
No commits in common. "1ebf64982f0fdf6985c484780b259d5cab5e93ec" and "f1c0642cf784f2287dcc0623a08ef39eda0d787b" have entirely different histories.
1ebf64982f
...
f1c0642cf7
@ -80,8 +80,4 @@ dependencies {
|
||||
ksp("androidx.room:room-compiler:$room_version")
|
||||
implementation("androidx.room:room-ktx:$room_version")
|
||||
implementation("androidx.room:room-paging:$room_version")
|
||||
|
||||
//Paging
|
||||
implementation ("androidx.paging:paging-compose:3.2.1")
|
||||
implementation ("androidx.paging:paging-runtime:3.2.1")
|
||||
}
|
@ -3,7 +3,6 @@
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<application
|
||||
android:name=".MobileApp"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
@ -48,7 +48,6 @@ import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.navArgument
|
||||
import com.example.myapplication.database.MobileAppDataBase
|
||||
import com.example.myapplication.database.entities.User
|
||||
import com.example.myapplication.screens.Authorization
|
||||
import com.example.myapplication.screens.EditCardScreen
|
||||
import com.example.myapplication.screens.EditUserScreen
|
||||
@ -110,25 +109,3 @@ fun AppNavigation(navController: NavHostController){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalUser private constructor() {
|
||||
private var user: User? = null
|
||||
|
||||
fun setUser(user: User?) {
|
||||
this.user = user
|
||||
}
|
||||
|
||||
fun getUser(): User? {
|
||||
return user
|
||||
}
|
||||
|
||||
companion object {
|
||||
private var instance: GlobalUser? = null
|
||||
|
||||
fun getInstance(): GlobalUser {
|
||||
return instance ?: synchronized(this) {
|
||||
instance ?: GlobalUser().also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
package com.example.myapplication
|
||||
|
||||
import android.app.Application
|
||||
|
||||
class MobileApp: Application() {
|
||||
lateinit var container: MobileAppContainer
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
container = MobileAppDataContainer(this)
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.example.myapplication
|
||||
|
||||
import android.content.Context
|
||||
import com.example.myapplication.database.MobileAppDataBase
|
||||
import com.example.myapplication.database.repositories.CardRepository
|
||||
import com.example.myapplication.database.repositories.OfflineCardRepository
|
||||
import com.example.myapplication.database.repositories.OfflineUserRepository
|
||||
import com.example.myapplication.database.repositories.UserRepository
|
||||
|
||||
interface MobileAppContainer {
|
||||
val cardRepository: CardRepository
|
||||
val userRepository: UserRepository
|
||||
}
|
||||
|
||||
class MobileAppDataContainer(private val context: Context): MobileAppContainer {
|
||||
override val cardRepository: CardRepository by lazy {
|
||||
OfflineCardRepository(MobileAppDataBase.getInstance(context).cardDao())
|
||||
}
|
||||
|
||||
override val userRepository: UserRepository by lazy {
|
||||
OfflineUserRepository(MobileAppDataBase.getInstance(context).userDao())
|
||||
}
|
||||
|
||||
companion object{
|
||||
const val TIMEOUT = 5000L
|
||||
}
|
||||
}
|
@ -15,7 +15,6 @@ import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
@ -68,52 +67,3 @@ fun ActiveButton(label: String, backgroundColor: Color, textColor: Color, onClic
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun PlaceholderInputFieldAuth(label: String, startValue: String? = null, isSingleLine: Boolean, onTextChanged: (String) -> Unit){
|
||||
var text = remember { mutableStateOf("") }
|
||||
startValue?.let{
|
||||
text.value = startValue
|
||||
}
|
||||
OutlinedTextField(
|
||||
value = text.value,
|
||||
onValueChange = {
|
||||
text.value = it
|
||||
onTextChanged(it)
|
||||
},
|
||||
placeholder = {
|
||||
Text(label)
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
||||
shape = RoundedCornerShape(10.dp),
|
||||
singleLine = isSingleLine
|
||||
)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun PasswordInputField(label: String, startValue: String? = null, onPasswordChanged: (String) -> Unit){
|
||||
var text = remember { mutableStateOf("") }
|
||||
startValue?.let{
|
||||
text.value = startValue
|
||||
}
|
||||
|
||||
OutlinedTextField(
|
||||
value = text.value,
|
||||
onValueChange = {
|
||||
text.value = it
|
||||
onPasswordChanged(it)
|
||||
},
|
||||
placeholder = {
|
||||
Text(label)
|
||||
},
|
||||
visualTransformation = PasswordVisualTransformation(),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
||||
shape = RoundedCornerShape(10.dp))
|
||||
}
|
@ -53,8 +53,8 @@ abstract class MobileAppDataBase : RoomDatabase() {
|
||||
private suspend fun initialDataBase(appContext: Context) {
|
||||
INSTANCE?.let { database ->
|
||||
val userDao = database.userDao()
|
||||
userDao.insert(User(id = 1, login = "Margen", password = "1234",))
|
||||
userDao.insert(User(id = 2, login = "Leonel Messi", password = "4321",))
|
||||
userDao.insert(User(id = 1, login = "Иван", password = "1234",))
|
||||
userDao.insert(User(id = 2, login = "Леонель Месси", password = "4321",))
|
||||
|
||||
val cardDao = database.cardDao()
|
||||
cardDao.insert(
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.example.myapplication.database.dao
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
@ -13,13 +12,13 @@ import kotlinx.coroutines.flow.Flow
|
||||
@Dao
|
||||
interface CardDao {
|
||||
@Query("select * from cards")
|
||||
fun getAll(): PagingSource<Int, Card>
|
||||
fun getAll(): Flow<List<Card>>
|
||||
|
||||
@Query("select * from cards where cards.id = :id")
|
||||
fun getById(id: Int): Flow<Card?>
|
||||
fun getById(id: Int): Card?
|
||||
|
||||
@Query("select * from cards where cards.user_id = :userId")
|
||||
fun getByUserId(userId: Int): PagingSource<Int, Card>
|
||||
fun getByUserId(userId: Int): Flow<List<Card>>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insert(card: Card)
|
||||
|
@ -19,10 +19,7 @@ interface UserDao {
|
||||
fun getAll(): Flow<List<User>>
|
||||
|
||||
@Query("select * from users where users.id = :id")
|
||||
fun getById(id: Int): Flow<User?>
|
||||
|
||||
@Query("select * from users where users.login = :login")
|
||||
suspend fun getByLogin(login: String): User?
|
||||
fun getById(id: Int): User?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insert(user: User)
|
||||
@ -32,8 +29,4 @@ interface UserDao {
|
||||
|
||||
@Delete
|
||||
suspend fun delete(user: User)
|
||||
|
||||
@Query("SELECT * FROM users WHERE login = :login AND password = :password")
|
||||
fun getUserByLoginAndPassword(login: String, password: String): User?
|
||||
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package com.example.myapplication.database.repositories
|
||||
|
||||
import androidx.paging.PagingData
|
||||
import com.example.myapplication.database.entities.Card
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface CardRepository {
|
||||
fun getAllCards(): Flow<PagingData<Card>>
|
||||
|
||||
fun getCardsByUserId(userId: Int): Flow<PagingData<Card>>
|
||||
|
||||
fun getCardById(id: Int): Flow<Card?>
|
||||
|
||||
suspend fun insertCard(card: Card)
|
||||
|
||||
suspend fun updateCard(card: Card)
|
||||
|
||||
suspend fun deleteCard(card: Card)
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package com.example.myapplication.database.repositories
|
||||
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
import com.example.myapplication.database.dao.CardDao
|
||||
import com.example.myapplication.database.entities.Card
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class OfflineCardRepository(private val cardDao: CardDao): CardRepository {
|
||||
override fun getAllCards(): Flow<PagingData<Card>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 5,
|
||||
prefetchDistance = 1,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = 10,
|
||||
maxSize = 15
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
cardDao.getAll()
|
||||
}
|
||||
).flow
|
||||
}
|
||||
|
||||
override fun getCardsByUserId(UserId: Int):Flow<PagingData<Card>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 5,
|
||||
prefetchDistance = 1,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = 10,
|
||||
maxSize = 15
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
cardDao.getByUserId(UserId)
|
||||
}
|
||||
).flow
|
||||
}
|
||||
|
||||
override fun getCardById(id: Int): Flow<Card?> = cardDao.getById(id)
|
||||
|
||||
override suspend fun insertCard(card: Card) = cardDao.insert(card)
|
||||
|
||||
override suspend fun updateCard(card: Card) = cardDao.update(card)
|
||||
|
||||
override suspend fun deleteCard(card: Card) = cardDao.delete(card)
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.example.myapplication.database.repositories
|
||||
|
||||
import com.example.myapplication.database.dao.UserDao
|
||||
import com.example.myapplication.database.entities.User
|
||||
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 getUserByLogin(login: String): User? = userDao.getByLogin(login)
|
||||
|
||||
override suspend fun insertUser(user: User) = userDao.insert(user)
|
||||
|
||||
override suspend fun updateUser(user: User) = userDao.update(user)
|
||||
|
||||
override suspend fun deleteUser(user: User) = userDao.delete(user)
|
||||
|
||||
override suspend fun getUserByLoginAndPassword(login: String, password: String): User? = userDao.getUserByLoginAndPassword(login, password)
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.example.myapplication.database.repositories
|
||||
|
||||
import androidx.room.Query
|
||||
import com.example.myapplication.database.entities.User
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface UserRepository {
|
||||
fun getAllUsers(): Flow<List<User>>
|
||||
|
||||
suspend fun getUserById(id: Int): Flow<User?>
|
||||
|
||||
suspend fun getUserByLogin(login: String): User?
|
||||
|
||||
suspend fun insertUser(user: User)
|
||||
|
||||
suspend fun updateUser(user: User)
|
||||
|
||||
suspend fun deleteUser(user: User)
|
||||
|
||||
suspend fun getUserByLoginAndPassword(login: String, password: String): User?
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package com.example.myapplication.database.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.cachedIn
|
||||
import com.example.myapplication.database.dao.CardDao
|
||||
import com.example.myapplication.database.entities.Card
|
||||
import com.example.myapplication.database.repositories.CardRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class CardViewModel(private val cardRepository : CardRepository):ViewModel() {
|
||||
val getAllCards: Flow<PagingData<Card>> = cardRepository.getAllCards().cachedIn(viewModelScope)
|
||||
|
||||
fun getCardById(id: Int): Flow<Card?> = cardRepository.getCardById(id)
|
||||
|
||||
fun getCardByUserId(userId: Int): Flow<PagingData<Card>> = cardRepository.getCardsByUserId(userId).cachedIn(viewModelScope)
|
||||
|
||||
fun insertCard(card: Card) = viewModelScope.launch { cardRepository.insertCard(card) }
|
||||
|
||||
fun updateCard(card: Card) = viewModelScope.launch { cardRepository.updateCard(card) }
|
||||
|
||||
fun deleteCard(card: Card) = viewModelScope.launch { cardRepository.deleteCard(card) }
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.example.myapplication.database.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import androidx.lifecycle.viewmodel.initializer
|
||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||
import com.example.myapplication.MobileApp
|
||||
|
||||
object MobileAppViewModelProvider {
|
||||
val Factory = viewModelFactory {
|
||||
initializer {
|
||||
CardViewModel(app().container.cardRepository)
|
||||
}
|
||||
initializer {
|
||||
UserViewModel(app().container.userRepository)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun CreationExtras.app(): MobileApp =
|
||||
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MobileApp)
|
@ -1,53 +0,0 @@
|
||||
package com.example.myapplication.database.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.myapplication.GlobalUser
|
||||
import com.example.myapplication.database.entities.User
|
||||
import com.example.myapplication.database.repositories.UserRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
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)
|
||||
|
||||
fun updateUser(user: User) = viewModelScope.launch {
|
||||
userRepository.updateUser(user)
|
||||
}
|
||||
|
||||
fun deleteUser(user: User) = viewModelScope.launch {
|
||||
userRepository.deleteUser(user)
|
||||
}
|
||||
|
||||
suspend fun getUserByLogin(login: String): User? = userRepository.getUserByLogin(login)
|
||||
|
||||
fun regUser(user: User) = viewModelScope.launch {
|
||||
val globalUser = userRepository.getUserByLogin(user.login)
|
||||
globalUser?.let {
|
||||
return@launch
|
||||
} ?: run {
|
||||
if(user.password.isEmpty()){
|
||||
return@launch
|
||||
}
|
||||
|
||||
userRepository.insertUser(user)
|
||||
GlobalUser.getInstance().setUser(userRepository.getUserByLogin(user.login))
|
||||
}
|
||||
}
|
||||
|
||||
fun authUser(user: User) = viewModelScope.launch {
|
||||
val globalUser = userRepository.getUserByLogin(user.login)
|
||||
if (user.password.isNotEmpty() && user.password == globalUser?.password){
|
||||
GlobalUser.getInstance().setUser(globalUser)
|
||||
val user123 = GlobalUser.getInstance().getUser()
|
||||
println(user123)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isValidEmail(email: String): Boolean {
|
||||
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
|
||||
}
|
||||
}
|
@ -14,9 +14,7 @@ import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@ -27,33 +25,32 @@ import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.myapplication.R
|
||||
import com.example.myapplication.components.ActiveButton
|
||||
import com.example.myapplication.components.LoginField
|
||||
import com.example.myapplication.components.PasswordField
|
||||
import com.example.myapplication.components.PasswordInputField
|
||||
import com.example.myapplication.components.PlaceholderInputField
|
||||
import com.example.myapplication.components.PlaceholderInputFieldAuth
|
||||
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.MobileAppViewModelProvider
|
||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
||||
import com.example.myapplication.ui.theme.SkyBlue
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun Authorization(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel( factory = MobileAppViewModelProvider.Factory )){
|
||||
fun Authorization(navController: NavHostController){
|
||||
val context = LocalContext.current
|
||||
val users = userViewModel.getAllUsers.collectAsState(emptyList()).value
|
||||
val login = remember { mutableStateOf("") }
|
||||
val password = remember { mutableStateOf("") }
|
||||
val users = remember { mutableStateListOf<User>() }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
val database = MobileAppDataBase.getInstance(context)
|
||||
database.userDao().getAll().collect { data ->
|
||||
users.clear()
|
||||
data.forEach { users.add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column (
|
||||
modifier = Modifier
|
||||
@ -76,26 +73,11 @@ fun Authorization(navController: NavHostController,
|
||||
fontSize = 20.sp
|
||||
)
|
||||
}
|
||||
PlaceholderInputFieldAuth(label = "Логин", isSingleLine = true, onTextChanged = { newlogin ->
|
||||
login.value = newlogin
|
||||
})
|
||||
PasswordInputField(label = "Пароль", onPasswordChanged = { newpassword ->
|
||||
password.value = newpassword
|
||||
})
|
||||
|
||||
ActiveButton(label = "Вход", backgroundColor = SkyBlue,
|
||||
textColor = Color.Black, onClickAction = {
|
||||
if (login.value.isNotEmpty() && password.value.isNotEmpty()) {
|
||||
userViewModel.authUser(
|
||||
User(
|
||||
login = login.value,
|
||||
password = password.value,
|
||||
)
|
||||
)
|
||||
navController.navigate("mainScreen")
|
||||
}
|
||||
})
|
||||
LoginField(text = "Логин")
|
||||
PasswordField(text = "Пароль")
|
||||
navButton(navController = navController, destination = "mainScreen", label = "Вход",
|
||||
backgroundColor = Color.Cyan, textColor = Color.Black)
|
||||
navButton(navController = navController, destination = "registration", label = "Регистрация",
|
||||
backgroundColor = SkyBlue, textColor = Color.Black)
|
||||
backgroundColor = Color.Cyan, textColor = Color.Black)
|
||||
}
|
||||
}
|
@ -35,10 +35,8 @@ import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
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
|
||||
@ -47,19 +45,12 @@ 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,
|
||||
cardViewModel: CardViewModel = viewModel( factory = MobileAppViewModelProvider.Factory),
|
||||
cardId: Int? = null) {
|
||||
fun EditCardScreen(navController: NavHostController, cardId: Int? = null) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val image = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.ferrari_laferrari_car2)) }
|
||||
@ -85,26 +76,52 @@ fun EditCardScreen(navController: NavHostController,
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
cardId?.let{
|
||||
cardViewModel.getCardById(cardId).collect {
|
||||
if (it != null) {
|
||||
image.value = it.image
|
||||
}
|
||||
if (it != null) {
|
||||
name.value = it.name
|
||||
}
|
||||
if (it != null) {
|
||||
location.value = it.location
|
||||
}
|
||||
if (it != null) {
|
||||
mileage.value = it.mileage
|
||||
}
|
||||
if (it != null) {
|
||||
price.value = it.price
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
val card = MobileAppDataBase.getInstance(context).cardDao().getById(cardId!!)
|
||||
image.value = card!!.image
|
||||
name.value = card!!.name
|
||||
location.value = card!!.location
|
||||
price.value = card!!.price
|
||||
mileage.value = card!!.mileage
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
@ -143,52 +160,34 @@ fun EditCardScreen(navController: NavHostController,
|
||||
price.value = newPrice.toIntOrNull() ?: 10000
|
||||
})
|
||||
ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = {
|
||||
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")
|
||||
edit.value = !edit.value
|
||||
})
|
||||
navButton(navController = navController, destination = "mainScreen", label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EditUserScreen(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory)){
|
||||
fun EditUserScreen(navController: NavHostController){
|
||||
val context = LocalContext.current
|
||||
|
||||
var userId = remember { mutableStateOf(0) }
|
||||
val login = remember { mutableStateOf("") }
|
||||
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.login)) }
|
||||
val name = 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)
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
GlobalUser.getInstance().getUser()?.let { user ->
|
||||
userId.value = user!!.id!!
|
||||
login.value = user!!.login
|
||||
password.value = user!!.password
|
||||
} else {
|
||||
val source = ImageDecoder
|
||||
.createSource(context.contentResolver, imageData.value!!)
|
||||
photo.value = ImageDecoder.decodeBitmap(source)
|
||||
}
|
||||
}
|
||||
|
||||
@ -198,23 +197,27 @@ fun EditUserScreen(navController: NavHostController,
|
||||
.padding(bottom = 8.dp),
|
||||
verticalArrangement = Arrangement.Bottom
|
||||
) {
|
||||
PlaceholderInputField(label = "Логин", isSingleLine = true,
|
||||
startValue = login.value, onTextChanged = { newName ->
|
||||
login.value = newName
|
||||
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 = password.value, onTextChanged = { newPassword ->
|
||||
password.value = newPassword
|
||||
})
|
||||
ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = {
|
||||
userViewModel.updateUser(
|
||||
User(
|
||||
id = userId.value,
|
||||
login = login.value,
|
||||
password = password.value,
|
||||
)
|
||||
)
|
||||
navController.navigate("userSettings")
|
||||
//edit.value = !edit.value
|
||||
})
|
||||
}
|
||||
}
|
@ -37,8 +37,6 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.requiredHeight
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.AlertDialog
|
||||
@ -51,25 +49,27 @@ import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.paging.compose.collectAsLazyPagingItems
|
||||
import androidx.paging.compose.itemKey
|
||||
import com.example.myapplication.GlobalUser
|
||||
import com.example.myapplication.R
|
||||
import com.example.myapplication.database.MobileAppDataBase
|
||||
import com.example.myapplication.database.viewmodels.CardViewModel
|
||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun MainScreen(navController: NavHostController,
|
||||
cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
||||
fun MainScreen(navController: NavHostController) {
|
||||
val context = LocalContext.current
|
||||
val cards = remember { mutableStateListOf<Card>() }
|
||||
|
||||
val cards = cardViewModel.getAllCards.collectAsLazyPagingItems()
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
val database = MobileAppDataBase.getInstance(context)
|
||||
database.cardDao().getAll().collect { data ->
|
||||
cards.clear()
|
||||
data.forEach { cards.add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column {
|
||||
Box(modifier = Modifier
|
||||
@ -77,22 +77,7 @@ fun MainScreen(navController: NavHostController,
|
||||
.fillMaxHeight(0.9f)) {
|
||||
Column()
|
||||
{
|
||||
LazyVerticalGrid(
|
||||
columns = GridCells.Fixed(1)
|
||||
) {
|
||||
item {
|
||||
addNewListItem(navController, "editcard")
|
||||
}
|
||||
items(
|
||||
count = cards.itemCount,
|
||||
key = cards.itemKey { item -> item.id!! }
|
||||
) { index: Int ->
|
||||
val card: Card? = cards[index]
|
||||
if (card != null) {
|
||||
CardListItem(item = card, navController = navController)
|
||||
}
|
||||
}
|
||||
}
|
||||
DataListScroll(navController, cards)
|
||||
}
|
||||
}
|
||||
Column(
|
||||
|
@ -4,61 +4,31 @@ import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ModifierInfo
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.myapplication.components.ActiveButton
|
||||
import com.example.myapplication.components.LoginField
|
||||
import com.example.myapplication.components.PasswordField
|
||||
import com.example.myapplication.components.PasswordInputField
|
||||
import com.example.myapplication.components.PlaceholderInputField
|
||||
import com.example.myapplication.components.PlaceholderInputFieldAuth
|
||||
import com.example.myapplication.components.navButton
|
||||
import com.example.myapplication.database.entities.User
|
||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
||||
import com.example.myapplication.ui.theme.SkyBlue
|
||||
|
||||
@Composable
|
||||
fun Registration(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory)) {
|
||||
val login = remember { mutableStateOf("") }
|
||||
val password = remember { mutableStateOf("") }
|
||||
val repeatepassword = remember { mutableStateOf("") }
|
||||
|
||||
fun Registration(navController: NavHostController) {
|
||||
Column (modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
PlaceholderInputFieldAuth(label = "Логин", isSingleLine = true, onTextChanged = {newlogin ->
|
||||
login.value = newlogin
|
||||
})
|
||||
PasswordInputField(label = "Пароль", onPasswordChanged = {newpassword ->
|
||||
password.value = newpassword
|
||||
})
|
||||
PasswordInputField(label = "Повторите пароль", onPasswordChanged = {newpassword ->
|
||||
repeatepassword.value = newpassword
|
||||
})
|
||||
ActiveButton(label = "Зарегистрироваться", backgroundColor = SkyBlue,
|
||||
textColor = Color.Black, onClickAction = {
|
||||
if (password.value == repeatepassword.value){
|
||||
userViewModel.regUser(
|
||||
User(
|
||||
login = login.value,
|
||||
password = password.value,
|
||||
)
|
||||
)
|
||||
}
|
||||
navController.navigate("authorization")
|
||||
})
|
||||
navButton(navController = navController, destination = "authorization",
|
||||
label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black)
|
||||
Text(text = "Привет!",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold)
|
||||
Text(text = "Зарегистрируйся, чтобы начать!",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold)
|
||||
LoginField(text = "Логин")
|
||||
PasswordField(text = "Пароль")
|
||||
PasswordField(text = "Повторите пароль")
|
||||
navButton(navController = navController, destination = "authorization", label = "Зарегистрироваться", backgroundColor = Color.Cyan, textColor = Color.Black)
|
||||
}
|
||||
}
|
@ -19,47 +19,35 @@ import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
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.MobileApp
|
||||
import com.example.myapplication.R
|
||||
import com.example.myapplication.components.ActiveButton
|
||||
import com.example.myapplication.components.LoginField
|
||||
import com.example.myapplication.components.PlaceholderInputField
|
||||
import com.example.myapplication.components.navBar
|
||||
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.MobileAppViewModelProvider
|
||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
||||
import com.example.myapplication.ui.theme.SkyBlue
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun UserSettings(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
||||
fun UserSettings(navController: NavHostController) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val login = remember { mutableStateOf("") }
|
||||
val password = remember { mutableStateOf("") }
|
||||
val userId = GlobalUser.getInstance().getUser()?.id?: 1
|
||||
val userId = 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -77,18 +65,8 @@ fun UserSettings(navController: NavHostController,
|
||||
modifier = Modifier
|
||||
.size(300.dp)
|
||||
)
|
||||
PlaceholderInputField(label = "Логин пользователя: ${login.value}", isSingleLine = true, onTextChanged = {newlogin ->
|
||||
login.value = newlogin})
|
||||
PlaceholderInputField(label = "Пароль пользователя: ${password.value}", isSingleLine = true, onTextChanged = {newpassword ->
|
||||
password.value = newpassword})
|
||||
ActiveButton(label = "Сохранить изменения", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction =
|
||||
{
|
||||
userViewModel.updateUser(User(
|
||||
id = userId,
|
||||
login = login.value,
|
||||
password = password.value
|
||||
))
|
||||
})
|
||||
LoginField(text = "Логин пользователя: ${login.value}")
|
||||
LoginField(text = "Пароль пользователя: ${password.value}")
|
||||
}
|
||||
}
|
||||
Column(modifier = Modifier
|
||||
|
Loading…
Reference in New Issue
Block a user