CoolVery 1 месяц назад
Родитель
Сommit
4aa7a3cf37

+ 52 - 13
app/src/main/java/com/example/collectionofshortstories/view/catalog/Catalog.kt

@@ -1,9 +1,16 @@
 package com.example.collectionofshortstories.view.catalog
 
 import android.util.Log
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.foundation.layout.padding
 import androidx.compose.foundation.lazy.LazyColumn
 import androidx.compose.foundation.lazy.items
+import androidx.compose.material3.Button
 
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
@@ -14,6 +21,7 @@ import androidx.compose.runtime.key
 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.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
@@ -25,24 +33,55 @@ import io.github.jan.supabase.postgrest.from
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
 
-
 @Composable
 fun Catalog(navHostController: NavHostController) {
     val viewModel = CatalogViewModel()
-    viewModel.get()
+
+
     val articles by viewModel.articles.collectAsState(initial = emptyList())
-    LazyColumn {
-        items (
-            articles,
-            key = { articleDate -> articleDate.id }
-        ) { articleDate ->
-            Text(
-                text = articleDate.title,
-                modifier = Modifier
-                    .padding(30.dp)
-            )
+    val user_role by viewModel.role_name.collectAsState(initial = "")
 
-        }
+    LaunchedEffect(Unit) {
+        viewModel.GetArticles()
+        viewModel.GetUserRole()
     }
 
+    // Кэширование значения для отображения кнопки
+    val showButton = remember(user_role) { user_role == "Писатель" }
+
+    Box() {
+        LazyColumn(
+            modifier = Modifier
+                .fillMaxSize() // Занимает весь экран
+                .padding(16.dp) // Добавлен отступ для LazyColumn
+        ) {
+            items(
+                articles,
+                key = { articleDate -> articleDate.id }
+            ) { articleDate ->
+                Row(
+                    horizontalArrangement = Arrangement.SpaceAround,
+                    modifier = Modifier
+                        .fillMaxWidth()
+                ) {
+                    Text(
+                        text = articleDate.title,
+                        modifier = Modifier
+                            .padding(30.dp)
+                            .weight(1f)
+                    )
+                }
+            }
+        }
+        if (showButton) {
+            Button(
+                onClick = { /* Действия при нажатии на кнопку */ },
+                modifier = Modifier
+                    .align(Alignment.BottomEnd)
+                    .padding(0.dp, 0.dp, 40.dp, 80.dp)
+            ) {
+                Text("Добавить рассказ")
+            }
+        }
+    }
 }

+ 38 - 4
app/src/main/java/com/example/collectionofshortstories/view/catalog/CatalogViewModel.kt

@@ -4,6 +4,7 @@ import android.util.Log
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.MutableState
+import androidx.compose.runtime.State
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
@@ -13,6 +14,9 @@ import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import com.example.collectionofshortstories.domain.untils.Constants
 import com.example.collectionofshortstories.model.Article
+import com.example.collectionofshortstories.model.Role
+import com.example.collectionofshortstories.model.User
+import io.github.jan.supabase.gotrue.auth
 import io.github.jan.supabase.postgrest.from
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.MutableStateFlow
@@ -25,17 +29,47 @@ class CatalogViewModel(): ViewModel() {
     private val _articles: MutableStateFlow<List<Article>> = MutableStateFlow( listOf() )
     val articles: StateFlow<List<Article>> = _articles.asStateFlow()
 
-    fun get() {
+    private val _role_name: MutableStateFlow<String> = MutableStateFlow("")
+    val role_name: StateFlow<String> = _role_name.asStateFlow()
+
+    fun GetArticles() {
         viewModelScope.launch {
             withContext(Dispatchers.IO) {
                 val updatedCategories = Constants.supabase.from("articles")
                     .select()
-                Log.d("OOOOOOOOOOOOOOOOOO", updatedCategories.data.toString())
                 _articles.value = updatedCategories.decodeList<Article>()
-                articles.value!!.forEach { it ->
-                    Log.d("Cdsdsdsdssds", it.title)
+            }
+        }
+    }
+
+    fun GetUserRole() {
+        viewModelScope.launch {
+            try {
+                val userDate = Constants.supabase.from("users").select {
+                    filter {
+                        eq("id", Constants.supabase.auth.currentUserOrNull()!!.id)
+                    }
+                }
+                val userRoleId = userDate.decodeSingle<User>().id_role
+                Log.d("DSDSDSDSD", userRoleId.toString())
+                val roleDate = Constants.supabase.from("roles")
+                    .select {
+                        filter {
+                            eq("id", userRoleId)
+                        }
+                    }.decodeSingleOrNull<Role>()
+                if (roleDate == null) {
+                    Log.d("DSDSDSDSD","BAS")
+                }
+                else {
+                    Log.d("DSDSDSDSD","TES")
+                    _role_name.value = roleDate.title
                 }
+            } catch (e: Exception){
+                Log.d("DSDSDSDSD","пипец")
             }
+
+
         }
     }
 }