Ver Fonte

add: method createNewUser, add validation; update: Registration and her viewModel

unknown há 4 dias atrás
pai
commit
9e08ab1d8a

+ 29 - 2
TripHelper/app/src/main/java/com/example/triphelper/view/Registration/Registration.kt

@@ -2,6 +2,7 @@ package com.example.triphelper.view.Registration
 
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.ScrollState
+import androidx.compose.foundation.background
 import androidx.compose.foundation.border
 import androidx.compose.foundation.layout.Arrangement
 import androidx.compose.foundation.layout.Box
@@ -22,6 +23,7 @@ import androidx.compose.material.icons.filled.ArrowForward
 import androidx.compose.material.icons.filled.MoreVert
 import androidx.compose.material3.Button
 import androidx.compose.material3.ButtonDefaults.buttonColors
+import androidx.compose.material3.ButtonDefaults.shape
 import androidx.compose.material3.Card
 import androidx.compose.material3.Divider
 import androidx.compose.material3.DropdownMenu
@@ -182,7 +184,7 @@ fun Registration(
                                     selectedItem = item.title
                                     coutryTitle.value = item.title
                                     expanded = false
-                                    viewModel.getIdCountry(coutryTitle.value)
+//                                    viewModel.getIdCountry(coutryTitle.value)
                                 },
                                 text = {
                                     Text(
@@ -200,6 +202,29 @@ fun Registration(
                 }
                 TextFieldStylePasswordAuth("Пароль", password)
                 TextFieldStylePasswordAuth("Повтор пароля", repeatPassword)
+                if(viewModel.wrongSignUp.value){
+                    Box(
+                        modifier = Modifier
+                            .fillMaxWidth()
+                            .clip(RoundedCornerShape(10.dp))
+                            .background(color = Color.White)
+                            .border(
+                                width = 2.dp,
+                                color = Color.White,
+                                shape = RoundedCornerShape(10.dp)
+                            ),
+                    ){
+                        Text(
+                            text = viewModel.wrongText.value,
+                            fontFamily = Roboto,
+                            fontWeight = FontWeight.W600,
+                            fontSize = 22.sp,
+                            color = Color.Red,
+                            modifier = Modifier.align(Alignment.Center)
+                        )
+                    }
+
+                }
             }
             Row(
                 modifier = Modifier
@@ -226,7 +251,9 @@ fun Registration(
                 }
                 Spacer(modifier = Modifier.width(20.dp))
                 Button(
-                    onClick = {},
+                    onClick = {
+                              viewModel.createNewUser(email.value,name.value, coutryTitle.value
+                                  ,password.value,repeatPassword.value)},
                     shape = RoundedCornerShape(10.dp),
                     modifier = Modifier
                         .height(100.dp)

+ 50 - 7
TripHelper/app/src/main/java/com/example/triphelper/view/Registration/RegistrationViewModel.kt

@@ -1,18 +1,20 @@
 package com.example.triphelper.view.Registration
 
-import android.provider.SyncStateContract.Columns
 import android.util.Log
 import androidx.compose.runtime.mutableStateOf
+import androidx.compose.ui.unit.dp
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import androidx.navigation.NavController
 import com.example.triphelper.domain.Constants
 import com.example.triphelper.model.Country
+import com.example.triphelper.model.Users
 import dagger.hilt.android.lifecycle.HiltViewModel
 import io.github.jan.supabase.gotrue.Auth
 import io.github.jan.supabase.gotrue.auth
 import io.github.jan.supabase.gotrue.providers.builtin.Email
 import io.github.jan.supabase.postgrest.from
+import io.github.jan.supabase.postgrest.query.Columns
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.StateFlow
@@ -23,10 +25,14 @@ import javax.inject.Inject
 
 @HiltViewModel
 class RegistrationViewModel@Inject constructor(): ViewModel(){
-    private var _navigationTo = MutableStateFlow<String?>(null)
+    private var _navigationTo = MutableStateFlow<String?>(null) //Переменная для навигации
     val navigationTo = _navigationTo.asStateFlow()
-    private var _country: MutableStateFlow<List<Country>> = MutableStateFlow(listOf())
+    private var _country: MutableStateFlow<List<Country>> = MutableStateFlow(listOf()) //Переменная для листа стран
     val country: StateFlow<List<Country>> = _country.asStateFlow()
+    private val _idCountry = MutableStateFlow<Int?>(null) //Переменная для получения id выбранной страны
+    val idCountry: StateFlow<Int?> = _idCountry.asStateFlow()
+    var wrongSignUp = mutableStateOf(false) //Видимость сообщения для пользователя
+    val wrongText = mutableStateOf("")
 
     fun getCountry(){
         viewModelScope.launch {
@@ -43,24 +49,61 @@ class RegistrationViewModel@Inject constructor(): ViewModel(){
             }
         }
     }
-    fun getIdCountry(titleCountry :String){
+    fun getIdCountry(titleCountry: String) {
         viewModelScope.launch {
             try {
                 withContext(Dispatchers.IO) {
                     val response = Constants.supabase
                         .from("country")
-                        .select(columns = Columns.list("id")){
+                        .select(){
                             filter {
                                 eq("title", titleCountry)
                             }
                         }
                         .decodeList<Country>()
-                    _country.value = response
+                    val countryId = if (response.isNotEmpty()) response[0].id else null
+                    _idCountry.value = countryId
                 }
             } catch (e: Exception) {
                 Log.e("getCountry", "Error retrieving country data", e)
             }
-
+        }
+    }
+    fun createNewUser(emailUser:String, nameUser:String, countryTitle:String,passwordUser:String, repeatPasswordUser:String){
+        viewModelScope.launch {
+            getIdCountry(countryTitle)
+            val idCountryUser = _idCountry.value
+            try {
+                if(emailUser == null || nameUser == null || countryTitle == null || passwordUser == null || repeatPasswordUser == null){
+                    wrongSignUp.value = true
+                    wrongText.value = "Заполните все данные"
+                }
+                else if(idCountryUser == null){
+                    wrongSignUp.value = true
+                    wrongText.value = "Не выбрана страна"
+                }
+                else if(passwordUser != repeatPasswordUser){
+                    wrongSignUp.value = true
+                    wrongText.value = "Пароли не совпадают"
+                }
+                else{
+                    val user = Constants.supabase.auth.signUpWith(Email) {
+                        email = emailUser
+                        password = passwordUser
+                    }
+                    val response = Constants.supabase.auth.signInWith(Email){
+                        email = emailUser
+                        password = passwordUser
+                    }
+                    val idUser = Constants.supabase.auth.currentUserOrNull()!!.id
+                    val newUser = Users(id = idUser.toString(), avatar = null, country = idCountryUser, name = nameUser)
+                    Constants.supabase.from("users").insert(newUser)
+                    Log.i("Auth", "Success")
+                    wrongSignUp.value = false
+                }
+            }catch (e: Exception){
+                Log.i("Registration", "Error")
+            }
         }
     }
 }