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

View File

@ -1,10 +1,19 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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 <ListView
android:id="@+id/shopping_list" android:id="@+id/shopping_list"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -13,15 +22,24 @@
android:dividerHeight="1dp" 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 <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_to_shopping_list" android:id="@+id/add_to_shopping_list"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="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:layout_margin="16dp"
android:backgroundTint="@color/cyan" android:backgroundTint="@color/cyan"
app:srcCompat="@android:drawable/ic_input_add" app:srcCompat="@android:drawable/ic_input_add"
app:tint="@android:color/white" app:tint="@android:color/white"
/> />
</FrameLayout> </RelativeLayout>

View File

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