Add button to add shopping item

This commit is contained in:
Reimar 2025-05-05 13:23:56 +02:00
parent f84cd6167b
commit a446ad934c
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
5 changed files with 126 additions and 7 deletions

View File

@ -39,8 +39,8 @@ fun request(ctx: Context, method: String, path: String, data: String?, autoRefre
outputStream.flush()
}
if (responseCode == 401 && refreshToken != null) {
if (!autoRefresh || !refreshAuthToken(ctx, refreshToken)) {
if (responseCode == 401) {
if (!autoRefresh || refreshToken == null || !refreshAuthToken(ctx, refreshToken)) {
val intent = Intent(ctx, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
ctx.startActivity(intent)

View File

@ -1,10 +1,14 @@
package tech.mercantec.easyeat.ui.shopping_list
import android.app.AlertDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.fragment.app.Fragment
import tech.mercantec.easyeat.R
import tech.mercantec.easyeat.databinding.FragmentShoppingListBinding
class ShoppingListFragment : Fragment() {
@ -23,6 +27,27 @@ class ShoppingListFragment : Fragment() {
_binding = FragmentShoppingListBinding.inflate(inflater, container, false)
val root: View = binding.root
binding.addToShoppingList.setOnClickListener {
val view = requireActivity().layoutInflater.inflate(R.layout.dialog_add_to_shopping_list, null)
val dialog = AlertDialog.Builder(activity)
.setView(view)
.setPositiveButton(R.string.add_label, { dialog, id ->
})
.setNegativeButton(R.string.cancel_label, { dialog, id ->
dialog.cancel()
})
.create()
dialog.show()
val adapter = ArrayAdapter.createFromResource(requireContext(), R.array.units, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
dialog.findViewById<Spinner>(R.id.unit_selector).adapter = adapter
}
return root
}

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_shopping_item_label"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="18sp"
/>
<TextView
android:id="@+id/amount_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:text="@string/ingredient_amount_label"
/>
<EditText
android:id="@+id/amount"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:layout_below="@id/amount_label"
android:layout_alignParentStart="true"
android:hint="@string/ingredient_amount_hint"
/>
<Spinner
android:id="@+id/unit_selector"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/amount"
android:layout_below="@+id/amount_label"
/>
<TextView
android:id="@+id/name_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_alignStart="@id/name"
android:text="@string/ingredient_name_label"
/>
<EditText
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name_label"
android:layout_toEndOf="@id/unit_selector"
android:layout_alignParentEnd="true"
android:hint="@string/ingredient_name_hint"
/>
</RelativeLayout>

View File

@ -1,9 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.shopping_list.ShoppingListFragment">
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<ListView
android:id="@+id/shopping_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_to_shopping_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:backgroundTint="@color/cyan"
app:srcCompat="@android:drawable/ic_input_add"
app:tint="@android:color/white"
/>
</FrameLayout>

View File

@ -26,4 +26,17 @@
<string name="general">General</string>
<string name="ingredients">Ingredients</string>
<string name="Instructions">Instructions</string>
<string name="cancel_label">Cancel</string>
<string name="add_shopping_item_label">Add shopping item</string>
<string name="ingredient_amount_label">Amount</string>
<string name="ingredient_amount_hint">500</string>
<string name="ingredient_name_label">Name</string>
<string name="ingredient_name_hint">Beef</string>
<string-array name="units">
<item>g</item>
<item>kg</item>
<item>ml</item>
<item>dl</item>
<item>l</item>
</string-array>
</resources>