Closed manueldidonna closed 4 years ago
Maximize current items quantity
fun Inventory.applyToCurrentItems(quantity: Int = maxAllowedQuantity) {
val reusableItem = object : Inventory.Item {
override var index = 0
override var id = 0
override val quantity = quantity
}
val mutateItem = fun (index: Int, id: Int, quantity: Int) -> Inventory.Item {
reusableItem.index = index
reusableItem.id = id
return reusableItem
}
for (index in 0 until itemCounts) {
setItem(selectItem(index, mapTo = mutateItem))
}
}
Give max quantity of all items
fun Inventory.giveAllItems() {
val itemsCount = supportedItemIds.size.coerceAtMost(capacity)
val reusableItem = object : Inventory.Item {
override var index = 0
override var id = 0
override val quantity = maxAllowedQuantity
}
for (i in 0 until maxSupportedCount) {
reusableItem.index = i
reusableItem.id = supportedItemIds[i]
setItem(reusableItem)
}
}
Sort items by id
fun Inventory.sortItemsById() {
val items = MutableList(size) { getItem(it) }
items.sortBy { it.id }
items.forEachIndexed { index, item ->
setItem(item, index)
}
}
@Composable
fun LoadItems(saveData: SaveData, resources: PokemonTextResources.Items) {
val inventories: List<Inventory> = remember {
saveData
.supportedInventoryTypes
.map { type -> saveData.getInventory(type) }
.filterNotNull()
}
val itemNames = remember { resources.getItems(saveData.version) }
InventoryTabs(inventories) { inventory ->
ItemsList(inventory.getAllItems(), itemNames)
}
}
@Composable
fun ItemsList(items: List<Inventory.Item>, names: List<String>) {
AdapterList(items) { item ->
ItemRow(names[item.id], item.quantity)
}
}