Explorar el Código

App: filter completed

БезаеваАА hace 1 semana
padre
commit
617b023080

+ 2 - 0
Project_exvesta/app/build.gradle.kts

@@ -77,6 +77,8 @@ dependencies {
 
     implementation(platform(libs.bom.v261))
     implementation(libs.io.github.jan.tennert.supabase.postgrest.kt2)
+    implementation(libs.io.github.jan.tennert.supabase.postgrest.kt)
+    implementation(libs.io.github.jan.tennert.supabase.gotrue.kt)
     implementation(libs.io.github.jan.tennert.supabase.gotrue.kt2)
     implementation(libs.github.realtime.kt)
     implementation(libs.storage.kt.v190)

+ 26 - 20
Project_exvesta/app/src/main/java/com/example/exvesta/Screans/Filters.kt

@@ -53,17 +53,23 @@ import androidx.compose.material3.RadioButton
 import androidx.compose.foundation.layout.*
 import androidx.compose.material.*
 import androidx.compose.runtime.*
+import androidx.lifecycle.viewmodel.compose.viewModel
 
 @OptIn(ExperimentalLayoutApi::class)
 @Composable
-public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
+public  fun Filters(   onApplyFilters: (Type_subtype_dublicate?, Season_dublicate?,  Style_dublicate?) -> Unit,
+                       onDismiss: () -> Unit) {
     var styledubs by remember { mutableStateOf<List<Style_dublicate>>(listOf()) }
     var subdubtypes by remember { mutableStateOf<List<Type_subtype_dublicate>>(listOf()) }
     var typedubs by remember { mutableStateOf<List<Type_dublicate>>(listOf()) }
     var seasondubs by remember { mutableStateOf<List<Season_dublicate>>(listOf()) }
-    var selectedSeasondub by remember { mutableStateOf<Season_dublicate?>(null) }
-    var selectedStyledub by remember { mutableStateOf<Style_dublicate?>(null) }
-    var selectedTypedub by remember { mutableStateOf<Type_subtype_dublicate?>(null) }
+    var selectedStyle by remember { mutableStateOf<Style_dublicate?>(null) }
+    var selectedSubtype by remember { mutableStateOf<Type_subtype_dublicate?>(null) }
+    var selectedSeason by remember { mutableStateOf<Season_dublicate?>(null) }
+
+    var selectedsubId by remember { mutableStateOf(0) }
+    var selectedseasonId by remember { mutableStateOf(0) }
+    val viewModel = MainViewModel()
     LaunchedEffect(Unit) {
         withContext(Dispatchers.IO) {
             subdubtypes = Constants.supabase.from("Type_subtype_dublicate")
@@ -82,7 +88,7 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
             }
         }
     }
-    if (isVisible) {
+
         Column(
             modifier = Modifier
                 .fillMaxHeight()
@@ -104,7 +110,7 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
 
             // Кнопка для закрытия фильтров
             Button(
-                onClick = { onClose() }, // Закрыть выбор фильтров
+                onClick = { onDismiss() }, // Закрыть выбор фильтров
                 modifier = Modifier.padding(top = 16.dp)
                     .background( Color(0xFFD05656),shape = ButtonDefaults.shape),
                 colors = ButtonDefaults.buttonColors(containerColor = Color.Transparent)
@@ -163,13 +169,13 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
                                         modifier = Modifier
                                             .padding(4.dp) // Отступы между элементами
                                             .selectable(
-                                                selected = (seasondub == selectedSeasondub),
-                                                onClick = { selectedSeasondub = seasondub }
+                                                selected = (seasondub == selectedSeason),
+                                                onClick = { selectedSeason = seasondub }
                                             )
                                     ) {
                                         RadioButton(
-                                            selected = (seasondub == selectedSeasondub),
-                                            onClick = { selectedSeasondub = seasondub }
+                                            selected = (seasondub == selectedSeason),
+                                            onClick = { selectedSeason = seasondub }
                                         )
                                         Text(
                                             text = seasondub.title_season,
@@ -219,13 +225,13 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
                                             modifier = Modifier
 
                                                 .selectable(
-                                                    selected = (styledub == selectedStyledub),
-                                                    onClick = { selectedStyledub = styledub }
+                                                    selected = (styledub == selectedStyle),
+                                                    onClick = { selectedStyle = styledub }
                                                 )
                                         ) {
                                             RadioButton(
-                                                selected = (styledub == selectedStyledub),
-                                                onClick = { selectedStyledub = styledub }
+                                                selected = (styledub == selectedStyle),
+                                                onClick = { selectedStyle = styledub }
                                             )
                                             Text(
                                                 text = styledub.title_style,
@@ -282,8 +288,8 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
                                                     modifier = Modifier
 
                                                         .selectable(
-                                                            selected = (subtypedub == selectedTypedub),
-                                                            onClick = { selectedTypedub = subtypedub }
+                                                            selected = (subtypedub ==  selectedSubtype),
+                                                            onClick = { selectedSubtype = subtypedub }
                                                         )
                                                         .padding(top = 14.dp)
 
@@ -291,8 +297,8 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
 
                                                 ) {
                                                     RadioButton(
-                                                        selected = (subtypedub == selectedTypedub),
-                                                        onClick = { selectedTypedub = subtypedub }
+                                                        selected = (subtypedub ==  selectedSubtype),
+                                                        onClick = { selectedSubtype = subtypedub }
                                                     )
                                                     Text(
                                                         text = subtypedub.sub_type,
@@ -320,7 +326,7 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
                 }
 
                 Button(
-                    onClick = { /* Действие при нажатии */ },
+                    onClick = {  onApplyFilters(selectedSubtype, selectedSeason, selectedStyle) },
                     modifier = Modifier
                         .align(Alignment.BottomCenter)
                         .fillMaxWidth() // Занять всю ширину
@@ -346,5 +352,5 @@ public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
                 }
             }
         }
-    }
+
 }

+ 16 - 6
Project_exvesta/app/src/main/java/com/example/exvesta/Screans/Home.kt

@@ -79,7 +79,8 @@ fun Home(navHost: NavHostController) {
     var seasondub by remember { mutableStateOf<List<Season_dublicate>>(listOf()) }
     var publicthings by remember { mutableStateOf<List<Publicthings>>(listOf()) }
     var styledub by remember { mutableStateOf<List<Style_dublicate>>(listOf()) }
-
+    var recpubthingsList by remember { mutableStateOf<List<Publicthings>>(listOf()) }
+    var filteredPublicThings by remember { mutableStateOf<List<Publicthings>>(listOf()) }
 
     LaunchedEffect(Unit) {
         withContext(Dispatchers.IO) {
@@ -97,9 +98,9 @@ fun Home(navHost: NavHostController) {
                 Log.d("ts", it.title)
             }
         }
-    }
-
+        filteredPublicThings = publicthings
 
+    }
     var isFilterSheetVisible by remember { mutableStateOf(false) }
     var drawerState = rememberDrawerState(DrawerValue.Closed)
     val scope = rememberCoroutineScope()
@@ -245,8 +246,7 @@ fun Home(navHost: NavHostController) {
             ) {
 
                 items(
-                    publicthings,
-                    key = { publicthing -> publicthing.id },
+                    filteredPublicThings
                 ) { publicthing ->
                     val imageState = rememberAsyncImagePainter(
                         model = ImageRequest.Builder(LocalContext.current).data(publicthing.img)
@@ -399,8 +399,18 @@ fun Home(navHost: NavHostController) {
     }
     if (isFilterSheetVisible) {
         Column {
-            Filters(isVisible = isFilterSheetVisible, onClose = { isFilterSheetVisible = false }) // Ваш компонент фильтров
+            Filters(
+                onApplyFilters = { selectedSubtype, selectedSeason, selectedStyle ->
+                    // Применяем фильтры и обновляем отображение
+                    filteredPublicThings = publicthings.filter { publicThing ->
+                        (selectedSubtype == null || publicThing.id_subtype == selectedSubtype.id) &&
+                                (selectedSeason == null || publicThing.seson_id == selectedSeason.id)&&
+                                (selectedStyle == null || publicThing.id_style == selectedStyle.id)
+                    }
 
+                    isFilterSheetVisible = false // Закрываем окно фильтров
+                },
+                onDismiss = { isFilterSheetVisible = false })
 
         }
     }

+ 41 - 34
Project_exvesta/app/src/main/java/com/example/exvesta/Screans/MainViewModel.kt

@@ -1,26 +1,36 @@
 package com.example.exvesta.Screans
 
+import android.util.Log
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
 import androidx.compose.runtime.setValue
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
+import com.example.exvesta.Screans.items.Itempublicthings
 import com.example.exvesta.domain.utils.Constants
+import com.example.exvesta.domain.utils.Constants.supabase
 import com.example.exvesta.model.Publicthings
+import com.example.exvesta.model.Season_dublicate
 import com.example.exvesta.model.Type_dublicate
 import com.example.exvesta.model.Type_subtype_dublicate
 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.SupabaseClient
+import io.github.jan.supabase.postgrest.query.Columns
 import kotlinx.coroutines.Dispatchers
 import io.github.jan.supabase.postgrest.query.filter.PostgrestFilterBuilder
 import io.github.jan.supabase.postgrest.query.PostgrestQueryBuilder
 import io.github.jan.supabase.postgrest.result.PostgrestResult
+import io.github.jan.supabase.storage.publicStorageItem
+import kotlinx.coroutines.awaitAll
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import kotlinx.serialization.builtins.serializer
 
 class MainViewModel(): ViewModel() {
     private val _loginResult = MutableLiveData<Boolean>()
@@ -82,46 +92,43 @@ class MainViewModel(): ViewModel() {
         }
         return _loginResult.value ?: false
     }
-}
-
-class FiltersViewModel : ViewModel() {
-    var selectedType by mutableStateOf<Type_dublicate?>(null)
-    var selectedSubType by mutableStateOf<Type_subtype_dublicate?>(null)
-    var filteredData by mutableStateOf<List<Publicthings>>(listOf())
 
-    class FiltersViewModel : ViewModel() {
+    fun getPublicthings(seson_id: Int, id_subtype:Int): List<Publicthings> {
+        var recpubthingsList: List<Publicthings>
         var selectedType by mutableStateOf<Type_dublicate?>(null)
         var selectedSubType by mutableStateOf<Type_subtype_dublicate?>(null)
         var filteredData by mutableStateOf<List<Publicthings>>(listOf())
+        var selectedSeasondub by  mutableStateOf<Season_dublicate?>(null)
+        recpubthingsList = ArrayList()
+        viewModelScope.launch {
 
-        fun applyFilters() {
-            viewModelScope.launch {
-                val typeId = selectedType?.id
-                val subTypeId = selectedSubType?.id
-
-                // Выполняем запрос к базе данных
-                filteredData = withContext(Dispatchers.IO) {
-                    // Создаем запрос
-                    var query = Constants.supabase.from("Publicthings").select()
-
-                    // Применяем фильтры, если они указаны
-//                    if (typeId != null) {
-//                        query = query.eq("type_id", typeId) // Фильтр по type_id
-//                    }
-//                    if (subTypeId != null) {
-//                        query = query.eq("subtype_id", subTypeId) // Фильтр по subtype_id
-//                    }
-
-                    // Выполняем запрос и декодируем результат
-                    try {
-                        query.decodeList<Publicthings>()
-                    } catch (e: Exception) {
-                        // Обработка ошибок, если запрос не удался
-                        e.printStackTrace()
-                        emptyList() // Возвращаем пустой список в случае ошибки
+            try{
+                recpubthingsList = Constants.supabase.from("Publicthings").select() {
+                    filter {
+                        if(id_subtype != null && seson_id == null){
+                            Publicthings::id_subtype eq id_subtype
+                        }
+                        if(seson_id != null && id_subtype == null){
+                            Publicthings::seson_id eq id_subtype
+                        }
                     }
-                }
+
+                }.decodeList<Publicthings>()
+         Log.d("Категори ", recpubthingsList.toString())
+            }
+            catch(e:Exception){
+                Log.d("ошибка ", e.message.toString())
             }
+
+
+
+
         }
+        return recpubthingsList;
     }
-    }
+
+
+
+
+}
+