Compare commits

...

2 Commits

12 changed files with 110 additions and 86 deletions

View File

@ -83,4 +83,8 @@ dependencies {
implementation("androidx.hilt:hilt-navigation-compose:1.0.0") implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01' implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'
implementation "androidx.datastore:datastore-preferences:1.0.0" 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

@ -24,6 +24,6 @@ interface OrderDao {
fun getByUserId(userId: Int): Flow<List<OrderWithProducts>> fun getByUserId(userId: Int): Flow<List<OrderWithProducts>>
@Query("select * from orders where orders.user_id =:userId and orders.order_status = 'Неоплачено'") @Query("select * from orders where orders.user_id =:userId and orders.order_status = 'Неоплачено'")
fun getUnpaidByUser(userId: Int) : Flow<OrderWithProducts> fun getUnpaidByUser(userId: Int) : Flow<OrderWithProducts>
@Query("select * from orders where orders.user_id =:userId and orders.order_status = 'Готовится'") @Query("select * from orders where orders.user_id =:userId and (orders.order_status = 'Готовится' or orders.order_status = 'Готово')")
fun getPaidByUser(userId: Int) : Flow<List<OrderWithProducts>> fun getPaidByUser(userId: Int) : Flow<List<OrderWithProducts>>
} }

View File

@ -1,5 +1,6 @@
package com.example.shawarma.data.interfaces.dao package com.example.shawarma.data.interfaces.dao
import androidx.paging.PagingSource
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Delete import androidx.room.Delete
import androidx.room.Insert import androidx.room.Insert
@ -30,4 +31,12 @@ interface ProductDao {
@Query("select * from products where products.id = :id") @Query("select * from products where products.id = :id")
fun getById(id: Int): Flow<ProductModel> 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

@ -7,5 +7,6 @@ data class OrderProductModelWithProduct(
@Embedded @Embedded
val orderProductModel: OrderProductModel, val orderProductModel: OrderProductModel,
@Relation(entity = ProductModel::class, parentColumn = "product_id", entityColumn = "id") @Relation(entity = ProductModel::class, parentColumn = "product_id", entityColumn = "id")
val product: ProductModel val product: ProductModel,
) )

View File

@ -1,5 +1,8 @@
package com.example.shawarma.data.repos 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.OrderProductDao
import com.example.shawarma.data.interfaces.dao.ProductDao import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.models.ProductModel import com.example.shawarma.data.models.ProductModel
@ -20,7 +23,7 @@ class ProductRepository @Inject constructor(
orderProductDao.deleteByProductId(product.id!!) orderProductDao.deleteByProductId(product.id!!)
return productDao.delete(product) return productDao.delete(product)
} }
fun getAll(): Flow<List<ProductModel>> { /*fun getAll(): Flow<List<ProductModel>> {
return productDao.getAll() return productDao.getAll()
} }
fun getDiscounts(): Flow<List<ProductModel>> { fun getDiscounts(): Flow<List<ProductModel>> {
@ -28,8 +31,32 @@ class ProductRepository @Inject constructor(
} }
fun getItems(): Flow<List<ProductModel>> { fun getItems(): Flow<List<ProductModel>> {
return productDao.getItems() return productDao.getItems()
} }*/
fun getById(id: Int): Flow<ProductModel> { fun getById(id: Int): Flow<ProductModel> {
return productDao.getById(id) 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

@ -199,7 +199,7 @@ fun PaidItem(order : OrderWithProducts) {
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = order.orderWithProducts[index].orderProductModel.totalPrice.toString() + " руб.", text = (order.orderWithProducts[index].orderProductModel.quantity * order.orderWithProducts[index].product.price).toString() + " руб.",
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp), modifier = Modifier.padding(top = 15.dp),
@ -250,7 +250,7 @@ fun CartItem(order : OrderWithProducts) {
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = order.orderWithProducts[index].orderProductModel.totalPrice.toString() + " руб.", text = (order.orderWithProducts[index].orderProductModel.quantity * order.orderWithProducts[index].product.price).toString() + " руб.",
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 18.sp, fontSize = 18.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold

View File

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

View File

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

View File

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

View File

@ -1,9 +1,8 @@
package com.example.shawarma.viewmodels package com.example.shawarma.viewmodels
import androidx.lifecycle.LiveData
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 com.example.shawarma.data.models.OrderModel import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderProductModel import com.example.shawarma.data.models.OrderProductModel
import com.example.shawarma.data.models.OrderStatus 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.OrderRepository
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
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.Date import java.util.Date
@ -23,26 +23,11 @@ class HomeViewModel @Inject constructor(
private val orderRepository: OrderRepository, private val orderRepository: OrderRepository,
private val orderProductRepository: OrderProductRepository private val orderProductRepository: OrderProductRepository
) : ViewModel() { ) : ViewModel() {
private val _products = MutableLiveData<List<ProductModel>>()
val products: LiveData<List<ProductModel>>
get() = _products
private val _discounts= MutableLiveData<List<ProductModel>>() val productListUiState: Flow<PagingData<ProductModel>> = productRepository.getAllProductsPaged()
val discounts: LiveData<List<ProductModel>>
get() = _discounts 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) { fun addProductToCart(productId: Int, userId: String) {
if (userId == "null") return if (userId == "null") return

View File

@ -4,9 +4,11 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData 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 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
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
@ -18,13 +20,7 @@ class ProductsViewModel @Inject constructor(
val products: LiveData<List<ProductModel>> val products: LiveData<List<ProductModel>>
get() = _products get() = _products
init { val productListUiState: Flow<PagingData<ProductModel>> = productRepository.getAllItemsPaged()
viewModelScope.launch {
productRepository.getItems().collect {
_products.postValue(it)
}
}
}
fun deleteProduct(product: ProductModel) { fun deleteProduct(product: ProductModel) {
viewModelScope.launch { viewModelScope.launch {

View File

@ -54,11 +54,11 @@ class UserViewModel @Inject constructor(
_registrationState.postValue(false) _registrationState.postValue(false)
return return
} }
if (login.length < 8 || login.length > 20) { if (login.length < 5 || login.length > 20) {
_registrationState.postValue(false) _registrationState.postValue(false)
return return
} }
if (password.length < 8 || password.length > 20) { if (password.length < 5 || password.length > 20) {
_registrationState.postValue(false) _registrationState.postValue(false)
return return
} }