|
@@ -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")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|