New Code

New Code
 avatar
unknown
kotlin
10 months ago
12 kB
15
Indexable
/**
     * Helper function to update quantity for custom SKU products
     */
    private fun updateCustomSKUQuantity(products: List<ProductResponseModel.Category.Product>?) {
        products?.forEach { product ->
            if (product.customSKUProduct == true && product.inStock == true) {
                product.quantity = product.prefillQuantity ?: 0
            }
        }
    }

    private fun processDataForProducts(productResponseModel: ProductResponseModel): Job {
        return viewModelScope.launch {
            if (!productResponseModel.categoryDataList.isNullOrEmpty()) {
                // Handle custom SKU products in first category
                productResponseModel.categoryDataList?.firstOrNull()?.let { firstCategory ->
                    updateCustomSKUQuantity(firstCategory.productInfo)
                    firstCategory.productVariantsInfo?.forEach { variantInfo ->
                        updateCustomSKUQuantity(variantInfo.productInfo)
                    }
                }

                // Create chip category lookup map for O(1) access
                val productIdToChipCategories = mutableMapOf<Int, MutableList<ProductResponseModel.Category>>()
                productResponseModel.chipsCategory?.forEach { chipCategory ->
                    chipCategory.productIds?.forEach { productId ->
                        productIdToChipCategories.getOrPut(productId) { mutableListOf() }.add(chipCategory)
                    }
                }

                productResponseModel.categoryDataList?.let { categoryDataList ->
                    // Setting categories
                    listCategory.clear()
                    listCategory.addAll(categoryDataList)
                    listCategoryWithOriginalSequence.clear()
                    listCategoryWithOriginalSequence.addAll(categoryDataList.map { it.copy() })
                    
                    // Setting products
                    listProduct.clear()
                    
                    categoryDataList.forEach { category ->
                        category.productVariantsInfo?.forEach { productVariantsInfo ->
                            val hasMultipleVariants = (productVariantsInfo.productInfo?.size ?: 0) > 1
                            val variantId = productVariantsInfo.id ?: (50000..60000).random()
                            val variantName = productVariantsInfo.name ?: ""
                            val variantDisplayName = productVariantsInfo.displayName ?: ""
                            
                            // Process all products in one pass
                            productVariantsInfo.productInfo?.forEach { product ->
                                product.parentCategoryId = category.categoryId
                                product.categoryBackgroundColor = category.categoryBackgroundColor
                                
                                if (hasMultipleVariants) {
                                    product.productVariantId = variantId
                                    product.productVariantName = variantName
                                    product.productVariantDisplayName = variantDisplayName
                                }
                            }
                            
                            listProduct.addAll(productVariantsInfo.productInfo ?: emptyList())
                            
                            // Add to chip categories using the lookup map
                            productIdToChipCategories[productVariantsInfo.id]?.forEach { chipCategory ->
                                if (chipCategory.productVariantsInfo.isNullOrEmpty()) {
                                    chipCategory.productVariantsInfo = arrayListOf()
                                }
                                
                                val variantCopy = productVariantsInfo.copy()
                                variantCopy.productInfo?.forEach { product ->
                                    product.parentCategoryId = chipCategory.categoryId
                                    product.categoryBackgroundColor = chipCategory.categoryBackgroundColor
                                    
                                    if (hasMultipleVariants) {
                                        product.productVariantId = variantId
                                        product.productVariantName = variantName
                                        product.productVariantDisplayName = variantDisplayName
                                    }
                                }
                                
                                chipCategory.productVariantsInfo?.add(variantCopy)
                            }
                            
                        } ?: kotlin.run {
                            // Handle products without variants
                            category.productInfo?.let { productInfo ->
                                productInfo.forEach { product ->
                                    product.parentCategoryId = category.categoryId
                                    product.categoryBackgroundColor = category.categoryBackgroundColor
                                }
                                listProduct.addAll(productInfo)
                                
                                // Add to chip categories
                                productInfo.forEach { product ->
                                    productIdToChipCategories[product.id]?.forEach { chipCategory ->
                                        if (chipCategory.productInfo.isNullOrEmpty()) {
                                            chipCategory.productInfo = arrayListOf()
                                        }
                                        
                                        val productCopy = product.copy().apply {
                                            parentCategoryId = chipCategory.categoryId
                                            categoryBackgroundColor = chipCategory.categoryBackgroundColor
                                        }
                                        chipCategory.productInfo?.add(productCopy)
                                    }
                                }
                            }
                        }
                    }
                }

                // Process chip categories
                productResponseModel.chipsCategory?.let { chipsCategory ->
                    listChipsCategory.clear()
                    chipsCategory.forEach { chipCategory ->
                        chipCategory.productIds?.let { ids ->
                            val orderMap = ids.withIndex().associate { it.value to it.index }
                            
                            chipCategory.productInfo = chipCategory.productInfo
                                ?.sortedBy { orderMap[it.id] ?: Int.MAX_VALUE }
                                ?.let { ArrayList(it) }
                            
                            chipCategory.productVariantsInfo = chipCategory.productVariantsInfo
                                ?.sortedBy { orderMap[it.id] ?: Int.MAX_VALUE }
                                ?.let { ArrayList(it) }
                            
                            chipCategory.isChip = true
                        }
                        
                        if (!chipCategory.productVariantsInfo.isNullOrEmpty() || !chipCategory.productInfo.isNullOrEmpty()) {
                            listChipsCategory.add(chipCategory)
                        }
                    }
                }

                // Create product map for efficient lookups
                val productMap = listProduct.associateBy { it.id }
                
                // Update old cart items
                updateOldCartItems(listProduct.filter { (it.previousOrderQuantity ?: 0) > 0 }.map { it.deepCopy() })
                
                // Update previous order quantity
                listProduct.forEach { product ->
                    if ((product.previousOrderQuantity ?: 0) > 0 && product.customSKUProduct != true) {
                        product.quantity = product.previousOrderQuantity ?: 0
                        product.cartQuantity = product.previousOrderQuantity ?: 0
                    }
                }
                
                // Update from saved cart
                cartPersistData.value?.cartProductsInfo?.forEach { cartProduct ->
                    productMap[cartProduct.productId]?.let { product ->
                        if ((product.inStock == true || (product.inStock == false && product.thresholdBreached == true)) 
                            && product.customSKUProduct != true) {
                            product.quantity = cartProduct.quantity
                            product.cartQuantity = cartProduct.quantity
                        }
                    }
                }
                
                reorderCartPersist?.productInfo?.forEach { cartProduct ->
                    productMap[cartProduct.productId]?.let { product ->
                        if (product.inStock == true || (product.inStock == false && product.thresholdBreached == true)) {
                            product.quantity = cartProduct.quantity
                            product.cartQuantity = cartProduct.quantity
                        }
                    }
                }
                
                // Process variants more efficiently
                val variantGroups = listProduct
                    .filter { it.productVariantId != null && (it.quantity > 0 || it.default == true) }
                    .groupBy { it.productVariantId }
                
                val defaultFalseVariants = mutableSetOf<Int>()
                
                variantGroups.forEach { (variantId, products) ->
                    val selectedProducts = products.filter { it.quantity > 0 }
                    val defaultProduct = products.find { it.default == true }
                    
                    defaultProduct?.let { product ->
                        if (product.quantity > 0 && selectedProducts.size > 1) {
                            val variantsList = ArrayList(selectedProducts)
                            variantsList.removeAll { it.id == product.id }
                            variantsList.add(product.deepCopy())
                            product.listVariants = ArrayList(variantsList.map { it.copy() })
                        } else if (product.quantity == 0) {
                            defaultFalseVariants.add(variantId ?: 0)
                        }
                    }
                }
                
                // Handle default false variants
                defaultFalseVariants.forEach { variantId ->
                    val selectedProducts = listProduct.filter { 
                        it.productVariantId == variantId && it.quantity > 0 
                    }
                    
                    selectedProducts.firstOrNull()?.let { newDefault ->
                        // Reset old default
                        listProduct.find { it.productVariantId == variantId && it.default == true }?.apply {
                            default = false
                            listVariants = arrayListOf()
                        }
                        
                        // Set new default
                        newDefault.default = true
                        if (selectedProducts.size > 1) {
                            val variantsList = ArrayList(selectedProducts)
                            variantsList.removeAll { it.id == newDefault.id }
                            variantsList.add(newDefault)
                            newDefault.listVariants = ArrayList(variantsList.map { it.copy() })
                        }
                    }
                }
            }
            
            // Create final product map
            listProductMap = listProduct
                .asSequence()
                .filter { it.inStock == true }
                .associateBy { it.id }
        }
    }
Editor is loading...
Leave a Comment