From ef32b3c79874458524fe1d239acc5fa33c4bb605 Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Tue, 21 Nov 2023 03:29:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=91=D0=95=D0=90=D0=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 + .../example/shawarma/data/db/AppDatabase.kt | 9 +--- .../shawarma/data/interfaces/dao/UserDao.kt | 6 +-- .../shawarma/data/repos/UserRepository.kt | 3 +- .../authorization/AuthorizationScreen.kt | 21 ++++----- .../registration/RegistrationScreen.kt | 19 ++++++-- .../shawarma/viewmodels/UserViewModel.kt | 46 ++++++++++++++----- 7 files changed, 66 insertions(+), 39 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a213b85..097b818 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -81,4 +81,5 @@ dependencies { implementation "com.google.dagger:hilt-android:2.42" kapt "com.google.dagger:hilt-android-compiler:2.42" implementation("androidx.hilt:hilt-navigation-compose:1.0.0") + implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01' } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt b/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt index 13e8617..33a70ba 100644 --- a/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt +++ b/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt @@ -12,16 +12,11 @@ import com.example.shawarma.data.interfaces.dao.ProductDao import com.example.shawarma.data.interfaces.dao.UserDao import com.example.shawarma.data.models.OrderModel import com.example.shawarma.data.models.OrderProductModel -import com.example.shawarma.data.models.OrderStatus import com.example.shawarma.data.models.ProductModel import com.example.shawarma.data.models.UserModel -import dagger.Binds -import dagger.Module -import dagger.hilt.InstallIn import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import java.util.Date @Database( entities = @@ -51,8 +46,8 @@ abstract class AppDatabase : RoomDatabase() { INSTANCE?.let { database -> // Users val userDao = database.userDao() - //val user1 = UserModel(1, "danya", "password", "ADMIN") - //userDao.insert(user1) + val user1 = UserModel(1, "danya", "password", "ADMIN") + userDao.insert(user1) // // Products // val productDao = database.productDao() // val product1 = ProductModel(1, "Классик", 100, null) diff --git a/app/src/main/java/com/example/shawarma/data/interfaces/dao/UserDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/UserDao.kt index 23dd4e6..456ac56 100644 --- a/app/src/main/java/com/example/shawarma/data/interfaces/dao/UserDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/UserDao.kt @@ -3,15 +3,15 @@ package com.example.shawarma.data.interfaces.dao import androidx.room.Dao import androidx.room.Delete import androidx.room.Insert +import androidx.room.OnConflictStrategy.Companion.ABORT import androidx.room.Query import androidx.room.Update import com.example.shawarma.data.models.UserModel -import dagger.Provides import kotlinx.coroutines.flow.Flow @Dao interface UserDao { - @Insert + @Insert(onConflict = ABORT) suspend fun insert(user: UserModel) @Update suspend fun update(user: UserModel) @@ -22,5 +22,5 @@ interface UserDao { @Query("select * from users where id = :id") fun getById(id: Int): Flow @Query("select * from users where user_login = :login and user_password = :password limit 1") - fun login(login: String, password: String) : Flow + fun login(login: String, password: String) : Flow } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt index 70b1a03..621e2a1 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt @@ -3,7 +3,6 @@ package com.example.shawarma.data.repos import com.example.shawarma.data.interfaces.dao.UserDao import com.example.shawarma.data.models.UserModel import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.first import javax.inject.Inject class UserRepository @Inject constructor( @@ -24,7 +23,7 @@ class UserRepository @Inject constructor( fun getById(id: Int): Flow { return userDao.getById(id) } - suspend fun login(login: String, password: String): Flow { + fun login(login: String, password: String): Flow { return userDao.login(login, password) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt b/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt index 6e8b822..7fee75b 100644 --- a/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt @@ -14,13 +14,12 @@ import androidx.compose.material.ButtonDefaults import androidx.compose.material.Card import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.livedata.observeAsState 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.platform.LocalContext import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.style.TextDecoration @@ -28,17 +27,13 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.zIndex import androidx.hilt.navigation.compose.hiltViewModel -import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController -import com.example.shawarma.data.db.AppDatabase import com.example.shawarma.ui.theme.JejuFamily import com.example.shawarma.ui.theme.MyLightRed import com.example.shawarma.utils.ScreenPaths import com.example.shawarma.viewmodels.UserViewModel import com.example.shawarma.widgets.MyTextField import com.example.shawarma.widgets.ShawarmaLogo1 -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext @Composable fun AuthorizationScreen(navHostController: NavHostController) { @@ -54,6 +49,14 @@ fun AuthorizationCard(navHostController: NavHostController) { val password = remember { mutableStateOf(TextFieldValue("")) } val userViewModel: UserViewModel = hiltViewModel() + if (userViewModel.userModel.observeAsState().value != null) { + navHostController.navigate(ScreenPaths.home.name) { + popUpTo(ScreenPaths.authorization.name) { + inclusive = true + } + } + } + Column( horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier @@ -107,11 +110,7 @@ fun AuthorizationCard(navHostController: NavHostController) { Button( onClick = { userViewModel.login(login.value.text, password.value.text) - navHostController.navigate(ScreenPaths.home.name) { - popUpTo(ScreenPaths.authorization.name) { - inclusive = true - } - }}, + }, colors = ButtonDefaults.buttonColors(MyLightRed, Color.White), shape = RoundedCornerShape(20.dp), modifier = Modifier diff --git a/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt b/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt index 6eb6ebd..21bb236 100644 --- a/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt @@ -1,8 +1,5 @@ package com.example.shawarma.screens.registration -import androidx.compose.foundation.background -import androidx.compose.foundation.gestures.FlingBehavior -import androidx.compose.foundation.gestures.ScrollScope import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxHeight @@ -17,6 +14,7 @@ import androidx.compose.material.ButtonDefaults import androidx.compose.material.Card import androidx.compose.material.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment @@ -28,11 +26,12 @@ import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.zIndex +import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavHostController -import com.example.shawarma.screens.authorization.AuthorizationCard import com.example.shawarma.ui.theme.JejuFamily import com.example.shawarma.ui.theme.MyLightRed import com.example.shawarma.utils.ScreenPaths +import com.example.shawarma.viewmodels.UserViewModel import com.example.shawarma.widgets.MyTextField import com.example.shawarma.widgets.ShawarmaLogo1 @@ -50,6 +49,16 @@ fun RegistrationCard(navHostController: NavHostController){ val password = remember { mutableStateOf(TextFieldValue("")) } val passwordRepeat = remember { mutableStateOf(TextFieldValue("")) } + val userViewModel: UserViewModel = hiltViewModel() + + if (userViewModel.registrationState.observeAsState().value == true) { + navHostController.navigate(ScreenPaths.authorization.name) { + popUpTo(ScreenPaths.authorization.name) { + inclusive = true + } + } + } + Column( horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier @@ -110,7 +119,7 @@ fun RegistrationCard(navHostController: NavHostController){ ) Button( onClick = { - // TODO + userViewModel.register(login.value.text, password.value.text, passwordRepeat.value.text) }, colors = ButtonDefaults.buttonColors(MyLightRed, Color.White), shape = RoundedCornerShape(20.dp), diff --git a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt index eacf79f..aa2c3fb 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt @@ -1,17 +1,13 @@ package com.example.shawarma.viewmodels -import androidx.compose.runtime.State -import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.example.shawarma.ShawarmaApp import com.example.shawarma.data.models.UserModel import com.example.shawarma.data.repos.UserRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.collect -import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import javax.inject.Inject @@ -20,14 +16,42 @@ import javax.inject.Inject class UserViewModel @Inject constructor( private val userRepository: UserRepository ) : ViewModel() { - private var userModel: UserModel? = null - fun login(login: String, password: String) { - viewModelScope.launch() { - withContext(Dispatchers.IO) { - userRepository.login(login, password).collect() { - println(it) + private val _userModel = MutableLiveData() + val userModel: LiveData + get() = _userModel + + fun login(login: String, password: String){ + viewModelScope.launch { + withContext(Dispatchers.Main) { + userRepository.login(login, password).collect() { user -> + if (user != null) { + _userModel.postValue(user) + } } } } } + + private val _registrationState = MutableLiveData() + val registrationState: LiveData + get() = _registrationState + + fun register(login: String, password: String, passwordRepeat: String) { + if (password != passwordRepeat) { + // ругаться + _registrationState.postValue(false) + } + viewModelScope.launch { + withContext(Dispatchers.Main) { + userRepository.login(login, password).collect() { user -> + if (user == null) { + userRepository.insert(UserModel(null, login, password, "USER")) + _registrationState.postValue(true) + } + } + + } + } + } + } \ No newline at end of file