From dc290f19d34e66e0e137a6df3c8ee1d266f5fbeb Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Sun, 3 Dec 2023 23:42:14 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F,=20=D1=81=D1=82=D0=B0=D0=B3=D0=BD=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F,=20=D0=B0=D0=BC=D0=BF=D1=83=D1=82=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F,=20=D1=86=D0=B8=D0=B2=D0=B8=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F,=20=D1=81=D0=B8=D0=BD=D1=85=D1=80?= =?UTF-8?q?=D0=BE=D0=BD=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F,=20=D1=84?= =?UTF-8?q?=D1=80=D0=B0=D0=BA=D1=86=D0=B8=D1=8F,=20=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F,=20=D0=BA=D0=BE=D0=BD=D0=BA=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=D1=86=D0=B8=D1=8F,=20=D0=BD=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F,=20=D1=80=D0=B0=D1=86=D0=B8=D1=8F,=20=D1=81=D1=83?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=BC=D0=B0=D1=86=D0=B8=D1=8F,=20=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D0=BE=D1=82=D0=B0=D1=86=D0=B8=D1=8F,=20=D1=81?= =?UTF-8?q?=D1=83=D0=B1=D1=81=D1=82=D0=B0=D0=BD=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 ++ .../data/interfaces/dao/ProductDao.kt | 9 ++++ .../shawarma/data/repos/ProductRepository.kt | 31 ++++++++++++- .../screens/discount/DiscountScreen.kt | 40 ++++++++--------- .../shawarma/screens/home/HomeScreen.kt | 44 ++++++++++--------- .../screens/products/ProductsScreen.kt | 20 +++++---- .../shawarma/viewmodels/HomeViewModel.kt | 25 +++-------- .../shawarma/viewmodels/ProductsViewModel.kt | 10 ++--- 8 files changed, 103 insertions(+), 80 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 031a540..8dcff33 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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") } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/interfaces/dao/ProductDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/ProductDao.kt index f539ef2..6908d82 100644 --- a/app/src/main/java/com/example/shawarma/data/interfaces/dao/ProductDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/ProductDao.kt @@ -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 + @Query("select * from products where products.product_old_price is null") + fun getPaged(): PagingSource + + @Query("select * from products where products.product_old_price is not null") + fun getPagedDiscounts(): PagingSource + + @Query("select * from products") + fun getPagedItems(): PagingSource } \ No newline at end of file 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 0913ee8..2db5efe 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 @@ -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> { + /*fun getAll(): Flow> { return productDao.getAll() } fun getDiscounts(): Flow> { @@ -28,8 +31,32 @@ class ProductRepository @Inject constructor( } fun getItems(): Flow> { return productDao.getItems() - } + }*/ fun getById(id: Int): Flow { return productDao.getById(id) } + fun getAllProductsPaged(): Flow> = Pager( + config = PagingConfig( + pageSize = 6, + enablePlaceholders = false + ), + pagingSourceFactory = productDao::getPaged + ).flow + + fun getAllDiscountsPaged(): Flow> = Pager( + config = PagingConfig( + pageSize = 6, + enablePlaceholders = false + ), + pagingSourceFactory = productDao::getPagedDiscounts + ).flow + + fun getAllItemsPaged(): Flow> = Pager( + config = PagingConfig( + pageSize = 6, + enablePlaceholders = false + ), + pagingSourceFactory = productDao::getPagedItems + ).flow + } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt b/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt index 3aa69e0..8c20d1a 100644 --- a/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt @@ -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() - val products = homeViewModel.discounts.observeAsState().value + val productsListUiState = homeViewModel.discountListUiState.collectAsLazyPagingItems() Box( modifier = Modifier @@ -83,33 +83,31 @@ 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(productsListUiState[index]!!) + } + } else { + if (index % 2 != 1) { Row( horizontalArrangement = Arrangement.SpaceAround, modifier = Modifier .fillMaxWidth() .padding(top = 10.dp) ) { - DiscountCard(products[index]) - } - } else { - if (index % 2 != 1) { - Row( - horizontalArrangement = Arrangement.SpaceAround, - modifier = Modifier - .fillMaxWidth() - .padding(top = 10.dp) - ) { - DiscountCard(products[index]) - DiscountCard(products[index+1]) - } + DiscountCard(productsListUiState[index]!!) + DiscountCard(productsListUiState[index+1]!!) } } - if (index == products.size - 1) { - Spacer(modifier = Modifier.height(70.dp)) - } + } + if (index == productsListUiState.itemCount - 1) { + Spacer(modifier = Modifier.height(70.dp)) } } } diff --git a/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt b/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt index 5c11939..a4d9641 100644 --- a/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt @@ -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() - val products = homeViewModel.products.observeAsState().value + val productsListUiState = homeViewModel.productListUiState.collectAsLazyPagingItems() Box( @@ -81,33 +82,34 @@ 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(productsListUiState[index]!!) + } + } else { + if (index % 2 != 1) { Row( horizontalArrangement = Arrangement.SpaceAround, modifier = Modifier .fillMaxWidth() .padding(top = 10.dp) ) { - ProductCard(products[index]) - } - } else { - if (index % 2 != 1) { - Row( - horizontalArrangement = Arrangement.SpaceAround, - modifier = Modifier - .fillMaxWidth() - .padding(top = 10.dp) - ) { - ProductCard(products[index]) - ProductCard(products[index + 1]) - } + ProductCard(productsListUiState[index]!!) + ProductCard(productsListUiState[index + 1]!!) } } - if (index == products.size - 1) { - Spacer(modifier = Modifier.height(70.dp)) - } + } + if (index == productsListUiState.itemCount - 1) { + Spacer(modifier = Modifier.height(70.dp)) } } } diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt index 1ba2a02..f751239 100644 --- a/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt @@ -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() - val products = productsViewModel.products.observeAsState().value + val products = productsViewModel.productListUiState.collectAsLazyPagingItems() Box( modifier = Modifier @@ -103,13 +104,14 @@ fun ProductsList(navHostController: NavHostController){ modifier = Modifier.padding(top = 160.dp) ) { - if (products != null) { - items(products.size) { index -> - ProductItem(products[index], navHostController) - Spacer(modifier = Modifier.height(20.dp)) - if (index == products.size - 1) { - Spacer(modifier = Modifier.height(70.dp)) - } + items( + products.itemCount, + key = products.itemKey() + ) { index -> + ProductItem(products[index]!!, navHostController) + Spacer(modifier = Modifier.height(20.dp)) + if (index == products.itemCount - 1) { + Spacer(modifier = Modifier.height(70.dp)) } } } diff --git a/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt index 62a428a..0eeff4a 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt @@ -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>() - val products: LiveData> - get() = _products - private val _discounts= MutableLiveData>() - val discounts: LiveData> - get() = _discounts + val productListUiState: Flow> = productRepository.getAllProductsPaged() + + val discountListUiState: Flow> = 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 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 61023ff..38c648b 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt @@ -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> get() = _products - init { - viewModelScope.launch { - productRepository.getItems().collect { - _products.postValue(it) - } - } - } + val productListUiState: Flow> = productRepository.getAllItemsPaged() fun deleteProduct(product: ProductModel) { viewModelScope.launch {