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