Add quantity textfield to Inventory item add dialog.

This commit is contained in:
Thomas Andres Gomez 2025-05-09 18:05:06 +02:00
parent 763f575be4
commit a2bfd6d775
10 changed files with 43 additions and 21 deletions

View file

@ -128,6 +128,7 @@ interface LwaClient {
suspend fun createInventoryItem(
characterSheetId: String,
itemId: String,
count: Float,
): APIResponse<String>
suspend fun changeInventoryItemCount(

View file

@ -219,8 +219,9 @@ class LwaClientImpl(
override suspend fun createInventoryItem(
characterSheetId: String,
itemId: String,
count: Float,
): APIResponse<String> = client
.put("$root/inventory/item/create?characterSheetId=$characterSheetId&itemId=$itemId")
.put("$root/inventory/item/create?characterSheetId=$characterSheetId&itemId=$itemId&count=$count")
.body<APIResponse<String>>()
@Throws

View file

@ -64,10 +64,12 @@ class InventoryRepository(
suspend fun createInventoryItem(
characterSheetId: String,
itemId: String,
count: Float,
): String {
return inventoryStore.createInventoryItem(
characterSheetId = characterSheetId,
itemId = itemId,
count = count,
)
}

View file

@ -103,10 +103,12 @@ class InventoryStore(
suspend fun createInventoryItem(
characterSheetId: String,
itemId: String,
count: Float,
): String {
val request = client.createInventoryItem(
characterSheetId = characterSheetId,
itemId = itemId,
count = count
)
if (request.success.not()) {
LwaClient.error(error = request)

View file

@ -159,8 +159,19 @@ fun ItemDetailDialog(
when (it) {
null -> Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
) {
if (state.countable != null) {
LwaTextField(
modifier = Modifier.width(width = 128.dp).height(32.dp),
colors = LwaTextFieldColors(
backgroundColor = MaterialTheme.lwa.colorScheme.elevated.base2dp,
),
field = state.countable,
)
}
Spacer(
modifier = Modifier.weight(1f),
)
TextButton(
onClick = { onAddItem(state) },
) {

View file

@ -1,10 +1,8 @@
package com.pixelized.desktop.lwa.ui.composable.character.item
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextField
import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextFieldFlow
import com.pixelized.shared.lwa.model.item.Item
import kotlinx.coroutines.flow.MutableStateFlow
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.character__inventory__description_empty__label
import lwacharactersheet.composeapp.generated.resources.character__inventory__inventory__dialog__count
@ -44,7 +42,7 @@ class ItemDetailDialogFactory {
value = format.format(count),
)
flow.createLwaTextField {
flow.errorFlow.value = isError(value = it)
flow.errorFlow.value = isError(inventoryId = inventoryId, value = it)
flow.valueFlow.value = it
}
},
@ -63,5 +61,10 @@ class ItemDetailDialogFactory {
null
}
private fun isError(value: String): Boolean = floatChecker.matches(value).not()
private fun isError(
inventoryId: String?,
value: String,
): Boolean {
return floatChecker.matches(value).not() || (inventoryId == null && value.toFloat() < 1f)
}
}

View file

@ -51,7 +51,7 @@ class ItemDetailDialogViewModel(
factory.convertToDialogUio(
characterSheetId = ids?.characterSheetId,
items = items,
count = selectedInventoryItem?.count ?: 0f,
count = selectedInventoryItem?.count ?: 1f,
equipped = selectedInventoryItem?.equipped ?: false,
inventoryId = ids?.inventoryId,
itemId = ids?.itemId,
@ -82,22 +82,21 @@ class ItemDetailDialogViewModel(
}
suspend fun onAddInventoryItem(
characterSheetId: String,
itemId: String,
dialog: ItemDetailDialogUio,
): Boolean {
try {
if (dialog.countable?.errorFlow?.value == true) return false
val quantity = dialog.countable?.valueFlow?.value ?: return false
val count = factory.parse(quantity = quantity)
?: quantity.toFloatOrNull()
?: 1f
// create the inventory item on the server, get the newly create id from that.
val inventoryId = inventoryRepository.createInventoryItem(
characterSheetId = characterSheetId,
itemId = itemId,
inventoryRepository.createInventoryItem(
characterSheetId = dialog.characterSheetId,
itemId = dialog.itemId,
count = count,
)
return true
// update the dialog with the id only if this dialog still correspond to this item. (should always be the case but hey).
// if (selectedItemId.value?.let { it.itemId == itemId && it.characterSheetId == characterSheetId } == true) {
// selectedItemId.update {
// it?.copy(inventoryId = inventoryId)
// }
// }
} catch (exception: Exception) {
val message = ErrorSnackUio.from(exception)
_error.emit(message)

View file

@ -195,8 +195,7 @@ fun CharacterDetailInventory(
onAddItem = { dialog ->
scope.launch {
val result = itemDetailDialogViewModel.onAddInventoryItem(
characterSheetId = dialog.characterSheetId,
itemId = dialog.itemId,
dialog = dialog,
)
if (result) {
blur.hide()

View file

@ -78,6 +78,7 @@ class InventoryService(
fun createInventoryItem(
characterSheetId: String,
itemId: String,
count: Float,
): String {
// get the inventory of the character, if none create one.
val inventory = inventoryStore.inventoryFlow().value[characterSheetId]
@ -88,7 +89,7 @@ class InventoryService(
val item = Inventory.Item(
inventoryId = inventoryId,
itemId = itemId,
count = 1f,
count = count,
equipped = false,
)
// update the inventory with the updated item.

View file

@ -2,6 +2,7 @@ package com.pixelized.server.lwa.server.rest.inventory
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.count
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.server.lwa.utils.extentions.itemId
import com.pixelized.shared.lwa.protocol.rest.APIResponse
@ -15,10 +16,12 @@ fun Engine.createInventoryItem(): suspend RoutingContext.() -> Unit {
// get the query parameter
val characterSheetId = call.queryParameters.characterSheetId
val itemId = call.queryParameters.itemId
val count = call.queryParameters.count
// add the item to the inventory.
val inventoryId = inventoryService.createInventoryItem(
characterSheetId = characterSheetId,
itemId = itemId,
count = count,
)
// API & WebSocket responses.
call.respond(