Прикрутил вьюмодель главного экрана к экрану со скидками

This commit is contained in:
Данила Мочалов 2023-11-26 21:02:30 +04:00
parent 377da2968c
commit 6a063c1f59
2 changed files with 38 additions and 34 deletions

View File

@ -20,9 +20,7 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@ -34,18 +32,18 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.sharedpref.PreferencesManager
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.MyPriceBackground
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.viewmodels.HomeViewModel
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun DiscountScreen() {
@ -60,17 +58,9 @@ fun DiscountScreen() {
@Composable
fun DiscountList(){
val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
val context = LocalContext.current
val products = remember { mutableStateListOf<ProductModel>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getDiscounts().collect { data ->
products.clear()
products.addAll(data)
}
}
}
val products = homeViewModel.discounts.observeAsState().value
Box(
modifier = Modifier
@ -93,19 +83,9 @@ fun DiscountList(){
modifier = Modifier.padding(top = 80.dp)
)
{
items(products.size) { index ->
if (index % 2 == 0 && index == products.size - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
}
}
else {
if (index % 2 != 1) {
if (products != null) {
items(products.size) { index ->
if (index % 2 == 0 && index == products.size - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
@ -113,12 +93,23 @@ fun DiscountList(){
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(products[index+1])
}
} else {
if (index % 2 != 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(products[index+1])
}
}
}
}
if (index == products.size - 1) {
Spacer(modifier = Modifier.height(70.dp))
if (index == products.size - 1) {
Spacer(modifier = Modifier.height(70.dp))
}
}
}
}
@ -127,6 +118,8 @@ fun DiscountList(){
@Composable
fun DiscountCard(product : ProductModel){
val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
val preferencesManager = PreferencesManager(LocalContext.current)
Card(
shape = RoundedCornerShape(20.dp),
backgroundColor = Color.White,
@ -181,7 +174,9 @@ fun DiscountCard(product : ProductModel){
}
Button(
onClick = { /*TODO*/ },
onClick = {
product.id?.let { homeViewModel.addProductToCart(it, preferencesManager.getData("user_id", "null")) }
},
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow,
contentColor = Color.Black

View File

@ -27,12 +27,21 @@ class HomeViewModel @Inject constructor(
val products: LiveData<List<ProductModel>>
get() = _products
private val _discounts= MutableLiveData<List<ProductModel>>()
val discounts: LiveData<List<ProductModel>>
get() = _discounts
init {
viewModelScope.launch {
productRepository.getAll().collect {
_products.postValue(it)
}
}
viewModelScope.launch {
productRepository.getDiscounts().collect {
_discounts.postValue(it)
}
}
}
fun addProductToCart(productId: Int, userId: String) {