Browse Source

работа с регистрацией

AlexMin 1 week ago
parent
commit
d284fedd22

BIN
.gradle/8.7/fileHashes/fileHashes.lock


BIN
.gradle/file-system.probe


+ 292 - 0
.idea/assetWizardSettings.xml

@@ -0,0 +1,292 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="WizardSettings">
+    <option name="children">
+      <map>
+        <entry key="imageWizard">
+          <value>
+            <PersistentState>
+              <option name="children">
+                <map>
+                  <entry key="imageAssetPanel">
+                    <value>
+                      <PersistentState>
+                        <option name="children">
+                          <map>
+                            <entry key="actionbar">
+                              <value>
+                                <PersistentState>
+                                  <option name="children">
+                                    <map>
+                                      <entry key="clipArt">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                                <entry key="imagePath" value="C:\Users\шапкинас\AppData\Local\Temp\ic_android_black_24dp.xml" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="text">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="textAsset">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                    </map>
+                                  </option>
+                                </PersistentState>
+                              </value>
+                            </entry>
+                            <entry key="launcher">
+                              <value>
+                                <PersistentState>
+                                  <option name="children">
+                                    <map>
+                                      <entry key="foregroundClipArt">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="imagePath" value="C:\Users\шапкинас\AppData\Local\Temp\ic_android_black_24dp.xml" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="foregroundImage">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                                <entry key="imagePath" value="D:\iPlace_SHA\iPlace_mobile\documentation\logoIPlace.png" />
+                                                <entry key="scalingPercent" value="81" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="foregroundText">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="foregroundTextAsset">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                    </map>
+                                  </option>
+                                </PersistentState>
+                              </value>
+                            </entry>
+                            <entry key="launcherLegacy">
+                              <value>
+                                <PersistentState>
+                                  <option name="children">
+                                    <map>
+                                      <entry key="clipArt">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                                <entry key="imagePath" value="C:\Users\шапкинас\AppData\Local\Temp\ic_android_black_24dp.xml" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="text">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="textAsset">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                    </map>
+                                  </option>
+                                </PersistentState>
+                              </value>
+                            </entry>
+                            <entry key="notification">
+                              <value>
+                                <PersistentState>
+                                  <option name="children">
+                                    <map>
+                                      <entry key="clipArt">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                                <entry key="imagePath" value="C:\Users\шапкинас\AppData\Local\Temp\ic_android_black_24dp.xml" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="text">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="textAsset">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                    </map>
+                                  </option>
+                                </PersistentState>
+                              </value>
+                            </entry>
+                            <entry key="tvBanner">
+                              <value>
+                                <PersistentState>
+                                  <option name="children">
+                                    <map>
+                                      <entry key="foregroundText">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                    </map>
+                                  </option>
+                                </PersistentState>
+                              </value>
+                            </entry>
+                            <entry key="tvChannel">
+                              <value>
+                                <PersistentState>
+                                  <option name="children">
+                                    <map>
+                                      <entry key="foregroundClipArt">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="imagePath" value="C:\Users\шапкинас\AppData\Local\Temp\ic_android_black_24dp.xml" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="foregroundImage">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="foregroundText">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                      <entry key="foregroundTextAsset">
+                                        <value>
+                                          <PersistentState>
+                                            <option name="values">
+                                              <map>
+                                                <entry key="color" value="000000" />
+                                              </map>
+                                            </option>
+                                          </PersistentState>
+                                        </value>
+                                      </entry>
+                                    </map>
+                                  </option>
+                                </PersistentState>
+                              </value>
+                            </entry>
+                          </map>
+                        </option>
+                      </PersistentState>
+                    </value>
+                  </entry>
+                </map>
+              </option>
+            </PersistentState>
+          </value>
+        </entry>
+      </map>
+    </option>
+  </component>
+</project>

+ 98 - 28
app/src/main/java/com/example/iplace/main/viewModel/MainViewModel.kt

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

+ 21 - 9
app/src/main/java/com/example/iplace/main/viewModel/mainView/Registration.kt

@@ -23,6 +23,8 @@ import androidx.compose.material3.IconButton
 import androidx.compose.material3.OutlinedTextField
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.draw.clip
@@ -49,6 +51,12 @@ fun Registration(navController: NavHostController) {
     val basicNunitosans = FontFamily(
         Font(resId = R.font.basic_nunitosans)
     )
+
+    val newemail = remember { mutableStateOf("") }
+    val newpassword1 = remember { mutableStateOf("") }
+    val newpassword2 = remember { mutableStateOf("") }
+
+
     Image(
         painter = painterResource(id = R.drawable.re5),
         contentDescription = "",
@@ -109,8 +117,8 @@ fun Registration(navController: NavHostController) {
                 Spacer(modifier = Modifier.height(40.dp)) // Отступ от заголовка
 
                 OutlinedTextField(
-                    value = "",
-                    onValueChange = {},
+                    value = newemail.value,
+                    onValueChange = { newText -> newemail.value = newText },
                     label = { Text("Логин") },
                     modifier = Modifier
                         .fillMaxWidth()
@@ -121,8 +129,8 @@ fun Registration(navController: NavHostController) {
                 Spacer(modifier = Modifier.height(10.dp))
 
                 OutlinedTextField(
-                    value = "",
-                    onValueChange = {},
+                    value = newpassword1.value,
+                    onValueChange = { newText -> newpassword1.value = newText },
                     label = { Text("Пароль") },
                     modifier = Modifier
                         .fillMaxWidth()
@@ -132,8 +140,8 @@ fun Registration(navController: NavHostController) {
                 Spacer(modifier = Modifier.height(10.dp))
 
                 OutlinedTextField(
-                    value = "",
-                    onValueChange = {},
+                    value = newpassword2.value,
+                    onValueChange = { newText -> newpassword2.value = newText },
                     label = { Text("Повторите пароль") },
                     modifier = Modifier
                         .fillMaxWidth()
@@ -144,10 +152,14 @@ fun Registration(navController: NavHostController) {
 
 
                 Spacer(modifier = Modifier.height(20.dp))
-
                 Button(
-                    onClick = { navController.navigate("creatingUser") },
-                    modifier = Modifier.width(290.dp).height(50.dp),
+                    onClick = {
+                        viewModel.createNewUser(
+                            newemail = newemail.value,
+                            newpassword1 = newpassword1.value,
+                            newpassword2 = newpassword2.value,
+                            nameUser = "test", titlegender = "testg")
+                              },modifier = Modifier.width(290.dp).height(50.dp),
                     colors = ButtonDefaults.buttonColors(brown_contrast)
                 ) {
                     Text(

+ 2 - 2
app/src/main/java/com/example/iplace/model/Users.kt

@@ -4,8 +4,8 @@ import kotlinx.serialization.Serializable
 
 
 @Serializable
-data class Users (
-    val id_user: Int,
+data class Users(
+    val id_user: String,
     val name: String,
     val address: String,
     val about_me: String,