Add progress bar and empty message to shopping list

This commit is contained in:
Reimar 2025-05-08 11:02:04 +02:00
parent fa877b2c5e
commit df0c951100
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
3 changed files with 37 additions and 20 deletions

View File

@ -1,7 +1,6 @@
package tech.mercantec.easyeat.ui.shopping_list
import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -19,26 +18,22 @@ 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
import tech.mercantec.easyeat.models.ShoppingListItem
import java.util.ArrayList
import kotlin.concurrent.thread
class ShoppingListFragment : Fragment() {
private var _binding: FragmentShoppingListBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentShoppingListBinding.inflate(inflater, container, false)
val root: View = binding.root
val binding = FragmentShoppingListBinding.inflate(inflater, container, false)
binding.shoppingList.visibility = View.GONE
binding.emptyShoppingList.visibility = View.GONE
binding.addToShoppingList.visibility = View.GONE
binding.loading.visibility = View.VISIBLE
// Fetch shopping list items
thread {
@ -48,12 +43,20 @@ class ShoppingListFragment : Fragment() {
} catch (e: ApiRequestException) {
activity?.runOnUiThread {
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
binding.loading.visibility = View.GONE
}
return@thread
}
activity?.runOnUiThread {
binding.shoppingList.visibility = View.VISIBLE
binding.emptyShoppingList.visibility = View.VISIBLE
binding.addToShoppingList.visibility = View.VISIBLE
binding.loading.visibility = View.GONE
binding.shoppingList.emptyView = binding.emptyShoppingList
binding.shoppingList.adapter = ShoppingItemAdapter(requireContext(), ArrayList(items.toMutableList()))
}
}
@ -162,11 +165,6 @@ class ShoppingListFragment : Fragment() {
dialog.findViewById<Spinner>(R.id.unit_selector).adapter = adapter
}
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
return binding.root
}
}

View File

@ -1,10 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
android:indeterminate="true"
/>
<ListView
android:id="@+id/shopping_list"
android:layout_width="match_parent"
@ -13,15 +22,24 @@
android:dividerHeight="1dp"
/>
<TextView
android:id="@+id/empty_shopping_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/empty_shopping_list"
/>
<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_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16dp"
android:backgroundTint="@color/cyan"
app:srcCompat="@android:drawable/ic_input_add"
app:tint="@android:color/white"
/>
</FrameLayout>
</RelativeLayout>

View File

@ -40,6 +40,7 @@
<string name="logout_label">Log out</string>
<string name="checked_desc">Checked</string>
<string name="delete_label">Delete</string>
<string name="empty_shopping_list">Your shopping list is empty</string>
<string-array name="units">
<item></item>
<item>g</item>