Selaa lähdekoodia

Удаление, выпадающие списки

Anastasia 4 päivää sitten
vanhempi
commit
eafd6cbeb9

+ 10 - 8
EatWell/app/src/main/java/com/example/eatwell/Resources.kt

@@ -1,6 +1,8 @@
 package com.example.eatwell
 
+import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.setValue
 import com.example.eatwell.model.Category
 import com.example.eatwell.model.Diets
 import com.example.eatwell.model.Fridge
@@ -10,15 +12,15 @@ import com.example.eatwell.view.MainViewModel
 
 class Resources {
     companion object {
-        var userId: Int = -1;
         var viewModel = MainViewModel()
-        var diets: MutableList<Diets> = mutableListOf()
-        var categories: MutableList<Category> = mutableListOf()
-        var recipes: MutableList<Recipes> = mutableListOf()
-        var chosenRecipes: MutableList<Recipes> = mutableListOf()
-        var ingredients: MutableList<Ingredients> = mutableListOf()
-        var chosenIngredients: MutableList<Ingredients> = mutableListOf()
-        var fridge: MutableList<Fridge> = mutableListOf()
+        //       var favourites: MutableList<Track> by mutableStateOf(mutableListOf())
+        //val favourites = remember { mutableStateListOf<Track>()}
+        var diets: MutableList<Diets> by mutableStateOf(mutableListOf())
+        var categories: MutableList<Category> by mutableStateOf(mutableListOf())
+        var recipes: MutableList<Recipes> by mutableStateOf(mutableListOf())
+        var ingredients: MutableList<Ingredients> by mutableStateOf(mutableListOf())
+        var chosenIngredients: MutableList<Ingredients> by mutableStateOf(mutableListOf())
+        var fridge: MutableList<Fridge> by mutableStateOf(mutableListOf())
         // Состояние для выбранных рецептов
     }
 }

+ 33 - 10
EatWell/app/src/main/java/com/example/eatwell/view/MainViewModel.kt

@@ -19,6 +19,7 @@ import java.util.UUID
 
 class MainViewModel():ViewModel() {
 
+
     fun onSignInEmailPassword(emailUser: String, passwordUser: String) {
         viewModelScope.launch {
             try {
@@ -36,7 +37,7 @@ class MainViewModel():ViewModel() {
                 Log.d("Логин",Constant.supabase.auth.currentUserOrNull()!!.id )
             } catch (e: Exception) {
 
-                Log.d("Ошибка: ", e.message.toString())
+                Log.d("Ошибка а: ", e.message.toString())
                 println(e.message.toString())
             }
         }
@@ -91,7 +92,29 @@ class MainViewModel():ViewModel() {
                     .decodeList<Recipes>() as ArrayList<Recipes>
                 Log.d("Список: ", " " + Resources.recipes)
             } catch (e: Exception) {
-                Log.e("Ошибка: ", e.message.toString())
+                Log.e("Ошибка р: ", e.message.toString())
+            }
+        }
+    }
+
+    fun deleteIngredient(ingredientId: Int) {
+
+        viewModelScope.launch {
+            try {
+                Constant.supabase.from("Fridge").delete{
+                    filter {
+                        Fridge::user_id eq Constant.supabase.auth.currentUserOrNull()!!.id
+                        Fridge::ingredients eq ingredientId
+                    }
+                }
+
+                var list = Constant.supabase.from("Fridge")
+                    .select().decodeList<Fridge>()
+                //   getFridge()
+
+                Log.d("Список продуктов: ", list.toString())
+            } catch (e: Exception) {
+                Log.e("Ошибка продукты: ", e.message.toString())
             }
         }
     }
@@ -110,9 +133,9 @@ class MainViewModel():ViewModel() {
                 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){
+                for (i in 0..list.size-1){
+                    for (j in 0..Resources.ingredients.size-1){
+                        if(Resources.ingredients.get(j).id == list.get(i).ingredients){
                             Resources.chosenIngredients.add(Resources.ingredients.get(j))
                             break
                         }
@@ -122,7 +145,7 @@ class MainViewModel():ViewModel() {
 
 
             } catch (e: Exception) {
-                Log.e("Ошибка: ", e.message.toString())
+                Log.e("Ошибка х: ", e.message.toString())
             }
         }
     }
@@ -141,7 +164,7 @@ class MainViewModel():ViewModel() {
                 Log.d("Список по категории: ", recipesList.toString())
             }
             catch (e: Exception){
-                Log.e("Ошибка: ", e.message.toString())
+                Log.e("Ошибка р: ", e.message.toString())
             }
         }
         return recipesList;
@@ -154,7 +177,7 @@ class MainViewModel():ViewModel() {
                 Log.d("Диеты: ", Resources.diets.toString())
             }
             catch (e: Exception){
-                Log.e("Ошибка: ", e.message.toString())
+                Log.e("Ошибка д: ", e.message.toString())
             }
         }
 
@@ -168,7 +191,7 @@ class MainViewModel():ViewModel() {
                 Log.d("Категория: ", Resources.categories.toString())
             }
             catch (e: Exception){
-                Log.e("Ошибка: ", e.message.toString())
+                Log.e("Ошибка к:  ", e.message.toString())
             }
         }
     }
@@ -181,7 +204,7 @@ class MainViewModel():ViewModel() {
                 Log.d("Инградиенты: ", Resources.ingredients.toString())
             }
             catch (e: Exception){
-                Log.e("Ошибка: ", e.message.toString())
+                Log.e("Ошибка и: ", e.message.toString())
             }
         }
 

+ 11 - 3
EatWell/app/src/main/java/com/example/eatwell/view/mainActivity/Fridge.kt

@@ -54,6 +54,7 @@ import coil.compose.rememberImagePainter
 import com.example.eatwell.R
 import com.example.eatwell.Resources
 import com.example.eatwell.domain.Constant
+import com.example.eatwell.model.Ingredients
 import com.example.eatwell.model.Recipes
 import com.example.eatwell.view.ui.theme.black
 import com.example.eatwell.view.ui.theme.fon2
@@ -66,6 +67,13 @@ import io.github.jan.supabase.gotrue.auth
 @Composable
 fun fridge(navController: NavController) {
     Resources.viewModel.getFridge()
+    var chosenIngredients = remember { mutableStateListOf<Ingredients>() }
+
+    Log.d("Продукты: ", Resources.chosenIngredients.toString())
+    chosenIngredients.clear()
+    for (i in 0..Resources.chosenIngredients.size - 1) {
+        chosenIngredients.add(Resources.chosenIngredients.get(i))
+    }
     Box(
         modifier = Modifier
             .fillMaxSize()
@@ -189,13 +197,12 @@ fun fridge(navController: NavController) {
 
 
                 ) {
-                    var list = Resources.chosenIngredients
                     LazyColumn(
                         modifier = Modifier
                             .fillMaxSize(),
                         verticalArrangement = Arrangement.spacedBy(16.dp)
                     ) {
-                        items(list) { item ->
+                        items(chosenIngredients) { item ->
                             Row(
                                 modifier = Modifier
                                     .fillMaxWidth(),
@@ -217,9 +224,10 @@ fun fridge(navController: NavController) {
                                     fontFamily = FontFamily.Monospace
                                 )
                                 Button(onClick = {
-                                    Resources.chosenIngredients.remove(
+                                    chosenIngredients.remove(
                                         item
                                     )
+                                    Resources.viewModel.deleteIngredient(item.id)
                                 }) {
 
                                 }

+ 71 - 41
EatWell/app/src/main/java/com/example/eatwell/view/mainActivity/Home.kt

@@ -32,6 +32,7 @@ import androidx.compose.material.icons.Icons
 import androidx.compose.material.icons.filled.ArrowDropDown
 import androidx.compose.material.icons.filled.DateRange
 import androidx.compose.material.icons.filled.Home
+import androidx.compose.material.icons.filled.KeyboardArrowDown
 import androidx.compose.material.icons.filled.Lock
 import androidx.compose.material.icons.filled.MoreVert
 import androidx.compose.material.icons.filled.ShoppingCart
@@ -56,6 +57,7 @@ import androidx.compose.ui.res.vectorResource
 import androidx.compose.ui.text.font.FontFamily
 import androidx.compose.ui.text.font.FontWeight.Companion.Bold
 import androidx.compose.ui.text.style.TextAlign
+import androidx.navigation.NavController
 import androidx.navigation.NavHostController
 import coil.compose.rememberImagePainter
 import com.example.eatwell.R
@@ -65,11 +67,13 @@ import com.example.eatwell.model.Category
 import com.example.eatwell.model.Diets
 import com.example.eatwell.model.Recipes
 import com.example.eatwell.view.MainViewModel
+import com.example.eatwell.view.ui.theme.fon2
+import com.example.eatwell.view.ui.theme.knopka
 import com.example.eatwell.view.ui.theme.text_1
 import io.github.jan.supabase.gotrue.auth
 
-var  dietId: Int = 0
-var categoryId: Int = 0
+var  dietId: Int = 1
+var categoryId: Int = 1
 var flag: Boolean = false;
 
 
@@ -77,14 +81,19 @@ var flag: Boolean = false;
 @OptIn(ExperimentalMaterial3Api::class)
 //@Preview(showBackground = true)
 @Composable
-fun home(navController: NavHostController) {
+fun home(navController: NavController) {
     var categoryExpanded by remember { mutableStateOf(false) }
     var expanded by remember { mutableStateOf(false) }
     var expandedCat by remember { mutableStateOf(false) }
     var selectedOption: String
+    var selectedCategory: String
 
-    for (i in 0..Resources.recipes.size-1){
-        Resources.chosenRecipes.add(Resources.recipes.get(i))
+    var list = remember { mutableStateListOf<Recipes>() }
+    if(!flag) {
+        for (i in 0..Resources.recipes.size - 1) {
+            list.add(Resources.recipes.get(i))
+        }
+        flag = true
     }
 
     Log.d("Диеты home: ", Resources.diets.toString())
@@ -103,22 +112,20 @@ fun home(navController: NavHostController) {
         Box(
             modifier = Modifier
                 .width(359.dp)
-                .height(730.dp)
+                .height(800.dp)
                 .clip(RoundedCornerShape(30.dp))
                 .background(white)
                 .align(Alignment.TopCenter) //   .shadow(elevation = 10.dp, shape = RoundedCornerShape(30.dp))
-                .padding(16.dp)
-
-
+                .padding(1.dp)
         ) {
             Column(
                 modifier = Modifier
                     .fillMaxSize()
                     .background(white)
-                    .padding(top = 50.dp, start = 15.dp, end = 15.dp)
+                    .padding(top = 15.dp, start = 15.dp, end = 15.dp)
             ) {
                 Row(
-                    modifier = Modifier.fillMaxWidth(),
+                    modifier = Modifier.fillMaxWidth().padding(vertical = 15.dp),
                     horizontalArrangement = Arrangement.SpaceBetween,
                     verticalAlignment = Alignment.CenterVertically
                 ) {
@@ -142,9 +149,11 @@ fun home(navController: NavHostController) {
                             )
                             IconButton(onClick = { expanded = true }) {
                                 Icon(
-                                    painter = painterResource(id = R.drawable.img_5),
-                                    contentDescription = "Показать меню",
-                                    tint = text_1
+                                    imageVector = if (expanded) Icons.Default.KeyboardArrowDown else Icons.Default.ArrowDropDown, // Используем векторную иконку
+                                    contentDescription = "Показать/Скрыть меню",
+                                    tint = text_1,
+                                    modifier = Modifier.size(24.dp)
+
                                 )
                             }
                             DropdownMenu(
@@ -156,11 +165,17 @@ fun home(navController: NavHostController) {
 
                                 Resources.diets.forEach { diet ->
                                     DropdownMenuItem(
-                                        text = { Text(diet.name) },
+                                        text = { Text(diet.name) }, // Указываем текст через `text` параметр
                                         onClick = {
-                                            selectedOption = diet.name
+                                            selectedOption = diet.name // Устанавливаем выбранный элемент
                                             dietId = diet.id
-                                            defineRecipes()
+                                            list.clear()
+                                            for(i in 0..Resources.recipes.size-1){
+                                                if(Resources.recipes.get(i).diets == dietId && Resources.recipes.get(i).category == categoryId){
+                                                    Log.d("Добавили: " ," ")
+                                                    list.add(Resources.recipes.get(i))
+                                                }
+                                            }
                                             expanded = false
                                         }
                                     )
@@ -190,20 +205,31 @@ fun home(navController: NavHostController) {
                                 Spacer(modifier = Modifier.width(8.dp))
                                 IconButton(onClick = { expandedCat = true }) {
                                     Icon(
-                                        painter = painterResource(id = R.drawable.img_5),
-                                        contentDescription = "Показать меню",
-                                        tint = text_1
+                                        imageVector = if (expandedCat) Icons.Default.KeyboardArrowDown else Icons.Default.ArrowDropDown, // Используем векторную иконку
+                                        contentDescription = "Показать/Скрыть меню",
+                                        tint = text_1,
+                                        modifier = Modifier.size(44.dp)
                                     )
                                 }
                                 DropdownMenu(
                                     expanded = expandedCat,
                                     onDismissRequest = { expandedCat = false }
                                 ) {
-                                    for (i in 0..Resources.categories.size - 1) {
-                                        Text(
-                                            text = Resources.categories.get(i).name,
-                                            fontSize = 18.sp,
-                                            modifier = Modifier.padding(10.dp)
+                                    Resources.categories.forEach { categories ->
+                                        DropdownMenuItem(
+                                            text = { Text(categories.name) }, // Указываем текст через `text` параметр
+                                            onClick = {
+                                                selectedCategory = categories.name // Устанавливаем выбранный элемент
+                                                categoryId = categories.id
+                                                list.clear()
+                                                for(i in 0..Resources.recipes.size-1){
+                                                    if(Resources.recipes.get(i).diets == dietId && Resources.recipes.get(i).category == categoryId){
+                                                        Log.d("Добавили: " ," ")
+                                                        list.add(Resources.recipes.get(i))
+                                                    }
+                                                }
+                                                expandedCat = false
+                                            }
                                         )
                                     }
                                 }
@@ -211,17 +237,26 @@ fun home(navController: NavHostController) {
                         }
                     }
 
+
+                Box(
+                    modifier = Modifier
+                        .width(359.dp)
+                        .height(630.dp)
+                        .clip(RoundedCornerShape(30.dp))
+                        .background(fon2)
+                        .padding(15.dp)
+
+                ) {
                     Column(
                         modifier = Modifier
                             .size(width = 400.dp, height = 750.dp)
-                            .background(Color.LightGray)
+                            .background(fon2)
+
                     ) {
 
                         LazyColumn(
                             modifier = Modifier.fillMaxSize()
                         ) {
-                            var list: MutableList<Recipes> = Resources.chosenRecipes
-
                             items(list.size) { itemId ->
                                 Image(
                                     painter = rememberImagePainter(
@@ -238,7 +273,9 @@ fun home(navController: NavHostController) {
                                             end = 7.5.dp
                                         )
                                         .fillMaxWidth()
-                                        .height(100.dp)
+                                        .height(150.dp)
+                                        .clip(RoundedCornerShape(50.dp))
+
                                 )
                                 Text(
                                     modifier = Modifier
@@ -257,7 +294,7 @@ fun home(navController: NavHostController) {
                                         .fillMaxWidth()
                                         .padding(top = 7.5.dp),
                                     text = list.get(itemId).calories,
-                                    color = Color.Gray,
+                                    color = knopka,
                                     fontSize = 15.sp,
                                     fontWeight = FontWeight.Bold,
                                     fontFamily = FontFamily.Monospace,
@@ -267,7 +304,7 @@ fun home(navController: NavHostController) {
                         }
                     }
 
-                }
+                }}
             }
         }
 
@@ -283,7 +320,7 @@ fun home(navController: NavHostController) {
                     .clip(RoundedCornerShape(30.dp))
                     .background(white)
                     .align(Alignment.BottomCenter)
-                    .padding(bottom = 50.dp)
+                    .padding(bottom = 20.dp)
             ) {
 
                 // Черная линия
@@ -319,7 +356,7 @@ fun home(navController: NavHostController) {
                     ) {
                         IconButton(
                             onClick = {
-                                navController.navigate("MyPlan")
+                               navController.navigate("MyPlan")
                                 {
                                 }
                             }, modifier = Modifier.size(32.dp)
@@ -379,12 +416,5 @@ fun home(navController: NavHostController) {
         }
     }
 
-fun defineRecipes(){
-    Resources.chosenRecipes.clear()
-    for(i in 0..Resources.recipes.size-1){
-        if(Resources.recipes.get(i).diets == dietId && Resources.recipes.get(i).category == categoryId){
-            Resources.chosenRecipes.add(Resources.recipes.get(i))
-        }
-    }
-}
+
 

+ 2 - 1
EatWell/app/src/main/java/com/example/eatwell/view/mainActivity/navigation.kt

@@ -7,7 +7,8 @@ import androidx.navigation.compose.rememberNavController
 
 
 @Composable
-fun Navigation() {    val navController = rememberNavController()
+fun Navigation() {
+    val navController = rememberNavController()
     NavHost(        navController = navController,
         startDestination = "Start"    ) {
         composable("auth"){            //auth(navController)