Move dish list into fragment, rename home fragment, add icon

This commit is contained in:
Reimar 2025-04-28 10:44:55 +02:00
parent bbd937ff65
commit 00ecfc8485
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
12 changed files with 90 additions and 135 deletions

View File

@ -1,8 +1,6 @@
package tech.mercantec.easyeat
import android.os.Bundle
import android.widget.ListView
import android.widget.Toast
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
@ -10,8 +8,6 @@ import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import tech.mercantec.easyeat.databinding.ActivityMainBinding
import tech.mercantec.easyeat.ui.home.DishAdapter
import tech.mercantec.easyeat.models.Dish
class MainActivity : AppCompatActivity() {
@ -28,32 +24,10 @@ class MainActivity : AppCompatActivity() {
val navController = findNavController(R.id.nav_host_fragment_activity_main)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
R.id.navigation_dishes, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
val listView: ListView = findViewById(R.id.dishesList)
val listItems = arrayOf(
Dish("Spaghetti Bolognese", "Beef", 70.5),
Dish("Margherita Pizza", "Cheese", 60.0),
Dish("Chicken Curry", "Chicken", 80.2),
Dish("Vegetable Stir Fry", "Mixed Vegetables", 50.5),
Dish("Sushi", "Fish", 100.0),
Dish("Beef Tacos", "Beef", 60.8),
Dish("Lentil Soup", "Lentils", 40.5),
Dish("Pasta Alfredo", "Cream", 60.9),
Dish("Caesar Salad", "Chicken", 50.8),
Dish("Falafel Wrap", "Chickpeas", 50.2)
)
val listAdapter = DishAdapter(this, listItems)
listView.adapter = listAdapter
listView.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position) as Dish
Toast.makeText(this, "you selected $selectedItem.name that costs ${selectedItem.expense} kr.", Toast.LENGTH_LONG).show()
}
}
}

View File

@ -1,4 +1,4 @@
package tech.mercantec.easyeat.ui.home
package tech.mercantec.easyeat.ui.dishes
import android.content.Context
import android.view.LayoutInflater

View File

@ -0,0 +1,57 @@
package tech.mercantec.easyeat.ui.dishes
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import tech.mercantec.easyeat.databinding.FragmentDishesBinding
import tech.mercantec.easyeat.models.Dish
class DishesFragment : Fragment() {
private var _binding: FragmentDishesBinding? = 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 = FragmentDishesBinding.inflate(inflater, container, false)
val root: View = binding.root
context?.let { context ->
binding.dishesList.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position) as Dish
Toast.makeText(context, "you selected $selectedItem.name that costs ${selectedItem.expense} kr.", Toast.LENGTH_LONG).show()
}
val listItems = arrayOf(
Dish("Spaghetti Bolognese", "Beef", 70.5),
Dish("Margherita Pizza", "Cheese", 60.0),
Dish("Chicken Curry", "Chicken", 80.2),
Dish("Vegetable Stir Fry", "Mixed Vegetables", 50.5),
Dish("Sushi", "Fish", 100.0),
Dish("Beef Tacos", "Beef", 60.8),
Dish("Lentil Soup", "Lentils", 40.5),
Dish("Pasta Alfredo", "Cream", 60.9),
Dish("Caesar Salad", "Chicken", 50.8),
Dish("Falafel Wrap", "Chickpeas", 50.2)
)
binding.dishesList.adapter = DishAdapter(context, listItems)
}
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}

View File

@ -1,42 +0,0 @@
package tech.mercantec.easyeat.ui.home
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import tech.mercantec.easyeat.databinding.FragmentHomeBinding
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = 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 {
val homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
val textView: TextView = binding.textHome
homeViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}

View File

@ -1,13 +0,0 @@
package tech.mercantec.easyeat.ui.home
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class HomeViewModel : ViewModel() {
private val _text = MutableLiveData<String>().apply {
value = "This is home Fragment"
}
val text: LiveData<String> = _text
}

View File

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" />
</vector>

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="M280,880L280,514Q229,500 194.5,458Q160,416 160,360L160,80L240,80L240,360L280,360L280,80L360,80L360,360L400,360L400,80L480,80L480,360Q480,416 445.5,458Q411,500 360,514L360,880L280,880ZM680,880L680,560L560,560L560,280Q560,197 618.5,138.5Q677,80 760,80L760,880L680,880Z"/>
</vector>

View File

@ -1,23 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
android:layout_height="match_parent">
<ListView
android:id="@+id/dishesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</ListView>
<!-- Navigation Fragment -->
<fragment
android:id="@+id/nav_host_fragment_activity_main"

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/dishes_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
/>
</FrameLayout>

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -2,8 +2,8 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:id="@+id/navigation_dishes"
android:icon="@drawable/ic_restaurant_black_24px"
android:title="@string/title_your_dishes" />
<item

View File

@ -3,13 +3,13 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_home">
app:startDestination="@+id/navigation_dishes">
<fragment
android:id="@+id/navigation_home"
android:name="tech.mercantec.easyeat.ui.home.HomeFragment"
android:id="@+id/navigation_dishes"
android:name="tech.mercantec.easyeat.ui.dishes.DishesFragment"
android:label="@string/title_your_dishes"
tools:layout="@layout/fragment_home" />
tools:layout="@layout/fragment_dishes" />
<fragment
android:id="@+id/navigation_dashboard"