Compare commits

...

2 Commits

Author SHA1 Message Date
b3b4562f21
Allow checking/unchecking shopping items 2025-05-08 10:06:48 +02:00
f924d2083b
Show shopping list 2025-05-08 10:06:48 +02:00
12 changed files with 177 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)
@ -18,3 +16,7 @@ fun addShoppingItem(ctx: Context, name: String, amount: Double?, unit: String?)
requestJson<AddShoppingItemRequest, Boolean>(ctx, "POST", "/api/ShoppingList/add", request)
}
fun toggleShoppingItemChecked(ctx: Context, item: ShoppingListItem) {
requestJson<Unit, Boolean>(ctx, "PUT", "/api/ShoppingList/check?itemId=${item.id}", null)
}

View File

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

View File

@ -0,0 +1,60 @@
package tech.mercantec.easyeat.ui.shopping_list
import android.content.Context
import android.graphics.Paint
import android.icu.text.DecimalFormat
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import tech.mercantec.easyeat.R
import tech.mercantec.easyeat.models.ShoppingListItem
class ShoppingItemAdapter(context: Context, items: ArrayList<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 ->
val checkmarkView = view.findViewById<ImageView>(R.id.checkmark)
val amountView = view.findViewById<TextView>(R.id.amount)
val unitView = view.findViewById<TextView>(R.id.unit)
val nameView = view.findViewById<TextView>(R.id.name)
val textViews = listOf(amountView, unitView, nameView)
amountView.text = DecimalFormat("#.##").format(item.amount)
unitView.text = item.unit
nameView.text = item.name
if (item.checked) {
textViews.forEach {
it.paintFlags = it.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
val color = TypedValue()
context.theme.resolveAttribute(R.attr.colorDisabled, color, true)
it.setTextColor(ContextCompat.getColor(context, color.resourceId))
checkmarkView.visibility = View.VISIBLE
}
} else {
textViews.forEach {
it.paintFlags = it.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
val color = TypedValue()
context.theme.resolveAttribute(android.R.attr.textColorSecondary, color, true)
it.setTextColor(ContextCompat.getColor(context, color.resourceId))
checkmarkView.visibility = View.INVISIBLE
}
}
}
return view
}
}

View File

@ -15,6 +15,11 @@ 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.helpers.toggleShoppingItemChecked
import tech.mercantec.easyeat.models.Dish
import tech.mercantec.easyeat.models.ShoppingListItem
import java.util.ArrayList
import kotlin.concurrent.thread
class ShoppingListFragment : Fragment() {
@ -33,6 +38,41 @@ 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(), ArrayList(items.toMutableList()))
}
}
binding.shoppingList.setOnItemClickListener { parent, view, position, id ->
val item = parent.getItemAtPosition(position) as ShoppingListItem
item.checked = !item.checked
thread {
try {
toggleShoppingItemChecked(requireContext(), item)
} catch (e: ApiRequestException) {
Toast.makeText(requireContext(), e.message, Toast.LENGTH_LONG).show()
}
}
val adapter = parent.adapter as ShoppingItemAdapter
adapter.remove(item)
adapter.insert(item, position)
}
binding.addToShoppingList.setOnClickListener {
val view = requireActivity().layoutInflater.inflate(R.layout.dialog_add_to_shopping_list, null)

View 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="M382,720L154,492L211,435L382,606L749,239L806,296L382,720Z"/>
</vector>

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,45 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:padding="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/checkmark"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/ic_check_24px"
app:tint="@color/green"
android:contentDescription="@string/checked_desc"
/>
<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>

View File

@ -4,6 +4,7 @@
<item name="colorPrimaryVariant">@color/dark_cyan</item>
<item name="colorOnPrimary">@color/white</item>
<item name="colorSecondary">@color/dark_gray</item>
<item name="colorDisabled">@color/dark_gray</item>
<item name="colorSurface">@color/dark_cyan</item>
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<item name="android:colorBackground">@color/black</item>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="EasyEat">
<attr name="colorDisabled" format="color" />
</declare-styleable>
</resources>

View File

@ -7,4 +7,5 @@
<color name="black">#FF242424</color>
<color name="white">#FFF9F9F9</color>
<color name="red">#D62D2D </color>
<color name="green">#4CAF50</color>
</resources>

View File

@ -38,6 +38,7 @@
<string name="ingredient_name_hint">Beef</string>
<string name="change_password_label">Change password</string>
<string name="logout_label">Log out</string>
<string name="checked_desc">Checked</string>
<string-array name="units">
<item></item>
<item>g</item>

View File

@ -4,6 +4,7 @@
<item name="colorPrimaryVariant">@color/dark_cyan</item>
<item name="colorOnPrimary">@color/white</item>
<item name="colorSecondary">@color/dark_gray</item>
<item name="colorDisabled">@color/gray</item>
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<item name="android:colorBackground">@color/white</item>
</style>