Is there a way to simplify this logic? ``` ...
# codereview
s
Is there a way to simplify this logic?
Copy code
var qtdTotal = 0.0f
            val partition = response?.partition { it.uidCaracteristica != 0 }
            val uidGroup = partition?.first?.groupBy { it.produto }?.toMutableMap() ?: mutableMapOf()
            uidGroup[0] = partition?.second ?: listOf()
            uidGroup.forEach { (key, uidsList) ->
                if (key == 0) {
                    uidsList.forEach { stk ->
                        addRow(
                            stk.description ?: "", "", "", "",
                            extraInfo = listOf(
                                stk.produto.toString(),
                                stk.description ?: "",
                                stk.codbarras ?: "",
                                stk.referencia ?: ""
                            )
                        )
                        qtdTotal += stk.stock ?: 0.0f
                    }
                } else {
                    val caracteristicas = uidsList.map {
                            ReportsDataset.Row(
                                it.caracteristica ?: "",
                                it.stkmin?.toString() ?: "",
                                it.stkmax?.toString() ?: "",
                                it.stock?.toString() ?: "",
                                extraInfo = listOf(it.uidCaracteristica?.toString() ?: "")
                            )
                        }
                    val stk = uidsList.first()
                    addRow(
                        stk.description ?: "", "", "", "",
                        extraRows = caracteristicas,
                        extraInfo = listOf(
                            stk.produto.toString(),
                            stk.description ?: "",
                            stk.codbarras ?: "",
                            stk.referencia ?: ""
                        )
                    )
                    qtdTotal += stk.stock ?: 0.0f
                }
            }
r
Can you give a summary of the problem you're trying to solve? That would help to understand the code.
s
The problem is solved with this big block of code, but it seems very repetitive.
response
is a list of products, each product with
uidCaracteristica != 0
must be organised under the same
Row
other products
uidCaracteristica == 0
must be a single
Row
need the total stock also, but that's easy
j

https://i.imgur.com/cvsYT4t.png

s
Locate Duplicates is not available in my AS, is that a plugin?