Skip to content

Commit d2807c1

Browse files
committed
Enhancement - Cart
- Close #203
1 parent 5a71f0d commit d2807c1

File tree

7 files changed

+238
-112
lines changed

7 files changed

+238
-112
lines changed

core/data/src/main/java/com/niyaj/data/data/repository/CartRepositoryImpl.kt

+77-74
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,36 @@ class CartRepositoryImpl(
5959
}
6060
}
6161

62-
override fun getAllDineInOrders(): Flow<List<CartItem>> {
62+
override suspend fun getAllDineInOrders(): Flow<List<CartItem>> {
6363
return channelFlow {
64-
try {
65-
val items = withContext(ioDispatcher) {
66-
realm.query<CartEntity>(
64+
withContext(ioDispatcher) {
65+
try {
66+
val items = realm.query<CartEntity>(
6767
"cartOrder.cartOrderStatus == $0 AND cartOrder.orderType == $1",
6868
OrderStatus.PROCESSING.name,
6969
OrderType.DineIn.name
7070
).sort("cartId", Sort.DESCENDING).asFlow()
71-
}
7271

73-
items.collectLatest { changes: ResultsChange<CartEntity> ->
74-
when (changes) {
75-
is UpdatedResults -> {
76-
send(mapCartRealmToCartItem(changes.list))
77-
}
72+
items.collectLatest { changes: ResultsChange<CartEntity> ->
73+
when (changes) {
74+
is UpdatedResults -> {
75+
send(mapCartRealmToCartItem(changes.list))
76+
}
7877

79-
else -> {
80-
send(mapCartRealmToCartItem(changes.list))
78+
else -> {
79+
send(mapCartRealmToCartItem(changes.list))
80+
}
8181
}
8282
}
83-
}
8483

85-
} catch (e: Exception) {
86-
send(emptyList())
84+
} catch (e: Exception) {
85+
send(emptyList())
86+
}
8787
}
8888
}
8989
}
9090

91-
override fun getAllDineOutOrders(): Flow<List<CartItem>> {
91+
override suspend fun getAllDineOutOrders(): Flow<List<CartItem>> {
9292
return channelFlow {
9393
try {
9494
val items = withContext(ioDispatcher) {
@@ -217,44 +217,47 @@ class CartRepositoryImpl(
217217
}
218218
}
219219

220-
override fun countTotalPrice(cartOrderId: String): Pair<Int, Int> {
221-
var totalPrice = 0
222-
var discountPrice = 0
220+
override suspend fun countTotalPrice(cartOrderId: String): Pair<Int, Int> {
221+
return withContext(ioDispatcher) {
222+
var totalPrice = 0
223+
var discountPrice = 0
223224

224-
val cartOrder =
225-
realm.query<CartOrderEntity>("cartOrderId == $0", cartOrderId).first().find()
226-
val cartOrders = realm.query<CartEntity>("cartOrder.cartOrderId == $0", cartOrderId).find()
225+
val cartOrder =
226+
realm.query<CartOrderEntity>("cartOrderId == $0", cartOrderId).first().find()
227+
val cartOrders =
228+
realm.query<CartEntity>("cartOrder.cartOrderId == $0", cartOrderId).find()
227229

228-
if (cartOrder != null && cartOrders.isNotEmpty()) {
229-
if (cartOrder.doesChargesIncluded) {
230-
val charges = realm.query<ChargesEntity>().find()
230+
if (cartOrder != null && cartOrders.isNotEmpty()) {
231+
if (cartOrder.doesChargesIncluded) {
232+
val charges = realm.query<ChargesEntity>().find()
231233

232-
for (charge in charges) {
233-
if (charge.isApplicable && cartOrder.orderType != OrderType.DineIn.name) {
234-
totalPrice += charge.chargesPrice
234+
for (charge in charges) {
235+
if (charge.isApplicable && cartOrder.orderType != OrderType.DineIn.name) {
236+
totalPrice += charge.chargesPrice
237+
}
235238
}
236239
}
237-
}
238240

239-
if (cartOrder.addOnItems.isNotEmpty()) {
240-
for (addOnItem in cartOrder.addOnItems) {
241+
if (cartOrder.addOnItems.isNotEmpty()) {
242+
for (addOnItem in cartOrder.addOnItems) {
241243

242-
totalPrice += addOnItem.itemPrice
244+
totalPrice += addOnItem.itemPrice
243245

244-
if (!addOnItem.isApplicable) {
245-
discountPrice += addOnItem.itemPrice
246+
if (!addOnItem.isApplicable) {
247+
discountPrice += addOnItem.itemPrice
248+
}
246249
}
247250
}
248-
}
249251

250-
for (cartOrder1 in cartOrders) {
251-
if (cartOrder1.product != null) {
252-
totalPrice += cartOrder1.quantity.times(cartOrder1.product?.productPrice!!)
252+
for (cartOrder1 in cartOrders) {
253+
if (cartOrder1.product != null) {
254+
totalPrice += cartOrder1.quantity.times(cartOrder1.product?.productPrice!!)
255+
}
253256
}
254257
}
255-
}
256258

257-
return Pair(totalPrice, discountPrice)
259+
Pair(totalPrice, discountPrice)
260+
}
258261
}
259262

260263
override suspend fun deletePastData(): Resource<Boolean> {
@@ -281,44 +284,44 @@ class CartRepositoryImpl(
281284
return realm.query<CartOrderEntity>("cartOrderId == $0", cartOrderId).find().first()
282285
}
283286

284-
private fun mapCartRealmToCartItem(carts: List<CartEntity>): List<CartItem> {
285-
val groupedByOrder = carts.groupBy { it.cartOrder?.cartOrderId }
286-
287-
val data = groupedByOrder.map { groupedCartProducts ->
288-
if (groupedCartProducts.key != null && groupedCartProducts.value.isNotEmpty()) {
289-
val cartOrder = getCartOrderById(groupedCartProducts.key!!)
290-
291-
if (cartOrder.cartOrderStatus != OrderStatus.PLACED.name) {
292-
CartItem(
293-
cartOrderId = cartOrder.cartOrderId,
294-
orderId = cartOrder.orderId,
295-
orderType = OrderType.valueOf(cartOrder.orderType),
296-
cartProducts = groupedCartProducts.value.map { cart ->
297-
if (cart.product != null) {
298-
CartProductItem(
299-
productId = cart.product!!.productId,
300-
productName = cart.product!!.productName,
301-
productPrice = cart.product!!.productPrice,
302-
productQuantity = cart.quantity
303-
)
304-
} else {
305-
CartProductItem()
306-
}
307-
},
308-
addOnItems = cartOrder.addOnItems.map { it.addOnItemId },
309-
customerPhone = cartOrder.customer?.customerPhone,
310-
customerAddress = cartOrder.address?.shortName,
311-
updatedAt = cartOrder.createdAt,
312-
orderPrice = countTotalPrice(cartOrder.cartOrderId)
313-
)
287+
private suspend fun mapCartRealmToCartItem(carts: List<CartEntity>): List<CartItem> {
288+
return withContext(ioDispatcher) {
289+
val groupedByOrder = carts.groupBy { it.cartOrder?.cartOrderId }
290+
291+
groupedByOrder.map { groupedCartProducts ->
292+
if (groupedCartProducts.key != null && groupedCartProducts.value.isNotEmpty()) {
293+
val cartOrder = getCartOrderById(groupedCartProducts.key!!)
294+
295+
if (cartOrder.cartOrderStatus != OrderStatus.PLACED.name) {
296+
CartItem(
297+
cartOrderId = cartOrder.cartOrderId,
298+
orderId = cartOrder.orderId,
299+
orderType = OrderType.valueOf(cartOrder.orderType),
300+
cartProducts = groupedCartProducts.value.map { cart ->
301+
if (cart.product != null) {
302+
CartProductItem(
303+
productId = cart.product!!.productId,
304+
productName = cart.product!!.productName,
305+
productPrice = cart.product!!.productPrice,
306+
productQuantity = cart.quantity
307+
)
308+
} else {
309+
CartProductItem()
310+
}
311+
},
312+
addOnItems = cartOrder.addOnItems.map { it.addOnItemId },
313+
customerPhone = cartOrder.customer?.customerPhone,
314+
customerAddress = cartOrder.address?.shortName,
315+
updatedAt = cartOrder.createdAt,
316+
orderPrice = countTotalPrice(cartOrder.cartOrderId)
317+
)
318+
} else {
319+
CartItem()
320+
}
314321
} else {
315322
CartItem()
316323
}
317-
} else {
318-
CartItem()
319324
}
320325
}
321-
322-
return data
323326
}
324327
}

core/data/src/main/java/com/niyaj/data/data/repository/OrderRepositoryImpl.kt

+7-13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import kotlinx.coroutines.CoroutineDispatcher
3434
import kotlinx.coroutines.flow.Flow
3535
import kotlinx.coroutines.flow.channelFlow
3636
import kotlinx.coroutines.flow.collectLatest
37+
import kotlinx.coroutines.flow.mapLatest
3738
import kotlinx.coroutines.withContext
3839
import timber.log.Timber
3940

@@ -45,23 +46,17 @@ class OrderRepositoryImpl(
4546
val realm = Realm.open(config)
4647

4748
override suspend fun getAllCharges(): Flow<List<Charges>> {
48-
return channelFlow {
49-
withContext(ioDispatcher) {
50-
try {
51-
val charges = realm.query<ChargesEntity>()
52-
.sort("chargesId", Sort.DESCENDING)
53-
.find()
54-
.asFlow()
55-
49+
return withContext(ioDispatcher) {
50+
realm.query<ChargesEntity>()
51+
.sort("chargesId", Sort.DESCENDING)
52+
.find()
53+
.asFlow()
54+
.mapLatest { charges ->
5655
charges.collectWithSearch(
5756
transform = { it.toExternalModel() },
5857
searchFilter = { it },
59-
send = { send(it) }
6058
)
61-
} catch (e: Exception) {
62-
send(emptyList())
6359
}
64-
}
6560
}
6661
}
6762

@@ -339,7 +334,6 @@ class OrderRepositoryImpl(
339334
}
340335
}
341336

342-
343337
private suspend fun mapCartOrderToDineOutOrders(data: List<CartOrderEntity>): List<DineOutOrder> {
344338
val result = withContext(ioDispatcher) {
345339
data.map { cartOrder ->

core/data/src/main/java/com/niyaj/data/repository/CartRepository.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ interface CartRepository {
99

1010
suspend fun getAllAddOnItems(searchText: String): Flow<List<AddOnItem>>
1111

12-
fun getAllDineInOrders(): Flow<List<CartItem>>
12+
suspend fun getAllDineInOrders(): Flow<List<CartItem>>
1313

14-
fun getAllDineOutOrders(): Flow<List<CartItem>>
14+
suspend fun getAllDineOutOrders(): Flow<List<CartItem>>
1515

1616
suspend fun addProductToCart(cartOrderId: String, productId: String): Resource<Boolean>
1717

1818
suspend fun removeProductFromCart(cartOrderId: String, productId: String): Resource<Boolean>
1919

20-
fun countTotalPrice(cartOrderId: String): Pair<Int, Int>
20+
suspend fun countTotalPrice(cartOrderId: String): Pair<Int, Int>
2121

2222
/**
2323
* Delete old data before current date

0 commit comments

Comments
 (0)