Show shopping item immediately as it is added to the list

This commit is contained in:
Reimar 2025-05-08 10:47:10 +02:00
parent 39bcdd9a9b
commit fa877b2c5e
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
3 changed files with 12 additions and 4 deletions

View File

@ -94,6 +94,6 @@ inline fun <reified Req, reified Res> requestJson(ctx: Context, method: String,
Log.e("EasyEat", e.message!!)
Log.e("EasyEat", response.body)
throw ApiRequestException("Failed to parse response: $response", e)
throw ApiRequestException("Failed to parse response: ${response.body}", e)
}
}

View File

@ -11,10 +11,10 @@ fun getShoppingList(ctx: Context): Array<ShoppingListItem> {
@Serializable
data class AddShoppingItemRequest(val name: String, val amount: Double?, val unit: String?, val checked: Boolean)
fun addShoppingItem(ctx: Context, name: String, amount: Double?, unit: String?) {
fun addShoppingItem(ctx: Context, name: String, amount: Double?, unit: String?): ShoppingListItem {
val request = AddShoppingItemRequest(name, amount, unit, false)
requestJson<AddShoppingItemRequest, Boolean>(ctx, "POST", "/api/ShoppingList/add", request)
return requestJson<AddShoppingItemRequest, ShoppingListItem>(ctx, "POST", "/api/ShoppingList/add", request)
}
fun toggleShoppingItemChecked(ctx: Context, item: ShoppingListItem) {

View File

@ -128,17 +128,25 @@ class ShoppingListFragment : Fragment() {
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).isEnabled = false
thread {
val item: ShoppingListItem
try {
addShoppingItem(requireContext(), name, amount, unit)
item = addShoppingItem(requireContext(), name, amount, unit)
} catch (e: ApiRequestException) {
activity?.runOnUiThread {
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
}
return@thread
} finally {
activity?.runOnUiThread {
dialog.dismiss()
}
}
activity?.runOnUiThread {
val adapter = binding.shoppingList.adapter as ShoppingItemAdapter
adapter.insert(item, adapter.count)
}
}
}
.setNegativeButton(R.string.cancel_label) { dialog, _ ->