|
@@ -1,33 +1,28 @@
|
|
|
package com.example.iplace.main.viewModel
|
|
|
|
|
|
+import android.util.Log
|
|
|
+import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
import androidx.navigation.NavHostController
|
|
|
import com.example.iplace.domain.Constants
|
|
|
+import com.example.iplace.model.Gender
|
|
|
+import com.example.iplace.model.Users
|
|
|
import io.github.jan.supabase.gotrue.auth
|
|
|
import io.github.jan.supabase.gotrue.providers.builtin.Email
|
|
|
import io.github.jan.supabase.gotrue.providers.builtin.OTP
|
|
|
+import io.github.jan.supabase.postgrest.from
|
|
|
+import io.github.jan.supabase.postgrest.query.Order
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
+import kotlinx.coroutines.flow.StateFlow
|
|
|
+import kotlinx.coroutines.flow.asStateFlow
|
|
|
import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
|
|
|
class MainViewModel(): ViewModel() {
|
|
|
|
|
|
|
|
|
- fun onSignInEmailCode(emailUser: String) {
|
|
|
- viewModelScope.launch {
|
|
|
- try {
|
|
|
- Constants.supabase.auth.signInWith(OTP) {
|
|
|
- email = emailUser
|
|
|
- createUser = false
|
|
|
- }
|
|
|
-
|
|
|
- } catch (e: Exception) {
|
|
|
- println(e.message.toString())
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
fun onSignInEmailPassword(emailUser: String, passwordUser: String, navController: NavHostController) {
|
|
|
viewModelScope.launch {
|
|
|
try {
|
|
@@ -45,23 +40,98 @@ class MainViewModel(): ViewModel() {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- fun onSignUpEmail(emailUser: String, passwordUser: String, navController: NavHostController) {
|
|
|
+ private var _gender: MutableStateFlow<List<Gender>> = MutableStateFlow(listOf())
|
|
|
+ val country: StateFlow<List<Gender>> = _gender.asStateFlow()
|
|
|
+
|
|
|
+ fun getGender(){
|
|
|
viewModelScope.launch {
|
|
|
- try{
|
|
|
- var user = Constants.supabase.auth.signUpWith(Email) {
|
|
|
- email = emailUser
|
|
|
- password = passwordUser
|
|
|
+ try {
|
|
|
+ withContext(Dispatchers.IO) {
|
|
|
+ val response = Constants.supabase
|
|
|
+ .from("gender")
|
|
|
+ .select(){
|
|
|
+ order(column = "gender", order = Order.ASCENDING)
|
|
|
+ }
|
|
|
+ .decodeList<Gender>()
|
|
|
+ _gender.value = response
|
|
|
}
|
|
|
- println(user.toString())
|
|
|
- println(Constants.supabase.auth.currentUserOrNull()!!.id)
|
|
|
- println("Success")
|
|
|
- navController.navigate("auth")
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e("getGender", "Error retrieving country data", e)
|
|
|
}
|
|
|
- catch (e: Exception) {
|
|
|
- println("Error")
|
|
|
- println(e.message.toString())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private val _idGender = MutableStateFlow<Int?>(null)
|
|
|
+ val idGender: StateFlow<Int?> = _idGender.asStateFlow()
|
|
|
+ suspend fun getIdGender(titlegender: String) {
|
|
|
+ try {
|
|
|
+ val response = Constants.supabase
|
|
|
+ .from("gender")
|
|
|
+ .select {
|
|
|
+ filter {
|
|
|
+ eq("gender", titlegender)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ .decodeSingleOrNull<Gender>()
|
|
|
+ _idGender.value = response?.id_gender
|
|
|
+ Log.i("id","${_idGender.value}")
|
|
|
+
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e("getGender", "Error retrieving country data", e)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var wrongSignUp = mutableStateOf(false)
|
|
|
+ val wrongText = mutableStateOf("")
|
|
|
+ private var _navigationTo = MutableStateFlow<String?>(null)
|
|
|
+ val navigationTo = _navigationTo.asStateFlow()
|
|
|
+ fun createNewUser(newemail:String, newpassword1:String,newpassword2:String,nameUser:String, titlegender:String){
|
|
|
+ viewModelScope.launch {
|
|
|
+ try {
|
|
|
+ getIdGender(titlegender)
|
|
|
+ if(newemail == "" || newpassword1 == "" || _idGender.value == null || newpassword2 == "" ){
|
|
|
+ wrongSignUp.value = true
|
|
|
+ wrongText.value = "Заполните все данные"
|
|
|
+ }
|
|
|
+ else if(idGender.value == null){
|
|
|
+ wrongSignUp.value = true
|
|
|
+ wrongText.value = "Не выбран пол"
|
|
|
+ }
|
|
|
+ else if(newpassword1 != newpassword2){
|
|
|
+ wrongSignUp.value = true
|
|
|
+ wrongText.value = "Пароли не совпадают"
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ val user = Constants.supabase.auth.signUpWith(Email) {
|
|
|
+ email = newemail
|
|
|
+ password = newpassword1
|
|
|
+ }
|
|
|
+ AddNewUserInTable(newemail,nameUser,newpassword1, _idGender.value!!)
|
|
|
+ Log.i("Reg", "Success")
|
|
|
+ wrongSignUp.value = false
|
|
|
+ _navigationTo.value = View.menuV.toString()//
|
|
|
+ }
|
|
|
+ }catch (e: Exception){
|
|
|
+ Log.i("Registration", "Error")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ suspend fun AddNewUserInTable(emailUser: String, nameUser: String, passwordUser:String, idCountryUser:Int){
|
|
|
+ try {
|
|
|
+ Constants.supabase.auth.signInWith(Email) {
|
|
|
+ email = emailUser
|
|
|
+ password = passwordUser
|
|
|
}
|
|
|
+ val idUser = Constants.supabase.auth.currentUserOrNull()?.id ?: return
|
|
|
+ val newUser = Users(id_user = idUser, name = nameUser,address = "null",about_me = "null", id_genderFK = idCountryUser, img_user = "null", id_auth_user = 0)
|
|
|
|
|
|
+ Constants.supabase.from("users").insert(newUser)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e("AddNewUserInTable", "Error adding user: ${e.message}")
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|