Пагинация, стагнация, ампутация, цивилизация, синхронизация, фракция, грация, конкатанация, нация, рация, сублимация, аннотация, субстанция

This commit is contained in:
Данила Мочалов 2023-12-03 23:42:14 +04:00
parent af6a0ffe3e
commit dc290f19d3
8 changed files with 103 additions and 80 deletions

View File

@ -83,4 +83,8 @@ dependencies {
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation("androidx.room:room-paging:2.5.0")
implementation("androidx.paging:paging-compose:3.2.1")
}

View File

@ -1,5 +1,6 @@
package com.example.shawarma.data.interfaces.dao
import androidx.paging.PagingSource
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
@ -30,4 +31,12 @@ interface ProductDao {
@Query("select * from products where products.id = :id")
fun getById(id: Int): Flow<ProductModel>
@Query("select * from products where products.product_old_price is null")
fun getPaged(): PagingSource<Int, ProductModel>
@Query("select * from products where products.product_old_price is not null")
fun getPagedDiscounts(): PagingSource<Int, ProductModel>
@Query("select * from products")
fun getPagedItems(): PagingSource<Int, ProductModel>
}

View File

@ -1,5 +1,8 @@
package com.example.shawarma.data.repos
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.example.shawarma.data.interfaces.dao.OrderProductDao
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.models.ProductModel
@ -20,7 +23,7 @@ class ProductRepository @Inject constructor(
orderProductDao.deleteByProductId(product.id!!)
return productDao.delete(product)
}
fun getAll(): Flow<List<ProductModel>> {
/*fun getAll(): Flow<List<ProductModel>> {
return productDao.getAll()
}
fun getDiscounts(): Flow<List<ProductModel>> {
@ -28,8 +31,32 @@ class ProductRepository @Inject constructor(
}
fun getItems(): Flow<List<ProductModel>> {
return productDao.getItems()
}
}*/
fun getById(id: Int): Flow<ProductModel> {
return productDao.getById(id)
}
fun getAllProductsPaged(): Flow<PagingData<ProductModel>> = Pager(
config = PagingConfig(
pageSize = 6,
enablePlaceholders = false
),
pagingSourceFactory = productDao::getPaged
).flow
fun getAllDiscountsPaged(): Flow<PagingData<ProductModel>> = Pager(
config = PagingConfig(
pageSize = 6,
enablePlaceholders = false
),
pagingSourceFactory = productDao::getPagedDiscounts
).flow
fun getAllItemsPaged(): Flow<PagingData<ProductModel>> = Pager(
config = PagingConfig(
pageSize = 6,
enablePlaceholders = false
),
pagingSourceFactory = productDao::getPagedItems
).flow
}

View File

@ -20,7 +20,6 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@ -33,6 +32,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 androidx.paging.compose.collectAsLazyPagingItems
import com.example.shawarma.R
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.sharedpref.PreferencesManager
@ -60,7 +60,7 @@ fun DiscountScreen() {
fun DiscountList(){
val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
val products = homeViewModel.discounts.observeAsState().value
val productsListUiState = homeViewModel.discountListUiState.collectAsLazyPagingItems()
Box(
modifier = Modifier
@ -83,16 +83,15 @@ fun DiscountList(){
modifier = Modifier.padding(top = 80.dp)
)
{
if (products != null) {
items(products.size) { index ->
if (index % 2 == 0 && index == products.size - 1) {
items(productsListUiState.itemCount) { index ->
if (index % 2 == 0 && index == productsListUiState.itemCount - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(productsListUiState[index]!!)
}
} else {
if (index % 2 != 1) {
@ -102,19 +101,18 @@ fun DiscountList(){
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(products[index+1])
DiscountCard(productsListUiState[index]!!)
DiscountCard(productsListUiState[index+1]!!)
}
}
}
if (index == products.size - 1) {
if (index == productsListUiState.itemCount - 1) {
Spacer(modifier = Modifier.height(70.dp))
}
}
}
}
}
}
@Composable
fun DiscountCard(product : ProductModel){

View File

@ -20,7 +20,6 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@ -32,6 +31,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey
import com.example.shawarma.R
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.sharedpref.PreferencesManager
@ -58,7 +59,7 @@ fun HomeScreen() {
fun HomeList(){
val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
val products = homeViewModel.products.observeAsState().value
val productsListUiState = homeViewModel.productListUiState.collectAsLazyPagingItems()
Box(
@ -81,16 +82,18 @@ fun HomeList(){
modifier = Modifier.padding(top = 80.dp)
)
{
if (products != null) {
items(products.size) { index ->
if (index % 2 == 0 && index == products.size - 1) {
items(
count = productsListUiState.itemCount,
key = productsListUiState.itemKey()
) { index ->
if (index % 2 == 0 && index == productsListUiState.itemCount - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
ProductCard(products[index])
ProductCard(productsListUiState[index]!!)
}
} else {
if (index % 2 != 1) {
@ -100,19 +103,18 @@ fun HomeList(){
.fillMaxWidth()
.padding(top = 10.dp)
) {
ProductCard(products[index])
ProductCard(products[index + 1])
ProductCard(productsListUiState[index]!!)
ProductCard(productsListUiState[index + 1]!!)
}
}
}
if (index == products.size - 1) {
if (index == productsListUiState.itemCount - 1) {
Spacer(modifier = Modifier.height(70.dp))
}
}
}
}
}
}
@Composable
fun ProductCard(product: ProductModel){

View File

@ -20,7 +20,6 @@ import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@ -32,6 +31,8 @@ import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey
import com.example.shawarma.R
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.ui.theme.MarckFamily
@ -60,7 +61,7 @@ fun ProductsScreen(navHostController: NavHostController) {
fun ProductsList(navHostController: NavHostController){
val productsViewModel: ProductsViewModel = hiltViewModel<ProductsViewModel>()
val products = productsViewModel.products.observeAsState().value
val products = productsViewModel.productListUiState.collectAsLazyPagingItems()
Box(
modifier = Modifier
@ -103,16 +104,17 @@ fun ProductsList(navHostController: NavHostController){
modifier = Modifier.padding(top = 160.dp)
)
{
if (products != null) {
items(products.size) { index ->
ProductItem(products[index], navHostController)
items(
products.itemCount,
key = products.itemKey()
) { index ->
ProductItem(products[index]!!, navHostController)
Spacer(modifier = Modifier.height(20.dp))
if (index == products.size - 1) {
if (index == products.itemCount - 1) {
Spacer(modifier = Modifier.height(70.dp))
}
}
}
}
}
}

View File

@ -1,9 +1,8 @@
package com.example.shawarma.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderProductModel
import com.example.shawarma.data.models.OrderStatus
@ -12,6 +11,7 @@ import com.example.shawarma.data.repos.OrderProductRepository
import com.example.shawarma.data.repos.OrderRepository
import com.example.shawarma.data.repos.ProductRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import java.util.Date
@ -23,26 +23,11 @@ class HomeViewModel @Inject constructor(
private val orderRepository: OrderRepository,
private val orderProductRepository: OrderProductRepository
) : ViewModel() {
private val _products = MutableLiveData<List<ProductModel>>()
val products: LiveData<List<ProductModel>>
get() = _products
private val _discounts= MutableLiveData<List<ProductModel>>()
val discounts: LiveData<List<ProductModel>>
get() = _discounts
val productListUiState: Flow<PagingData<ProductModel>> = productRepository.getAllProductsPaged()
val discountListUiState: Flow<PagingData<ProductModel>> = productRepository.getAllDiscountsPaged()
init {
viewModelScope.launch {
productRepository.getAll().collect {
_products.postValue(it)
}
}
viewModelScope.launch {
productRepository.getDiscounts().collect {
_discounts.postValue(it)
}
}
}
fun addProductToCart(productId: Int, userId: String) {
if (userId == "null") return

View File

@ -4,9 +4,11 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.repos.ProductRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import javax.inject.Inject
@ -18,13 +20,7 @@ class ProductsViewModel @Inject constructor(
val products: LiveData<List<ProductModel>>
get() = _products
init {
viewModelScope.launch {
productRepository.getItems().collect {
_products.postValue(it)
}
}
}
val productListUiState: Flow<PagingData<ProductModel>> = productRepository.getAllItemsPaged()
fun deleteProduct(product: ProductModel) {
viewModelScope.launch {