https://kotlinlang.org logo
#codereview
Title
# codereview
s

Sergio C.

11/30/2021, 6:23 PM
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

Richard Gomez

12/02/2021, 2:35 PM
Can you give a summary of the problem you're trying to solve? That would help to understand the code.
s

Sergio C.

12/02/2021, 6:42 PM
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

jimn

12/02/2021, 7:21 PM

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

s

Sergio C.

12/03/2021, 9:15 AM
Locate Duplicates is not available in my AS, is that a plugin?
2 Views