Add bonuses field to cart and products

This commit is contained in:
Grigorii 2023-01-09 15:02:00 +04:00
parent 8a0ed4ca06
commit 33a745e5e0
4 changed files with 17 additions and 18 deletions

View File

@ -6,20 +6,26 @@ abstract class CartModel<TProductModel : ProductModel> {
open val promocodeList: List<PromocodeModel> = emptyList()
open val availableBonuses: Int = 0
open val usedBonuses: Int = 0
val availableProducts: List<TProductModel>
get() = products.filter { it.isAvailable && !it.isDeleted }
val totalPrice: Int
get() = availableProducts.sumOf { it.countInCart * it.price }
val priceWithPromocode: Int
get() = promocodeList
.sortedByDescending { it.discount is PromocodeDiscount.ByPercent }
.fold(initial = totalPrice) { price, promo -> promo.discount.applyTo(price) }
val totalBonuses: Int
get() = availableProducts.sumOf { it.countInCart * (it.bonuses ?: 0) }
fun getPriceWithPromocode(): Int = promocodeList
.sortedByDescending { it.discount is PromocodeDiscount.ByPercent }
.fold(initial = totalPrice) { price, promo -> promo.discount.applyTo(price) }
abstract fun <TCart> copyWith(
products: List<TProductModel> = this.products,
promocodeList: List<PromocodeModel> = this.promocodeList
promocodeList: List<PromocodeModel> = this.promocodeList,
usedBonuses: Int = this.usedBonuses
): TCart
@Suppress("UNCHECKED_CAST")

View File

@ -7,6 +7,7 @@ abstract class ProductModel {
abstract val isAvailable: Boolean
abstract val isDeleted: Boolean
open val bonuses: Int? = null
open val variants: List<ProductModel> = emptyList()
abstract fun <TProduct> copyWith(

View File

@ -59,6 +59,11 @@ class LocalCartRepository<TCart : CartModel<TProduct>, TProduct : ProductModel>(
updatePromocodeList { removeAt(indexOfFirst { it.code == code }) }
}
fun useBonuses(bonuses: Int) {
require(currentCart.value.availableBonuses >= bonuses) { "Can't use bonuses more than available" }
_currentCart.update { it.copyWith(usedBonuses = bonuses) }
}
private fun updateCartProducts(updateAction: MutableList<TProduct>.() -> Unit) {
_currentCart.update { cart ->
cart.copyWith(products = cart.products.toMutableList().apply(updateAction))

View File

@ -5,7 +5,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import ru.touchin.roboswag.cart_utils.models.CartModel
import ru.touchin.roboswag.cart_utils.models.ProductModel
import ru.touchin.roboswag.cart_utils.models.PromocodeModel
import ru.touchin.roboswag.cart_utils.repositories.IRemoteCartRepository
import ru.touchin.roboswag.cart_utils.repositories.LocalCartRepository
import ru.touchin.roboswag.cart_utils.requests_qeue.Request
@ -74,18 +73,6 @@ open class CartUpdateManager<TCart : CartModel<TProduct>, TProduct : ProductMode
}
}
open fun completelyDeleteProduct(id: Int) {
localCartRepository.removeProduct(id)
}
open fun applyPromocode(promocode: PromocodeModel) {
localCartRepository.applyPromocode(promocode)
}
open fun removePromocode(code: String) {
localCartRepository.removePromocode(code)
}
private suspend fun tryToGetRemoteCartAgain() {
repeat(maxRequestAttemptsCount) {
runCatching {