Files

52 lines
1.9 KiB
Kotlin
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.example.myapplication.components
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.example.myapplication.components.templates.CatalogItems
import com.example.myapplication.components.templates.CategoryItem
import com.example.myapplication.database.AppDb
import com.example.myapplication.database.entities.Category
import com.example.myapplication.database.entities.Product
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun Сategory(navController: NavController) {
Column(
modifier = Modifier.fillMaxWidth().padding(start = 10.dp, end = 10.dp, top = 20.dp,),
horizontalAlignment = Alignment.End
) {
val context = LocalContext.current
val categories = remember { mutableStateListOf<Category>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDb.getInstance(context).categoryDao().getAll().collect { data ->
categories.clear()
data.forEach {
categories.add(it)
}
}
}
}
categories.forEach{
CategoryItem(it.name, { navController.navigate("main/" + it.id)})
}
}
}