@@ -59,36 +59,36 @@ class CartRepositoryImpl(
59
59
}
60
60
}
61
61
62
- override fun getAllDineInOrders (): Flow <List <CartItem >> {
62
+ override suspend fun getAllDineInOrders (): Flow <List <CartItem >> {
63
63
return channelFlow {
64
- try {
65
- val items = withContext(ioDispatcher) {
66
- realm.query<CartEntity >(
64
+ withContext(ioDispatcher) {
65
+ try {
66
+ val items = realm.query<CartEntity >(
67
67
" cartOrder.cartOrderStatus == $0 AND cartOrder.orderType == $1" ,
68
68
OrderStatus .PROCESSING .name,
69
69
OrderType .DineIn .name
70
70
).sort(" cartId" , Sort .DESCENDING ).asFlow()
71
- }
72
71
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
+ }
78
77
79
- else -> {
80
- send(mapCartRealmToCartItem(changes.list))
78
+ else -> {
79
+ send(mapCartRealmToCartItem(changes.list))
80
+ }
81
81
}
82
82
}
83
- }
84
83
85
- } catch (e: Exception ) {
86
- send(emptyList())
84
+ } catch (e: Exception ) {
85
+ send(emptyList())
86
+ }
87
87
}
88
88
}
89
89
}
90
90
91
- override fun getAllDineOutOrders (): Flow <List <CartItem >> {
91
+ override suspend fun getAllDineOutOrders (): Flow <List <CartItem >> {
92
92
return channelFlow {
93
93
try {
94
94
val items = withContext(ioDispatcher) {
@@ -217,44 +217,47 @@ class CartRepositoryImpl(
217
217
}
218
218
}
219
219
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
223
224
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()
227
229
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()
231
233
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
+ }
235
238
}
236
239
}
237
- }
238
240
239
- if (cartOrder.addOnItems.isNotEmpty()) {
240
- for (addOnItem in cartOrder.addOnItems) {
241
+ if (cartOrder.addOnItems.isNotEmpty()) {
242
+ for (addOnItem in cartOrder.addOnItems) {
241
243
242
- totalPrice + = addOnItem.itemPrice
244
+ totalPrice + = addOnItem.itemPrice
243
245
244
- if (! addOnItem.isApplicable) {
245
- discountPrice + = addOnItem.itemPrice
246
+ if (! addOnItem.isApplicable) {
247
+ discountPrice + = addOnItem.itemPrice
248
+ }
246
249
}
247
250
}
248
- }
249
251
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
+ }
253
256
}
254
257
}
255
- }
256
258
257
- return Pair (totalPrice, discountPrice)
259
+ Pair (totalPrice, discountPrice)
260
+ }
258
261
}
259
262
260
263
override suspend fun deletePastData (): Resource <Boolean > {
@@ -281,44 +284,44 @@ class CartRepositoryImpl(
281
284
return realm.query<CartOrderEntity >(" cartOrderId == $0" , cartOrderId).find().first()
282
285
}
283
286
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
+ }
314
321
} else {
315
322
CartItem ()
316
323
}
317
- } else {
318
- CartItem ()
319
324
}
320
325
}
321
-
322
- return data
323
326
}
324
327
}
0 commit comments