|
@@ -7,6 +7,7 @@ import com.example.eatwell.Resources
|
|
|
import com.example.eatwell.domain.Constant
|
|
|
import com.example.eatwell.model.Category
|
|
|
import com.example.eatwell.model.Diets
|
|
|
+import com.example.eatwell.model.Fridge
|
|
|
import com.example.eatwell.model.Ingredients
|
|
|
import com.example.eatwell.model.Recipes
|
|
|
|
|
@@ -14,23 +15,27 @@ import io.github.jan.supabase.gotrue.auth
|
|
|
import io.github.jan.supabase.gotrue.providers.builtin.Email
|
|
|
import io.github.jan.supabase.postgrest.from
|
|
|
import kotlinx.coroutines.launch
|
|
|
+import java.util.UUID
|
|
|
|
|
|
class MainViewModel():ViewModel() {
|
|
|
|
|
|
fun onSignInEmailPassword(emailUser: String, passwordUser: String) {
|
|
|
viewModelScope.launch {
|
|
|
try {
|
|
|
+ // Constant.supabase.auth.signOut()
|
|
|
val user = Constant.supabase.auth.signInWith(Email) {
|
|
|
email = emailUser
|
|
|
password = passwordUser
|
|
|
}
|
|
|
- // println(user.toString())
|
|
|
+
|
|
|
+
|
|
|
+ // println(user.toString())
|
|
|
Log.d("Логин: ", user.toString())
|
|
|
- // println(Constant.supabase.auth.currentUserOrNull()!!.id)
|
|
|
+ // println(Constant.supabase.auth.currentUserOrNull()!!.id)
|
|
|
println("Success")
|
|
|
Log.d("Логин",Constant.supabase.auth.currentUserOrNull()!!.id )
|
|
|
} catch (e: Exception) {
|
|
|
- println("Error)))))))")
|
|
|
+
|
|
|
Log.d("Ошибка: ", e.message.toString())
|
|
|
println(e.message.toString())
|
|
|
}
|
|
@@ -56,6 +61,28 @@ class MainViewModel():ViewModel() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fun addIngredientToFridge(ingredientId: Int) {
|
|
|
+ viewModelScope.launch {
|
|
|
+ val userId = UUID.fromString(Constant.supabase.auth.currentUserOrNull()!!.id)
|
|
|
+ val fridgeItem = Fridge(
|
|
|
+ ingredients = ingredientId,
|
|
|
+ user_id = userId.toString()
|
|
|
+ )
|
|
|
+
|
|
|
+ Log.d("Идентификатор: ", Constant.supabase.auth.currentUserOrNull()!!.id );
|
|
|
+ try {
|
|
|
+ val response = Constant.supabase
|
|
|
+ .from("Fridge")
|
|
|
+ .insert(Fridge(ingredients = ingredientId, user_id = Constant.supabase.auth.currentUserOrNull()!!.id ))
|
|
|
+ // .execute()
|
|
|
+ Log.d("Ответ вставки: ", response.toString())
|
|
|
+ }
|
|
|
+ catch (ex: Exception){
|
|
|
+ Log.d("Ошибка вставки: ", ex.toString())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
fun getRecipes(){
|
|
|
|
|
|
viewModelScope.launch {
|
|
@@ -69,17 +96,49 @@ class MainViewModel():ViewModel() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fun getFridge(){
|
|
|
+ viewModelScope.launch {
|
|
|
+
|
|
|
+ try {
|
|
|
+ var list = Constant.supabase.from("Fridge").select() {
|
|
|
+ filter {
|
|
|
+ Fridge::user_id eq Constant.supabase.auth.currentUserOrNull()!!.id
|
|
|
+ }
|
|
|
+ }.decodeList<Fridge>()
|
|
|
+
|
|
|
+ Log.d("id ", Constant.supabase.auth.currentUserOrNull()!!.id )
|
|
|
+ Log.d("fridge: ", list.toString())
|
|
|
+
|
|
|
+ Resources.chosenIngredients.clear();
|
|
|
+ for (i in 0..list.size){
|
|
|
+ for (j in 0..Resources.ingredients.size){
|
|
|
+ if(Resources.ingredients.get(j).id == list.get(i).id){
|
|
|
+ Resources.chosenIngredients.add(Resources.ingredients.get(j))
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e("Ошибка: ", e.message.toString())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
fun getRecipes(categoryId: Int, dietId: Int): List<Recipes>{
|
|
|
var recipesList: List<Recipes>
|
|
|
recipesList = ArrayList()
|
|
|
viewModelScope.launch {
|
|
|
try{
|
|
|
- recipesList = Constant.supabase.from("Recipes").select(){ filter {
|
|
|
+ recipesList = Constant.supabase.from("Recipes").select(){ filter {
|
|
|
|
|
|
- Recipes::category eq categoryId
|
|
|
- Recipes::diets eq dietId
|
|
|
- } }.decodeList<Recipes>()
|
|
|
- Log.d("Список по категории: ", recipesList.toString())
|
|
|
+ Recipes::category eq categoryId
|
|
|
+ Recipes::diets eq dietId
|
|
|
+ } }.decodeList<Recipes>()
|
|
|
+ Log.d("Список по категории: ", recipesList.toString())
|
|
|
}
|
|
|
catch (e: Exception){
|
|
|
Log.e("Ошибка: ", e.message.toString())
|
|
@@ -127,6 +186,4 @@ class MainViewModel():ViewModel() {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|