бета аналитика ver 0.2

This commit is contained in:
Данила Мочалов 2023-12-26 23:29:41 +04:00
parent da80721cd1
commit 6f5f1c40d3
5 changed files with 40 additions and 13 deletions

View File

@ -10,7 +10,9 @@ data class ProductModelRemote(
val price: Int = 0, val price: Int = 0,
val old_price: Int? = null, val old_price: Int? = null,
val order_products: List<OrderProductRemote> = listOf() val order_products: List<OrderProductRemote> = listOf(),
val purchase_count: Int? = null
) )
@Serializable @Serializable

View File

@ -6,7 +6,7 @@ import androidx.paging.PagingConfig
import androidx.paging.PagingData import androidx.paging.PagingData
import com.example.shawarma.data.api.MyServerService import com.example.shawarma.data.api.MyServerService
import com.example.shawarma.data.api.mediators.ProductRemoteMediator 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.api.repos.RestProductRepository
import com.example.shawarma.data.db.AppDatabase import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.interfaces.dao.ProductDao 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) remoteMediator = ProductRemoteMediator(database = database, serverService = MyServerService.getInstance(), query = "items", token = token)
).flow ).flow
suspend fun getTopProducts(period: String, token: String) : List<ProductModel> { suspend fun getTopProducts(period: String, token: String) : List<ProductModelRemote> {
return restRepository.getTopProducts(period, token).map { it.toProductModel() } return restRepository.getTopProducts(period, token)
} }
} }

View File

@ -31,7 +31,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel 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.data.sharedpref.PreferencesManager
import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow import com.example.shawarma.ui.theme.MyLightYellow
@ -74,10 +74,11 @@ fun TopProductsWidget(productsViewModel: ProductsViewModel) {
fontSize = 40.sp, fontSize = 40.sp,
modifier = Modifier.padding(top = 15.dp) modifier = Modifier.padding(top = 15.dp)
) )
Text(text = "Топ-10 продуктов за:", modifier = Modifier.padding(top = 60.dp))
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxHeight() .fillMaxHeight()
.padding(top = 60.dp) .padding(top = 80.dp)
){ ){
Button( Button(
modifier = Modifier modifier = Modifier
@ -127,13 +128,13 @@ fun TopProductsWidget(productsViewModel: ProductsViewModel) {
} }
if (!topProductsList.isNullOrEmpty()) { if (!topProductsList.isNullOrEmpty()) {
LazyColumn( LazyColumn(
modifier = Modifier.padding(top = 120.dp, bottom = 60.dp), modifier = Modifier.padding(top = 140.dp, bottom = 60.dp),
){ ){
items( items(
topProductsList.size topProductsList.size
) { ) {
TopProductItem(topProductsList[it]) TopProductItem(topProductsList[it], it)
Spacer(modifier = Modifier.size(20.dp)) Spacer(modifier = Modifier.size(20.dp))
} }
} }
@ -142,7 +143,7 @@ fun TopProductsWidget(productsViewModel: ProductsViewModel) {
} }
@Composable @Composable
fun TopProductItem(product: ProductModel){ fun TopProductItem(product: ProductModelRemote, index: Int){
Card( Card(
border = BorderStroke(width = 2.dp, color = MyOrange), border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp), shape = RoundedCornerShape(size = 20.dp),
@ -156,17 +157,17 @@ fun TopProductItem(product: ProductModel){
horizontalArrangement = Arrangement.SpaceAround horizontalArrangement = Arrangement.SpaceAround
) { ) {
Column( Column(
modifier = Modifier.fillMaxWidth(1f) modifier = Modifier.fillMaxWidth(0.5f)
) { ) {
Text( Text(
text = product.title, text = (index + 1).toString() + ". " + product.title,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis overflow = TextOverflow.Ellipsis
) )
if (product.oldPrice != null) { if (product.old_price != null) {
Text( Text(
text = product.price.toString() + " руб.", text = product.price.toString() + " руб.",
fontFamily = NunitoFamily, 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
)
}
} }
} }
} }

View File

@ -23,6 +23,9 @@ class ProductViewModel @Inject constructor(
get() = _product get() = _product
fun addProduct(name: String, price: Int, oldPrice: Int? = null, token: String) { fun addProduct(name: String, price: Int, oldPrice: Int? = null, token: String) {
if (name.isNullOrEmpty() || price <= 0) {
return
}
viewModelScope.launch { viewModelScope.launch {
productRepository.insert(ProductModel(null, name, price, oldPrice), token) productRepository.insert(ProductModel(null, name, price, oldPrice), token)
_addingProductState.postValue(true) _addingProductState.postValue(true)

View File

@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData import androidx.paging.PagingData
import com.example.shawarma.data.api.models.ProductModelRemote
import com.example.shawarma.data.models.ProductModel import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.repos.ProductRepository import com.example.shawarma.data.repos.ProductRepository
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
@ -26,7 +27,7 @@ class ProductsViewModel @Inject constructor(
} }
} }
val topProductList = MutableLiveData<List<ProductModel>?>(null) val topProductList = MutableLiveData<List<ProductModelRemote>?>(null)
fun getTopProductsList(token: String, period: String) { fun getTopProductsList(token: String, period: String) {
viewModelScope.launch { viewModelScope.launch {