Show shopping item immediately as it is added to the list
This commit is contained in:
parent
39bcdd9a9b
commit
fa877b2c5e
@ -94,6 +94,6 @@ inline fun <reified Req, reified Res> requestJson(ctx: Context, method: String,
|
|||||||
Log.e("EasyEat", e.message!!)
|
Log.e("EasyEat", e.message!!)
|
||||||
Log.e("EasyEat", response.body)
|
Log.e("EasyEat", response.body)
|
||||||
|
|
||||||
throw ApiRequestException("Failed to parse response: $response", e)
|
throw ApiRequestException("Failed to parse response: ${response.body}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,10 @@ fun getShoppingList(ctx: Context): Array<ShoppingListItem> {
|
|||||||
@Serializable
|
@Serializable
|
||||||
data class AddShoppingItemRequest(val name: String, val amount: Double?, val unit: String?, val checked: Boolean)
|
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)
|
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) {
|
fun toggleShoppingItemChecked(ctx: Context, item: ShoppingListItem) {
|
||||||
|
@ -128,17 +128,25 @@ class ShoppingListFragment : Fragment() {
|
|||||||
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).isEnabled = false
|
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).isEnabled = false
|
||||||
|
|
||||||
thread {
|
thread {
|
||||||
|
val item: ShoppingListItem
|
||||||
try {
|
try {
|
||||||
addShoppingItem(requireContext(), name, amount, unit)
|
item = addShoppingItem(requireContext(), name, amount, unit)
|
||||||
} catch (e: ApiRequestException) {
|
} catch (e: ApiRequestException) {
|
||||||
activity?.runOnUiThread {
|
activity?.runOnUiThread {
|
||||||
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
|
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return@thread
|
||||||
} finally {
|
} finally {
|
||||||
activity?.runOnUiThread {
|
activity?.runOnUiThread {
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
activity?.runOnUiThread {
|
||||||
|
val adapter = binding.shoppingList.adapter as ShoppingItemAdapter
|
||||||
|
adapter.insert(item, adapter.count)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.setNegativeButton(R.string.cancel_label) { dialog, _ ->
|
.setNegativeButton(R.string.cancel_label) { dialog, _ ->
|
||||||
|
Loading…
Reference in New Issue
Block a user