Browse Source

visualImprovAndAddMainScreen

x1tosi 13 hours ago
parent
commit
4c87da5e3d

+ 4 - 0
app/build.gradle.kts

@@ -1,6 +1,7 @@
 plugins {
     alias(libs.plugins.android.application)
     alias(libs.plugins.jetbrains.kotlin.android)
+    kotlin("plugin.serialization") version "1.9.22"
 }
 
 android {
@@ -62,6 +63,7 @@ dependencies {
     implementation(libs.androidx.navigation.compose)
     implementation(libs.play.services.auth)
     implementation(libs.play.services.analytics.impl)
+    implementation(libs.volley)
     testImplementation(libs.junit)
     androidTestImplementation(libs.androidx.junit)
     androidTestImplementation(libs.androidx.espresso.core)
@@ -78,4 +80,6 @@ dependencies {
 
     implementation(libs.ktor.ktor.client.okhttp)
 
+
+    implementation (libs.coil.compose)
 }

+ 1 - 1
app/src/main/java/com/example/fitmarcetplacekuzminiv/Model/Products.kt

@@ -7,7 +7,7 @@ data class Product(
     val id: Int,
     val name: String,
     val description: String,
-    val imageurl: String,
+    val imageurl: String?,
     val category: String,
     val instock: String,
     val createddat: String,

+ 77 - 85
app/src/main/java/com/example/fitmarcetplacekuzminiv/Screens/mainScreen.kt

@@ -1,100 +1,92 @@
 package com.example.fitmarcetplacekuzminiv.Screens
 
-
-import androidx.compose.foundation.layout.*
-import androidx.compose.material3.*
+import android.util.Log
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.foundation.lazy.items
+import androidx.compose.material3.CircularProgressIndicator
+import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.unit.dp
-import androidx.compose.ui.tooling.preview.Preview
+import coil.compose.AsyncImagePainter
+import coil.compose.rememberAsyncImagePainter
+import coil.request.ImageRequest
+import coil.size.Size
+import com.example.fitmarcetplacekuzminiv.Model.Product
+import com.example.fitmarcetplacekuzminiv.domain.Constants
+import io.github.jan.supabase.postgrest.from
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
 
 @Composable
 fun MainScreen() {
-    Box(
-        modifier = Modifier.fillMaxSize(),
-        contentAlignment = Alignment.Center
-    ) {
-        Column(
-            horizontalAlignment = Alignment.CenterHorizontally,
-            verticalArrangement = Arrangement.Center,
-            modifier = Modifier
-                .fillMaxWidth()
-                .padding(16.dp)
-        ) {
-
+    var products by remember { mutableStateOf<List<Product>>(listOf()) }
 
-            Spacer(modifier = Modifier.height(24.dp))
-            Button(onClick = {
-                // Обработка нажатия кнопки "Login"
-            }) {
-                Text("Good")
+    LaunchedEffect(Unit) {
+        withContext(Dispatchers.IO) {
+            products = Constants.supabase.from("products")
+                .select()
+                .decodeList<Product>()
+            products.forEach { product ->
+                Log.d("Products", product.name)
             }
         }
     }
-}
 
-@Preview(showBackground = true)
-@Composable
-fun PreviewMainScreen() {
-    MainScreen()
-}
+    LazyColumn {
+        items(
+            products,
+            key = { product -> product.id },
+        ) { product ->
+            val imageState = rememberAsyncImagePainter(
+                model = ImageRequest.Builder(LocalContext.current).data(product.imageurl)
+                    .size(Size.ORIGINAL).build()
+            ).state
+
+            if (imageState is AsyncImagePainter.State.Error) {
+                Box(
+                    modifier = Modifier
+                        .fillMaxWidth()
+                        .height(200.dp),
+                    contentAlignment = Alignment.Center
+                ) {
+                    CircularProgressIndicator()
+                }
+            }
 
-//@Composable
-//fun MainScreen(mainViewModel: MainViewModel = viewModel()) {
-//    // Fetch products when the screen is first displayed
-//    LaunchedEffect(Unit) {
-//        mainViewModel.fetchProducts()
-//    }
-//
-//    // Observe products from the ViewModel
-//    val products by mainViewModel.products.collectAsState()
-//
-//    // Display loading indicator while data is being fetched
-//    if (products.isEmpty()) {
-//        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
-//            CircularProgressIndicator()
-//        }
-//    } else {
-//        LazyColumn {
-//            items(products) { product ->
-//                ProductItem(product)
-//            }
-//        }
-//    }
-//}
-//
-//@Composable
-//fun ProductItem(product: Product) {
-//    Row(
-//        modifier = Modifier
-//            .fillMaxWidth()
-//            .padding(8.dp),
-//        verticalAlignment = Alignment.CenterVertically
-//    ) {
-//        Column(modifier = Modifier.weight(1f)) {
-//            Text(text = product.name, style = MaterialTheme.typography.titleMedium)
-//            Text(text = "${product.price} USD", style = MaterialTheme.typography.bodyMedium)
-//        }
-//    }
-//}
-//
-//@Preview(showBackground = true)
-//@Composable
-//fun PreviewMainScreen() {
-//    // Simulate a list of products for preview
-////    val products = listOf(
-////        Product(id = 1, name = "Product 1", 19.99),
-////        Product(id = 2, name = "Product 2", 29.99),
-////    )
-////    MainScreenContent(products)
-//}
-//
-//@Composable
-//fun MainScreenContent(products: List<Product>) {
-//    LazyColumn {
-//        items(products) { product ->
-//            ProductItem(product)
-//        }
-//    }
-//}
+            if (imageState is AsyncImagePainter.State.Success) {
+                Image(
+                    modifier = Modifier
+                        .fillMaxWidth()
+                        .height(200.dp),
+                    painter = imageState.painter,
+                    contentDescription = "",
+                    contentScale = ContentScale.Crop
+                )
+            }
+
+            Text(
+                text = product.name,
+                modifier = Modifier.padding(8.dp),
+            )
+
+            Text(
+                text = "$${product.price}",
+                modifier = Modifier.padding(start = 8.dp, bottom = 8.dp),
+            )
+        }
+    }
+}

+ 4 - 0
gradle/libs.versions.toml

@@ -1,6 +1,7 @@
 [versions]
 agp = "8.5.1"
 bom = "2.6.1"
+coilCompose = "2.2.2"
 kotlin = "1.9.0"
 coreKtx = "1.13.1"
 junit = "4.13.2"
@@ -13,10 +14,12 @@ composeBom = "2024.04.01"
 navigationCompose = "2.8.0"
 playServicesAuth = "21.2.0"
 playServicesAnalyticsImpl = "18.1.0"
+volley = "1.2.1"
 
 [libraries]
 androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
 bom = { module = "io.github.jan-tennert.supabase:bom", version.ref = "bom" }
+coil-compose = { module = "io.coil-kt:coil-compose", version.ref = "coilCompose" }
 junit = { group = "junit", name = "junit", version.ref = "junit" }
 androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
 androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
@@ -37,6 +40,7 @@ supabase-gotrue-kt = { module = "io.github.jan-tennert.supabase:gotrue-kt" }
 supabase-postgrest-kt = { module = "io.github.jan-tennert.supabase:postgrest-kt" }
 play-services-auth = { group = "com.google.android.gms", name = "play-services-auth", version.ref = "playServicesAuth" }
 play-services-analytics-impl = { group = "com.google.android.gms", name = "play-services-analytics-impl", version.ref = "playServicesAnalyticsImpl" }
+volley = { group = "com.android.volley", name = "volley", version.ref = "volley" }
 
 [plugins]
 android-application = { id = "com.android.application", version.ref = "agp" }