bl.txt 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.example.task1.view.mainAct.components
  2. import android.media.Image
  3. import androidx.compose.foundation.Image
  4. import androidx.compose.foundation.layout.Box
  5. import androidx.compose.foundation.layout.fillMaxWidth
  6. import androidx.compose.foundation.layout.height
  7. import androidx.compose.foundation.layout.padding
  8. import androidx.compose.foundation.lazy.LazyColumn
  9. import androidx.compose.foundation.lazy.items
  10. import androidx.compose.material3.CircularProgressIndicator
  11. import androidx.compose.material3.Text
  12. import androidx.compose.runtime.Composable
  13. import androidx.compose.runtime.LaunchedEffect
  14. import androidx.compose.runtime.getValue
  15. import androidx.compose.runtime.mutableStateOf
  16. import androidx.compose.runtime.remember
  17. import androidx.compose.runtime.setValue
  18. import androidx.compose.ui.Alignment
  19. import androidx.compose.ui.Modifier
  20. import androidx.compose.ui.layout.ContentScale
  21. import androidx.compose.ui.platform.LocalContext
  22. import androidx.compose.ui.semantics.Role.Companion.Image
  23. import androidx.compose.ui.unit.dp
  24. import coil.compose.AsyncImagePainter
  25. import coil.compose.rememberAsyncImagePainter
  26. import coil.request.ImageRequest
  27. import coil.size.Size
  28. import com.example.task1.Model.Branches
  29. import com.example.task1.domain.Constants
  30. import io.github.jan.supabase.postgrest.postgrest
  31. import kotlinx.coroutines.Dispatchers
  32. import kotlinx.coroutines.withContext
  33. @Composable
  34. fun BranchesListTest()
  35. {
  36. var branches by remember { mutableStateOf<List<Branches>>(listOf()) }
  37. /* var bucket: ByteArray? = null
  38. var bytes:ByteArray? = null
  39. var files: List<BucketItem>? = null*/
  40. LaunchedEffect(Unit) {
  41. withContext(Dispatchers.IO) {
  42. try {
  43. branches = Constants.supabase.postgrest.from("Branches").
  44. select().decodeList()
  45. branches.forEach{it->
  46. println(it.address)
  47. }
  48. } catch (e: Exception) {
  49. println("Error")
  50. println(e.message)
  51. }
  52. }
  53. }
  54. LazyColumn {
  55. items(
  56. branches,
  57. key = { branch -> branch.id }
  58. )
  59. {
  60. branch ->
  61. val imageState = rememberAsyncImagePainter(
  62. model = ImageRequest.Builder(LocalContext.current).data(branch.image)
  63. .size(Size.ORIGINAL).build()
  64. ).state
  65. if (imageState is AsyncImagePainter.State.Error) {
  66. Box(
  67. modifier = Modifier
  68. .fillMaxWidth()
  69. .height(200.dp),
  70. contentAlignment = Alignment.Center
  71. ) {
  72. CircularProgressIndicator()
  73. }
  74. }
  75. if (imageState is AsyncImagePainter.State.Success) {
  76. Image(
  77. modifier = Modifier
  78. .fillMaxWidth()
  79. .height(200.dp),
  80. painter = imageState.painter,
  81. contentDescription = "",
  82. contentScale = ContentScale.Crop
  83. )
  84. }
  85. Text(
  86. branch.address,
  87. modifier = Modifier.padding(8.dp),
  88. )
  89. }
  90. }
  91. }