Логика регистрации и авторизации БЕАЧ
This commit is contained in:
parent
32958bbbb5
commit
ef32b3c798
@ -81,4 +81,5 @@ dependencies {
|
|||||||
implementation "com.google.dagger:hilt-android:2.42"
|
implementation "com.google.dagger:hilt-android:2.42"
|
||||||
kapt "com.google.dagger:hilt-android-compiler:2.42"
|
kapt "com.google.dagger:hilt-android-compiler:2.42"
|
||||||
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
|
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
|
||||||
|
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'
|
||||||
}
|
}
|
@ -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.interfaces.dao.UserDao
|
||||||
import com.example.shawarma.data.models.OrderModel
|
import com.example.shawarma.data.models.OrderModel
|
||||||
import com.example.shawarma.data.models.OrderProductModel
|
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.ProductModel
|
||||||
import com.example.shawarma.data.models.UserModel
|
import com.example.shawarma.data.models.UserModel
|
||||||
import dagger.Binds
|
|
||||||
import dagger.Module
|
|
||||||
import dagger.hilt.InstallIn
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
@Database(
|
@Database(
|
||||||
entities =
|
entities =
|
||||||
@ -51,8 +46,8 @@ abstract class AppDatabase : RoomDatabase() {
|
|||||||
INSTANCE?.let { database ->
|
INSTANCE?.let { database ->
|
||||||
// Users
|
// Users
|
||||||
val userDao = database.userDao()
|
val userDao = database.userDao()
|
||||||
//val user1 = UserModel(1, "danya", "password", "ADMIN")
|
val user1 = UserModel(1, "danya", "password", "ADMIN")
|
||||||
//userDao.insert(user1)
|
userDao.insert(user1)
|
||||||
// // Products
|
// // Products
|
||||||
// val productDao = database.productDao()
|
// val productDao = database.productDao()
|
||||||
// val product1 = ProductModel(1, "Классик", 100, null)
|
// val product1 = ProductModel(1, "Классик", 100, null)
|
||||||
|
@ -3,15 +3,15 @@ package com.example.shawarma.data.interfaces.dao
|
|||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
import androidx.room.Delete
|
import androidx.room.Delete
|
||||||
import androidx.room.Insert
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy.Companion.ABORT
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import androidx.room.Update
|
import androidx.room.Update
|
||||||
import com.example.shawarma.data.models.UserModel
|
import com.example.shawarma.data.models.UserModel
|
||||||
import dagger.Provides
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
interface UserDao {
|
interface UserDao {
|
||||||
@Insert
|
@Insert(onConflict = ABORT)
|
||||||
suspend fun insert(user: UserModel)
|
suspend fun insert(user: UserModel)
|
||||||
@Update
|
@Update
|
||||||
suspend fun update(user: UserModel)
|
suspend fun update(user: UserModel)
|
||||||
@ -22,5 +22,5 @@ interface UserDao {
|
|||||||
@Query("select * from users where id = :id")
|
@Query("select * from users where id = :id")
|
||||||
fun getById(id: Int): Flow<UserModel>
|
fun getById(id: Int): Flow<UserModel>
|
||||||
@Query("select * from users where user_login = :login and user_password = :password limit 1")
|
@Query("select * from users where user_login = :login and user_password = :password limit 1")
|
||||||
fun login(login: String, password: String) : Flow<UserModel>
|
fun login(login: String, password: String) : Flow<UserModel?>
|
||||||
}
|
}
|
@ -3,7 +3,6 @@ package com.example.shawarma.data.repos
|
|||||||
import com.example.shawarma.data.interfaces.dao.UserDao
|
import com.example.shawarma.data.interfaces.dao.UserDao
|
||||||
import com.example.shawarma.data.models.UserModel
|
import com.example.shawarma.data.models.UserModel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.first
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class UserRepository @Inject constructor(
|
class UserRepository @Inject constructor(
|
||||||
@ -24,7 +23,7 @@ class UserRepository @Inject constructor(
|
|||||||
fun getById(id: Int): Flow<UserModel> {
|
fun getById(id: Int): Flow<UserModel> {
|
||||||
return userDao.getById(id)
|
return userDao.getById(id)
|
||||||
}
|
}
|
||||||
suspend fun login(login: String, password: String): Flow<UserModel> {
|
fun login(login: String, password: String): Flow<UserModel?> {
|
||||||
return userDao.login(login, password)
|
return userDao.login(login, password)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,13 +14,12 @@ import androidx.compose.material.ButtonDefaults
|
|||||||
import androidx.compose.material.Card
|
import androidx.compose.material.Card
|
||||||
import androidx.compose.material.Text
|
import androidx.compose.material.Text
|
||||||
import androidx.compose.runtime.Composable
|
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.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.text.TextStyle
|
import androidx.compose.ui.text.TextStyle
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
import androidx.compose.ui.text.style.TextDecoration
|
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.unit.sp
|
||||||
import androidx.compose.ui.zIndex
|
import androidx.compose.ui.zIndex
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.shawarma.data.db.AppDatabase
|
|
||||||
import com.example.shawarma.ui.theme.JejuFamily
|
import com.example.shawarma.ui.theme.JejuFamily
|
||||||
import com.example.shawarma.ui.theme.MyLightRed
|
import com.example.shawarma.ui.theme.MyLightRed
|
||||||
import com.example.shawarma.utils.ScreenPaths
|
import com.example.shawarma.utils.ScreenPaths
|
||||||
import com.example.shawarma.viewmodels.UserViewModel
|
import com.example.shawarma.viewmodels.UserViewModel
|
||||||
import com.example.shawarma.widgets.MyTextField
|
import com.example.shawarma.widgets.MyTextField
|
||||||
import com.example.shawarma.widgets.ShawarmaLogo1
|
import com.example.shawarma.widgets.ShawarmaLogo1
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AuthorizationScreen(navHostController: NavHostController) {
|
fun AuthorizationScreen(navHostController: NavHostController) {
|
||||||
@ -54,6 +49,14 @@ fun AuthorizationCard(navHostController: NavHostController) {
|
|||||||
val password = remember { mutableStateOf(TextFieldValue("")) }
|
val password = remember { mutableStateOf(TextFieldValue("")) }
|
||||||
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
|
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
|
||||||
|
|
||||||
|
if (userViewModel.userModel.observeAsState().value != null) {
|
||||||
|
navHostController.navigate(ScreenPaths.home.name) {
|
||||||
|
popUpTo(ScreenPaths.authorization.name) {
|
||||||
|
inclusive = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -107,11 +110,7 @@ fun AuthorizationCard(navHostController: NavHostController) {
|
|||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
userViewModel.login(login.value.text, password.value.text)
|
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),
|
colors = ButtonDefaults.buttonColors(MyLightRed, Color.White),
|
||||||
shape = RoundedCornerShape(20.dp),
|
shape = RoundedCornerShape(20.dp),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
package com.example.shawarma.screens.registration
|
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.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.fillMaxHeight
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
@ -17,6 +14,7 @@ import androidx.compose.material.ButtonDefaults
|
|||||||
import androidx.compose.material.Card
|
import androidx.compose.material.Card
|
||||||
import androidx.compose.material.Text
|
import androidx.compose.material.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.livedata.observeAsState
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
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.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.compose.ui.zIndex
|
import androidx.compose.ui.zIndex
|
||||||
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.shawarma.screens.authorization.AuthorizationCard
|
|
||||||
import com.example.shawarma.ui.theme.JejuFamily
|
import com.example.shawarma.ui.theme.JejuFamily
|
||||||
import com.example.shawarma.ui.theme.MyLightRed
|
import com.example.shawarma.ui.theme.MyLightRed
|
||||||
import com.example.shawarma.utils.ScreenPaths
|
import com.example.shawarma.utils.ScreenPaths
|
||||||
|
import com.example.shawarma.viewmodels.UserViewModel
|
||||||
import com.example.shawarma.widgets.MyTextField
|
import com.example.shawarma.widgets.MyTextField
|
||||||
import com.example.shawarma.widgets.ShawarmaLogo1
|
import com.example.shawarma.widgets.ShawarmaLogo1
|
||||||
|
|
||||||
@ -50,6 +49,16 @@ fun RegistrationCard(navHostController: NavHostController){
|
|||||||
val password = remember { mutableStateOf(TextFieldValue("")) }
|
val password = remember { mutableStateOf(TextFieldValue("")) }
|
||||||
val passwordRepeat = remember { mutableStateOf(TextFieldValue("")) }
|
val passwordRepeat = remember { mutableStateOf(TextFieldValue("")) }
|
||||||
|
|
||||||
|
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
|
||||||
|
|
||||||
|
if (userViewModel.registrationState.observeAsState().value == true) {
|
||||||
|
navHostController.navigate(ScreenPaths.authorization.name) {
|
||||||
|
popUpTo(ScreenPaths.authorization.name) {
|
||||||
|
inclusive = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -110,7 +119,7 @@ fun RegistrationCard(navHostController: NavHostController){
|
|||||||
)
|
)
|
||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
// TODO
|
userViewModel.register(login.value.text, password.value.text, passwordRepeat.value.text)
|
||||||
},
|
},
|
||||||
colors = ButtonDefaults.buttonColors(MyLightRed, Color.White),
|
colors = ButtonDefaults.buttonColors(MyLightRed, Color.White),
|
||||||
shape = RoundedCornerShape(20.dp),
|
shape = RoundedCornerShape(20.dp),
|
||||||
|
@ -1,17 +1,13 @@
|
|||||||
package com.example.shawarma.viewmodels
|
package com.example.shawarma.viewmodels
|
||||||
|
|
||||||
import androidx.compose.runtime.State
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.example.shawarma.ShawarmaApp
|
|
||||||
import com.example.shawarma.data.models.UserModel
|
import com.example.shawarma.data.models.UserModel
|
||||||
import com.example.shawarma.data.repos.UserRepository
|
import com.example.shawarma.data.repos.UserRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.collect
|
|
||||||
import kotlinx.coroutines.flow.first
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@ -20,14 +16,42 @@ import javax.inject.Inject
|
|||||||
class UserViewModel @Inject constructor(
|
class UserViewModel @Inject constructor(
|
||||||
private val userRepository: UserRepository
|
private val userRepository: UserRepository
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private var userModel: UserModel? = null
|
private val _userModel = MutableLiveData<UserModel?>()
|
||||||
fun login(login: String, password: String) {
|
val userModel: LiveData<UserModel?>
|
||||||
viewModelScope.launch() {
|
get() = _userModel
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
userRepository.login(login, password).collect() {
|
fun login(login: String, password: String){
|
||||||
println(it)
|
viewModelScope.launch {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
userRepository.login(login, password).collect() { user ->
|
||||||
|
if (user != null) {
|
||||||
|
_userModel.postValue(user)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val _registrationState = MutableLiveData<Boolean?>()
|
||||||
|
val registrationState: LiveData<Boolean?>
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user