Add quantity textfield to Inventory item add dialog.
This commit is contained in:
parent
763f575be4
commit
a2bfd6d775
10 changed files with 43 additions and 21 deletions
|
|
@ -128,6 +128,7 @@ interface LwaClient {
|
||||||
suspend fun createInventoryItem(
|
suspend fun createInventoryItem(
|
||||||
characterSheetId: String,
|
characterSheetId: String,
|
||||||
itemId: String,
|
itemId: String,
|
||||||
|
count: Float,
|
||||||
): APIResponse<String>
|
): APIResponse<String>
|
||||||
|
|
||||||
suspend fun changeInventoryItemCount(
|
suspend fun changeInventoryItemCount(
|
||||||
|
|
|
||||||
|
|
@ -219,8 +219,9 @@ class LwaClientImpl(
|
||||||
override suspend fun createInventoryItem(
|
override suspend fun createInventoryItem(
|
||||||
characterSheetId: String,
|
characterSheetId: String,
|
||||||
itemId: String,
|
itemId: String,
|
||||||
|
count: Float,
|
||||||
): APIResponse<String> = client
|
): 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>>()
|
.body<APIResponse<String>>()
|
||||||
|
|
||||||
@Throws
|
@Throws
|
||||||
|
|
|
||||||
|
|
@ -64,10 +64,12 @@ class InventoryRepository(
|
||||||
suspend fun createInventoryItem(
|
suspend fun createInventoryItem(
|
||||||
characterSheetId: String,
|
characterSheetId: String,
|
||||||
itemId: String,
|
itemId: String,
|
||||||
|
count: Float,
|
||||||
): String {
|
): String {
|
||||||
return inventoryStore.createInventoryItem(
|
return inventoryStore.createInventoryItem(
|
||||||
characterSheetId = characterSheetId,
|
characterSheetId = characterSheetId,
|
||||||
itemId = itemId,
|
itemId = itemId,
|
||||||
|
count = count,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,10 +103,12 @@ class InventoryStore(
|
||||||
suspend fun createInventoryItem(
|
suspend fun createInventoryItem(
|
||||||
characterSheetId: String,
|
characterSheetId: String,
|
||||||
itemId: String,
|
itemId: String,
|
||||||
|
count: Float,
|
||||||
): String {
|
): String {
|
||||||
val request = client.createInventoryItem(
|
val request = client.createInventoryItem(
|
||||||
characterSheetId = characterSheetId,
|
characterSheetId = characterSheetId,
|
||||||
itemId = itemId,
|
itemId = itemId,
|
||||||
|
count = count
|
||||||
)
|
)
|
||||||
if (request.success.not()) {
|
if (request.success.not()) {
|
||||||
LwaClient.error(error = request)
|
LwaClient.error(error = request)
|
||||||
|
|
|
||||||
|
|
@ -159,8 +159,19 @@ fun ItemDetailDialog(
|
||||||
when (it) {
|
when (it) {
|
||||||
null -> Row(
|
null -> Row(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
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(
|
TextButton(
|
||||||
onClick = { onAddItem(state) },
|
onClick = { onAddItem(state) },
|
||||||
) {
|
) {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
package com.pixelized.desktop.lwa.ui.composable.character.item
|
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.createLwaTextField
|
||||||
import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextFieldFlow
|
import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextFieldFlow
|
||||||
import com.pixelized.shared.lwa.model.item.Item
|
import com.pixelized.shared.lwa.model.item.Item
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import lwacharactersheet.composeapp.generated.resources.Res
|
import lwacharactersheet.composeapp.generated.resources.Res
|
||||||
import lwacharactersheet.composeapp.generated.resources.character__inventory__description_empty__label
|
import lwacharactersheet.composeapp.generated.resources.character__inventory__description_empty__label
|
||||||
import lwacharactersheet.composeapp.generated.resources.character__inventory__inventory__dialog__count
|
import lwacharactersheet.composeapp.generated.resources.character__inventory__inventory__dialog__count
|
||||||
|
|
@ -44,7 +42,7 @@ class ItemDetailDialogFactory {
|
||||||
value = format.format(count),
|
value = format.format(count),
|
||||||
)
|
)
|
||||||
flow.createLwaTextField {
|
flow.createLwaTextField {
|
||||||
flow.errorFlow.value = isError(value = it)
|
flow.errorFlow.value = isError(inventoryId = inventoryId, value = it)
|
||||||
flow.valueFlow.value = it
|
flow.valueFlow.value = it
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -63,5 +61,10 @@ class ItemDetailDialogFactory {
|
||||||
null
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -51,7 +51,7 @@ class ItemDetailDialogViewModel(
|
||||||
factory.convertToDialogUio(
|
factory.convertToDialogUio(
|
||||||
characterSheetId = ids?.characterSheetId,
|
characterSheetId = ids?.characterSheetId,
|
||||||
items = items,
|
items = items,
|
||||||
count = selectedInventoryItem?.count ?: 0f,
|
count = selectedInventoryItem?.count ?: 1f,
|
||||||
equipped = selectedInventoryItem?.equipped ?: false,
|
equipped = selectedInventoryItem?.equipped ?: false,
|
||||||
inventoryId = ids?.inventoryId,
|
inventoryId = ids?.inventoryId,
|
||||||
itemId = ids?.itemId,
|
itemId = ids?.itemId,
|
||||||
|
|
@ -82,22 +82,21 @@ class ItemDetailDialogViewModel(
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun onAddInventoryItem(
|
suspend fun onAddInventoryItem(
|
||||||
characterSheetId: String,
|
dialog: ItemDetailDialogUio,
|
||||||
itemId: String,
|
|
||||||
): Boolean {
|
): Boolean {
|
||||||
try {
|
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.
|
// create the inventory item on the server, get the newly create id from that.
|
||||||
val inventoryId = inventoryRepository.createInventoryItem(
|
inventoryRepository.createInventoryItem(
|
||||||
characterSheetId = characterSheetId,
|
characterSheetId = dialog.characterSheetId,
|
||||||
itemId = itemId,
|
itemId = dialog.itemId,
|
||||||
|
count = count,
|
||||||
)
|
)
|
||||||
return true
|
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) {
|
} catch (exception: Exception) {
|
||||||
val message = ErrorSnackUio.from(exception)
|
val message = ErrorSnackUio.from(exception)
|
||||||
_error.emit(message)
|
_error.emit(message)
|
||||||
|
|
|
||||||
|
|
@ -195,8 +195,7 @@ fun CharacterDetailInventory(
|
||||||
onAddItem = { dialog ->
|
onAddItem = { dialog ->
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val result = itemDetailDialogViewModel.onAddInventoryItem(
|
val result = itemDetailDialogViewModel.onAddInventoryItem(
|
||||||
characterSheetId = dialog.characterSheetId,
|
dialog = dialog,
|
||||||
itemId = dialog.itemId,
|
|
||||||
)
|
)
|
||||||
if (result) {
|
if (result) {
|
||||||
blur.hide()
|
blur.hide()
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ class InventoryService(
|
||||||
fun createInventoryItem(
|
fun createInventoryItem(
|
||||||
characterSheetId: String,
|
characterSheetId: String,
|
||||||
itemId: String,
|
itemId: String,
|
||||||
|
count: Float,
|
||||||
): String {
|
): String {
|
||||||
// get the inventory of the character, if none create one.
|
// get the inventory of the character, if none create one.
|
||||||
val inventory = inventoryStore.inventoryFlow().value[characterSheetId]
|
val inventory = inventoryStore.inventoryFlow().value[characterSheetId]
|
||||||
|
|
@ -88,7 +89,7 @@ class InventoryService(
|
||||||
val item = Inventory.Item(
|
val item = Inventory.Item(
|
||||||
inventoryId = inventoryId,
|
inventoryId = inventoryId,
|
||||||
itemId = itemId,
|
itemId = itemId,
|
||||||
count = 1f,
|
count = count,
|
||||||
equipped = false,
|
equipped = false,
|
||||||
)
|
)
|
||||||
// update the inventory with the updated item.
|
// update the inventory with the updated item.
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.pixelized.server.lwa.server.rest.inventory
|
||||||
|
|
||||||
import com.pixelized.server.lwa.server.Engine
|
import com.pixelized.server.lwa.server.Engine
|
||||||
import com.pixelized.server.lwa.utils.extentions.characterSheetId
|
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.exception
|
||||||
import com.pixelized.server.lwa.utils.extentions.itemId
|
import com.pixelized.server.lwa.utils.extentions.itemId
|
||||||
import com.pixelized.shared.lwa.protocol.rest.APIResponse
|
import com.pixelized.shared.lwa.protocol.rest.APIResponse
|
||||||
|
|
@ -15,10 +16,12 @@ fun Engine.createInventoryItem(): suspend RoutingContext.() -> Unit {
|
||||||
// get the query parameter
|
// get the query parameter
|
||||||
val characterSheetId = call.queryParameters.characterSheetId
|
val characterSheetId = call.queryParameters.characterSheetId
|
||||||
val itemId = call.queryParameters.itemId
|
val itemId = call.queryParameters.itemId
|
||||||
|
val count = call.queryParameters.count
|
||||||
// add the item to the inventory.
|
// add the item to the inventory.
|
||||||
val inventoryId = inventoryService.createInventoryItem(
|
val inventoryId = inventoryService.createInventoryItem(
|
||||||
characterSheetId = characterSheetId,
|
characterSheetId = characterSheetId,
|
||||||
itemId = itemId,
|
itemId = itemId,
|
||||||
|
count = count,
|
||||||
)
|
)
|
||||||
// API & WebSocket responses.
|
// API & WebSocket responses.
|
||||||
call.respond(
|
call.respond(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue