Show shopping list

This commit is contained in:
Reimar 2025-05-08 09:16:12 +02:00
parent dc7e94b37b
commit f924d2083b
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
6 changed files with 90 additions and 6 deletions

View File

@ -2,9 +2,7 @@ package tech.mercantec.easyeat.helpers
import android.content.Context
import kotlinx.serialization.Serializable
@Serializable
data class ShoppingListItem(val id: Int, val name: String, val amount: Double?, val unit: String?, val checked: Boolean)
import tech.mercantec.easyeat.models.ShoppingListItem
fun getShoppingList(ctx: Context): Array<ShoppingListItem> {
return requestJson<Unit, Array<ShoppingListItem>>(ctx, "GET", "/api/ShoppingList/get", null)

View File

@ -0,0 +1,6 @@
package tech.mercantec.easyeat.models
import kotlinx.serialization.Serializable
@Serializable
data class ShoppingListItem(val id: Int, val name: String, val amount: Double?, val unit: String?, val checked: Boolean)

View File

@ -0,0 +1,28 @@
package tech.mercantec.easyeat.ui.shopping_list
import android.content.Context
import android.icu.text.DecimalFormat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import tech.mercantec.easyeat.R
import tech.mercantec.easyeat.models.ShoppingListItem
class ShoppingItemAdapter(context: Context, items: Array<ShoppingListItem>)
: ArrayAdapter<ShoppingListItem>(context, R.layout.shopping_list_item, items) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val item = getItem(position)
val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.shopping_list_item, parent, false)
item?.let { item ->
view.findViewById<TextView>(R.id.amount).text = DecimalFormat("#.##").format(item.amount)
view.findViewById<TextView>(R.id.unit).text = item.unit
view.findViewById<TextView>(R.id.name).text = item.name
}
return view
}
}

View File

@ -15,6 +15,8 @@ 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.getShoppingList
import tech.mercantec.easyeat.models.ShoppingListItem
import kotlin.concurrent.thread
class ShoppingListFragment : Fragment() {
@ -33,6 +35,23 @@ class ShoppingListFragment : Fragment() {
_binding = FragmentShoppingListBinding.inflate(inflater, container, false)
val root: View = binding.root
thread {
val items: Array<ShoppingListItem>
try {
items = getShoppingList(requireContext())
} catch (e: ApiRequestException) {
activity?.runOnUiThread {
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
}
return@thread
}
activity?.runOnUiThread {
binding.shoppingList.adapter = ShoppingItemAdapter(requireContext(), items)
}
}
binding.addToShoppingList.setOnClickListener {
val view = requireActivity().layoutInflater.inflate(R.layout.dialog_add_to_shopping_list, null)

View File

@ -2,11 +2,9 @@
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment_activity_main"

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/amount"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textAlignment="textEnd"
android:textStyle="bold"
/>
<TextView
android:id="@+id/unit"
android:layout_marginStart="3dp"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/name"
android:layout_marginStart="3dp"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>