Implement deleting shopping items
This commit is contained in:
parent
7e1583f485
commit
39bcdd9a9b
@ -20,3 +20,7 @@ fun addShoppingItem(ctx: Context, name: String, amount: Double?, unit: String?)
|
||||
fun toggleShoppingItemChecked(ctx: Context, item: ShoppingListItem) {
|
||||
requestJson<Unit, Boolean>(ctx, "PUT", "/api/ShoppingList/check?itemId=${item.id}", null)
|
||||
}
|
||||
|
||||
fun deleteShoppingItem(ctx: Context, item: ShoppingListItem) {
|
||||
requestJson<Unit, Boolean>(ctx, "DELETE", "/api/ShoppingList/delete?itemId=${item.id}", null)
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.EditText
|
||||
import android.widget.PopupMenu
|
||||
import android.widget.Spinner
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
@ -15,6 +16,7 @@ import tech.mercantec.easyeat.R
|
||||
import tech.mercantec.easyeat.databinding.FragmentShoppingListBinding
|
||||
import tech.mercantec.easyeat.helpers.ApiRequestException
|
||||
import tech.mercantec.easyeat.helpers.addShoppingItem
|
||||
import tech.mercantec.easyeat.helpers.deleteShoppingItem
|
||||
import tech.mercantec.easyeat.helpers.getShoppingList
|
||||
import tech.mercantec.easyeat.helpers.toggleShoppingItemChecked
|
||||
import tech.mercantec.easyeat.models.Dish
|
||||
@ -38,6 +40,7 @@ class ShoppingListFragment : Fragment() {
|
||||
_binding = FragmentShoppingListBinding.inflate(inflater, container, false)
|
||||
val root: View = binding.root
|
||||
|
||||
// Fetch shopping list items
|
||||
thread {
|
||||
val items: Array<ShoppingListItem>
|
||||
try {
|
||||
@ -55,6 +58,7 @@ class ShoppingListFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
// Check / uncheck when clicking shopping item
|
||||
binding.shoppingList.setOnItemClickListener { parent, view, position, id ->
|
||||
val item = parent.getItemAtPosition(position) as ShoppingListItem
|
||||
item.checked = !item.checked
|
||||
@ -73,6 +77,41 @@ class ShoppingListFragment : Fragment() {
|
||||
adapter.insert(item, position)
|
||||
}
|
||||
|
||||
// Show context menu when long clicking shopping item
|
||||
binding.shoppingList.setOnItemLongClickListener { parent, view, position, id ->
|
||||
val item = parent.getItemAtPosition(position) as ShoppingListItem
|
||||
|
||||
val popup = PopupMenu(requireActivity(), view)
|
||||
popup.apply {
|
||||
menuInflater.inflate(R.menu.shopping_item_context_menu, menu)
|
||||
setOnMenuItemClickListener {
|
||||
when (it.itemId) {
|
||||
R.id.remove_shopping_item -> {
|
||||
(parent.adapter as ShoppingItemAdapter).remove(item)
|
||||
|
||||
thread {
|
||||
try {
|
||||
deleteShoppingItem(requireContext(), item)
|
||||
} catch (e: ApiRequestException) {
|
||||
activity?.runOnUiThread {
|
||||
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
show()
|
||||
}
|
||||
|
||||
|
||||
return@setOnItemLongClickListener true
|
||||
}
|
||||
|
||||
// Show new item dialog when clicking add
|
||||
binding.addToShoppingList.setOnClickListener {
|
||||
val view = requireActivity().layoutInflater.inflate(R.layout.dialog_add_to_shopping_list, null)
|
||||
|
||||
|
10
app/app/src/main/res/drawable/ic_delete_24px.xml
Normal file
10
app/app/src/main/res/drawable/ic_delete_24px.xml
Normal file
@ -0,0 +1,10 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="960"
|
||||
android:viewportHeight="960"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M280,840Q247,840 223.5,816.5Q200,793 200,760L200,240L160,240L160,160L360,160L360,120L600,120L600,160L800,160L800,240L760,240L760,760Q760,793 736.5,816.5Q713,840 680,840L280,840ZM360,680L440,680L440,320L360,320L360,680ZM520,680L600,680L600,320L520,320L520,680Z"/>
|
||||
</vector>
|
8
app/app/src/main/res/menu/shopping_item_context_menu.xml
Normal file
8
app/app/src/main/res/menu/shopping_item_context_menu.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item
|
||||
android:id="@+id/remove_shopping_item"
|
||||
android:icon="@drawable/ic_delete_24px"
|
||||
android:title="@string/delete_label"
|
||||
/>
|
||||
</menu>
|
@ -39,6 +39,7 @@
|
||||
<string name="change_password_label">Change password</string>
|
||||
<string name="logout_label">Log out</string>
|
||||
<string name="checked_desc">Checked</string>
|
||||
<string name="delete_label">Delete</string>
|
||||
<string-array name="units">
|
||||
<item></item>
|
||||
<item>g</item>
|
||||
|
Loading…
Reference in New Issue
Block a user