diff --git a/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt b/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt index fdc0ee0..250dea7 100644 --- a/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt +++ b/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt @@ -10,7 +10,9 @@ data class ProductModelRemote( val price: Int = 0, val old_price: Int? = null, - val order_products: List = listOf() + val order_products: List = listOf(), + + val purchase_count: Int? = null ) @Serializable diff --git a/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt index f8713cd..a768828 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt @@ -6,7 +6,7 @@ import androidx.paging.PagingConfig import androidx.paging.PagingData import com.example.shawarma.data.api.MyServerService import com.example.shawarma.data.api.mediators.ProductRemoteMediator -import com.example.shawarma.data.api.models.toProductModel +import com.example.shawarma.data.api.models.ProductModelRemote import com.example.shawarma.data.api.repos.RestProductRepository import com.example.shawarma.data.db.AppDatabase import com.example.shawarma.data.interfaces.dao.ProductDao @@ -62,8 +62,8 @@ class ProductRepository @Inject constructor( remoteMediator = ProductRemoteMediator(database = database, serverService = MyServerService.getInstance(), query = "items", token = token) ).flow - suspend fun getTopProducts(period: String, token: String) : List { - return restRepository.getTopProducts(period, token).map { it.toProductModel() } + suspend fun getTopProducts(period: String, token: String) : List { + return restRepository.getTopProducts(period, token) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductsAnalysis.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductsAnalysis.kt index 8c90308..6dd13ca 100644 --- a/app/src/main/java/com/example/shawarma/screens/products/ProductsAnalysis.kt +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductsAnalysis.kt @@ -31,7 +31,7 @@ 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.data.models.ProductModel +import com.example.shawarma.data.api.models.ProductModelRemote import com.example.shawarma.data.sharedpref.PreferencesManager import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MyLightYellow @@ -74,10 +74,11 @@ fun TopProductsWidget(productsViewModel: ProductsViewModel) { fontSize = 40.sp, modifier = Modifier.padding(top = 15.dp) ) + Text(text = "Топ-10 продуктов за:", modifier = Modifier.padding(top = 60.dp)) Row( modifier = Modifier .fillMaxHeight() - .padding(top = 60.dp) + .padding(top = 80.dp) ){ Button( modifier = Modifier @@ -127,13 +128,13 @@ fun TopProductsWidget(productsViewModel: ProductsViewModel) { } if (!topProductsList.isNullOrEmpty()) { LazyColumn( - modifier = Modifier.padding(top = 120.dp, bottom = 60.dp), + modifier = Modifier.padding(top = 140.dp, bottom = 60.dp), ){ items( topProductsList.size ) { - TopProductItem(topProductsList[it]) + TopProductItem(topProductsList[it], it) Spacer(modifier = Modifier.size(20.dp)) } } @@ -142,7 +143,7 @@ fun TopProductsWidget(productsViewModel: ProductsViewModel) { } @Composable -fun TopProductItem(product: ProductModel){ +fun TopProductItem(product: ProductModelRemote, index: Int){ Card( border = BorderStroke(width = 2.dp, color = MyOrange), shape = RoundedCornerShape(size = 20.dp), @@ -156,17 +157,17 @@ fun TopProductItem(product: ProductModel){ horizontalArrangement = Arrangement.SpaceAround ) { Column( - modifier = Modifier.fillMaxWidth(1f) + modifier = Modifier.fillMaxWidth(0.5f) ) { Text( - text = product.title, + text = (index + 1).toString() + ". " + product.title, fontFamily = NunitoFamily, fontSize = 20.sp, fontWeight = FontWeight.Bold, maxLines = 1, overflow = TextOverflow.Ellipsis ) - if (product.oldPrice != null) { + if (product.old_price != null) { Text( text = product.price.toString() + " руб.", fontFamily = NunitoFamily, @@ -185,6 +186,26 @@ fun TopProductItem(product: ProductModel){ } } + Column( + modifier = Modifier.fillMaxWidth(0.5f) + ) { + Text( + text = "Покупок:", + fontFamily = NunitoFamily, + fontSize = 16.sp, + fontWeight = FontWeight.Bold, + maxLines = 1, + overflow = TextOverflow.Ellipsis + ) + Text( + text = product.purchase_count.toString(), + fontFamily = NunitoFamily, + fontSize = 16.sp, + fontWeight = FontWeight.Bold, + maxLines = 1, + overflow = TextOverflow.Ellipsis + ) + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt index 2fd74d0..ee4f709 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt @@ -23,6 +23,9 @@ class ProductViewModel @Inject constructor( get() = _product fun addProduct(name: String, price: Int, oldPrice: Int? = null, token: String) { + if (name.isNullOrEmpty() || price <= 0) { + return + } viewModelScope.launch { productRepository.insert(ProductModel(null, name, price, oldPrice), token) _addingProductState.postValue(true) diff --git a/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt index c316521..bf57a91 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.paging.PagingData +import com.example.shawarma.data.api.models.ProductModelRemote import com.example.shawarma.data.models.ProductModel import com.example.shawarma.data.repos.ProductRepository import dagger.hilt.android.lifecycle.HiltViewModel @@ -26,7 +27,7 @@ class ProductsViewModel @Inject constructor( } } - val topProductList = MutableLiveData?>(null) + val topProductList = MutableLiveData?>(null) fun getTopProductsList(token: String, period: String) { viewModelScope.launch {