浏览代码

Add icon articles in catalog

CoolVery 1 月之前
父节点
当前提交
e063c77dce

+ 2 - 1
app/src/main/java/com/example/collectionofshortstories/model/Article.kt

@@ -7,5 +7,6 @@ data class Article (
     val id: Int,
     val title: String,
     val id_author: String,
-    val id_categories: Int
+    val id_categories: Int,
+    val textArticles: String? = null
 )

+ 6 - 0
app/src/main/java/com/example/collectionofshortstories/navigation/Navigation.kt

@@ -8,6 +8,7 @@ import androidx.navigation.compose.rememberNavController
 import com.example.collectionofshortstories.view.authorization.AuthorizationAndLogin
 import com.example.collectionofshortstories.view.catalog.Catalog
 import com.example.collectionofshortstories.view.createArticle.СreateArticle
+import com.example.collectionofshortstories.view.dateArticle.DateArticle
 import com.example.collectionofshortstories.view.start.LoadProgram
 
 @Composable
@@ -29,5 +30,10 @@ fun Navigathion() {
         composable(Routes.CREATEARTICLE) {
             СreateArticle(navController)
         }
+        composable(Routes.DATEARTICLE + "/{title}") {
+                backStackEntry ->
+            val title = backStackEntry.arguments?.getString("title") ?: ""
+            DateArticle(navController, title)
+        }
     }
 }

+ 1 - 0
app/src/main/java/com/example/collectionofshortstories/navigation/Routes.kt

@@ -5,4 +5,5 @@ object Routes {
     const val AUTH = "AuthorizationAndLogin"
     const val CATALOG = "Catalog"
     const val CREATEARTICLE = "СreateArticle"
+    const val DATEARTICLE = "DateArticle"
 }

+ 9 - 0
app/src/main/java/com/example/collectionofshortstories/view/catalog/Catalog.kt

@@ -1,6 +1,7 @@
 package com.example.collectionofshortstories.view.catalog
 
 import android.util.Log
+import androidx.compose.foundation.clickable
 import androidx.compose.foundation.layout.Arrangement
 import androidx.compose.foundation.layout.Box
 
@@ -23,6 +24,7 @@ import androidx.compose.runtime.remember
 import androidx.compose.runtime.setValue
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
+import androidx.compose.ui.text.LinkAnnotation
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
 import androidx.hilt.navigation.compose.hiltViewModel
@@ -53,16 +55,23 @@ fun Catalog(navHostController: NavHostController, viewModel: CatalogViewModel =
                 articles,
                 key = { articleDate -> articleDate.id }
             ) { articleDate ->
+
                 Row(
                     horizontalArrangement = Arrangement.SpaceAround,
                     modifier = Modifier
                         .fillMaxWidth()
+
                 ) {
+
                     Text(
                         text = articleDate.title,
+
                         modifier = Modifier
                             .padding(30.dp)
                             .weight(1f)
+                            .clickable {
+                                navHostController.navigate("${Routes.DATEARTICLE}/${articleDate.title}")
+                            }
                     )
                 }
             }

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

@@ -37,9 +37,9 @@ class CatalogViewModel @Inject constructor(): ViewModel() {
     fun GetArticles() {
         viewModelScope.launch {
             withContext(Dispatchers.IO) {
-                val updatedCategories = Constants.supabase.from("articles")
+                val updatedArticles = Constants.supabase.from("articles")
                     .select()
-                _articles.value = updatedCategories.decodeList<Article>()
+                _articles.value = updatedArticles.decodeList<Article>()
             }
         }
     }
@@ -62,4 +62,6 @@ class CatalogViewModel @Inject constructor(): ViewModel() {
             }
         }
     }
+
+
 }

+ 62 - 70
app/src/main/java/com/example/collectionofshortstories/view/createArticle/СreateArticle.kt

@@ -19,6 +19,7 @@ import androidx.compose.material3.DropdownMenuItem
 import androidx.compose.material3.Text
 import androidx.compose.material3.TextField
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.MutableState
 import androidx.compose.runtime.collectAsState
 import androidx.compose.runtime.getValue
@@ -37,20 +38,28 @@ import androidx.navigation.NavHostController
 import com.example.collectionofshortstories.view.authorization.AuthorizationViewModel
 import com.example.collectionofshortstories.view.authorization.CreateTextFolder
 
-
 @Composable
 fun СreateArticle(
     navHostController: NavHostController,
     viewModel: СreateArticleViewModel = hiltViewModel()
 ) {
     var expanded by remember { mutableStateOf(false) }
-    var selectedItem by remember { mutableStateOf("Выберите категорию") }
+    var selectedCategory by remember { mutableStateOf("Выберите категорию") }
+    var textArticle = remember { mutableStateOf("") }
+    var title = remember { mutableStateOf("") }
+
     val listCategories by viewModel.categories.collectAsState(initial = emptyList())
 
+    LaunchedEffect(key1 = viewModel.navigationTo) {
+        viewModel.navigationTo.collect { destination ->
+            destination?.let {
+                navHostController.navigate(destination)
+            }
+        }
+    }
+
     viewModel.GetCategories()
 
-    val title = remember { mutableStateOf("") }
-    val category = remember { mutableStateOf("") }
     Column(
         horizontalAlignment = Alignment.CenterHorizontally,
         modifier = Modifier
@@ -68,7 +77,7 @@ fun СreateArticle(
                 fontSize = 30.sp,
                 fontWeight = FontWeight.Bold,
                 modifier = Modifier
-                    .padding(0.dp, 50.dp, 0.dp, 30.dp)
+                    .padding(0.dp, 30.dp, 0.dp, 30.dp)
             )
             Divider(
                 modifier = Modifier.fillMaxWidth(),
@@ -84,55 +93,61 @@ fun СreateArticle(
                 color = Color.Black,
                 fontSize = 30.sp
             )
-            Row(
-                horizontalArrangement = Arrangement.spacedBy(10.dp),
-                modifier = Modifier.fillMaxWidth()
+
+            TextField(
+                value = title.value,
+                onValueChange = { titleNew -> title.value = titleNew },
+                maxLines = 1,
+                placeholder = { Text("Название") },
+                modifier = Modifier
+                    .padding(0.dp, 30.dp, 0.dp, 0.dp)
+            )
+            TextField(
+                value = textArticle.value,
+                onValueChange = { textArticleNew -> textArticle.value = textArticleNew },
+                maxLines = 1,
+                placeholder = { Text("Текст рассказа") },
+                modifier = Modifier
+                    .padding(0.dp, 30.dp, 0.dp, 0.dp)
+                    .weight(6f)
+            )
+
+            Button(
+                modifier = Modifier
+                    .padding(0.dp, 10.dp),
+                onClick = { expanded = !expanded }
             ) {
-                TextField(
-                    value = title.value,
-                    onValueChange = { newEmail -> title.value = newEmail },
-                    maxLines = 1,
-                    placeholder = { Text("Название") },
-                    modifier = Modifier
-                        .padding(0.dp, 30.dp, 0.dp, 0.dp)
-                        .weight(2f)
-                )
-                Column (
-                ) {
-                    Button(
-                        onClick = { expanded = !expanded }
-                    ) {
-                        Text(text = selectedItem)
-                    }
-                    DropdownMenu(
-                        expanded = expanded,
-                        onDismissRequest = { expanded = false },
+                Text(text = selectedCategory)
+            }
+            DropdownMenu(
+                expanded = expanded,
+                onDismissRequest = { expanded = false },
 
-                        ) {
-                        for (categories in listCategories) {
-                            DropdownMenuItem(onClick = {
-                                selectedItem = categories.title
-                                expanded = false
-                            },
-                                text = { Text(categories.title) }
-                            )
-                        }
-                    }
+                ) {
+                for (categories in listCategories) {
+                    DropdownMenuItem(onClick = {
+                        selectedCategory = categories.title
+                        expanded = false
+                    },
+                        text = { Text(categories.title) }
+                    )
                 }
+
+
             }
             Button(
                 onClick = {
+                    viewModel.InsertNewArticle(title.value, textArticle.value, selectedCategory)
+                },
+                colors = ButtonDefaults.buttonColors(
+                    containerColor = Color(0xFFFCE0C6)
+                ),
+                modifier = Modifier
+                    .fillMaxWidth()
+                    .padding(40.dp, 20.dp)
+                    .border(BorderStroke(2.dp, Color.Black), shape = RoundedCornerShape(25.dp))
 
-                    },
-                    colors = ButtonDefaults.buttonColors(
-                        containerColor = Color(0xFFFCE0C6)
-                    ),
-                    modifier = Modifier
-                        .fillMaxWidth()
-                        .padding(40.dp, 20.dp)
-                        .border(BorderStroke(2.dp, Color.Black), shape = RoundedCornerShape(25.dp))
-
-                ) {
+            ) {
                     Text(
                         text = "Сохранить рассказ",
                         color = Color.Black
@@ -143,26 +158,3 @@ fun СreateArticle(
     }
 }
 
-    @Composable
-    fun CreateTextFolder(
-        titleState: MutableState<String>,
-        categoryState: MutableState<String>
-    ) {
-        TextField(
-            value = titleState.value,
-            onValueChange = { newEmail -> titleState.value = newEmail },
-            maxLines = 1,
-            placeholder = { Text("Название") },
-            modifier = Modifier
-                .padding(0.dp, 30.dp, 0.dp, 0.dp)
-
-        )
-        TextField(
-            value = categoryState.value,
-            onValueChange = { newPass -> categoryState.value = newPass },
-            maxLines = 1,
-            placeholder = { Text("Категория рассказа") },
-            modifier = Modifier
-                .padding(0.dp, 30.dp, 0.dp, 0.dp)
-        )
-    }

+ 47 - 0
app/src/main/java/com/example/collectionofshortstories/view/createArticle/СreateArticleViewModel.kt

@@ -1,20 +1,27 @@
 package com.example.collectionofshortstories.view.createArticle
 
+import android.util.Log
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.mutableStateOf
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
+import androidx.lifecycle.viewmodel.compose.viewModel
 import com.example.collectionofshortstories.domain.untils.Constants
 import com.example.collectionofshortstories.model.Article
 import com.example.collectionofshortstories.model.Categories
+import com.example.collectionofshortstories.navigation.Routes
 import dagger.hilt.android.lifecycle.HiltViewModel
+import io.github.jan.supabase.gotrue.auth
 import io.github.jan.supabase.postgrest.from
+import io.github.jan.supabase.postgrest.query.Count
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.StateFlow
 import kotlinx.coroutines.flow.asStateFlow
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import java.util.UUID
 import javax.inject.Inject
 
 @HiltViewModel
@@ -22,6 +29,9 @@ class СreateArticleViewModel @Inject constructor() : ViewModel() {
     private val _categories: MutableStateFlow<List<Categories>> = MutableStateFlow( listOf() )
     val categories: StateFlow<List<Categories>> = _categories.asStateFlow()
 
+    private var _navigationTo = MutableStateFlow<String?>(null)
+    val navigationTo = _navigationTo.asStateFlow()
+
     fun GetCategories() {
         viewModelScope.launch {
             withContext(Dispatchers.IO) {
@@ -32,4 +42,41 @@ class СreateArticleViewModel @Inject constructor() : ViewModel() {
         }
     }
 
+    fun InsertNewArticle(titleArticleNew: String, textArticleNew: String, categoryArticle: String) {
+        try {
+            viewModelScope.launch {
+                withContext(Dispatchers.IO) {
+                    val maxId = Constants.supabase.from("articles")
+                        .select {
+                            count(
+                                Count.EXACT
+                            )
+                        }.countOrNull()!!.toInt()
+
+                    val NewArticle = Article(
+                        id = maxId + 1,
+                        id_author = Constants.supabase.auth.currentUserOrNull()!!.id,
+                        title = titleArticleNew,
+                        id_categories = Constants.supabase.from("categories")
+                            .select {
+                                filter {
+                                    eq("title", categoryArticle)
+                                }
+                            }.decodeSingleOrNull<Categories>()!!.id,
+                        textArticles = textArticleNew,
+                    )
+
+                    Constants.supabase.from("articles").insert(NewArticle)
+                    _navigationTo.value = Routes.CATALOG
+                }
+            }
+        }
+        catch (e: Exception) {
+            Log.d("Insert", e.toString())
+            Log.d("Insert", e.message.toString())
+
+        }
+    }
+
+
 }

+ 47 - 0
app/src/main/java/com/example/collectionofshortstories/view/dateArticle/DateArticle.kt

@@ -0,0 +1,47 @@
+package com.example.collectionofshortstories.view.dateArticle
+
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material3.Button
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.ui.Modifier
+import androidx.hilt.navigation.compose.hiltViewModel
+import androidx.navigation.NavHostController
+import com.example.collectionofshortstories.navigation.Routes
+import kotlinx.datetime.Month
+
+@Composable
+fun DateArticle(navHostController: NavHostController, titleArticle: String, viewModel: DateArticleViewModel = hiltViewModel()) {
+
+    viewModel.GetTextAndCategories(titleArticle)
+
+    Column(
+        modifier = Modifier
+            .fillMaxSize(),
+        verticalArrangement = Arrangement.SpaceAround
+    ) {
+        Button(
+            onClick = {
+                navHostController.navigate(Routes.CATALOG)
+            }
+        ) {
+            Text(
+                text = "Назад"
+            )
+        }
+        Text(
+            text = titleArticle
+        )
+        Text(
+            text = viewModel.textArticle.value
+        )
+        Text(
+            text = viewModel.categoriesArticle.value
+        )
+
+    }
+}

+ 43 - 0
app/src/main/java/com/example/collectionofshortstories/view/dateArticle/DateArticleViewModel.kt

@@ -0,0 +1,43 @@
+package com.example.collectionofshortstories.view.dateArticle
+
+import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
+import androidx.lifecycle.viewmodel.compose.viewModel
+import com.example.collectionofshortstories.domain.untils.Constants
+import com.example.collectionofshortstories.model.Article
+import com.example.collectionofshortstories.model.Categories
+import dagger.hilt.android.lifecycle.HiltViewModel
+import io.github.jan.supabase.postgrest.from
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import javax.inject.Inject
+
+@HiltViewModel
+class DateArticleViewModel @Inject constructor(): ViewModel() {
+    var textArticle =  mutableStateOf("")
+    val categoriesArticle = mutableStateOf("")
+
+    fun GetTextAndCategories(titleArticle: String) {
+        viewModelScope.launch {
+            withContext(Dispatchers.IO) {
+                val text = Constants.supabase.from("articles").select {
+                    filter {
+                        eq("title", titleArticle)
+                    }
+                }.decodeSingleOrNull<Article>()
+                textArticle.value = text?.textArticles ?: ""
+
+                val category = Constants.supabase.from("categories").select {
+                    filter {
+                        eq("id", text!!.id_categories)
+                    }
+                }.decodeSingleOrNull<Categories>()
+                categoriesArticle.value = category?.title ?: ""
+            }
+        }
+    }
+}