create dish button moved to dishesfragment

This commit is contained in:
LilleBRG 2025-05-01 10:13:52 +02:00
parent a63b98e180
commit 9d9a2975f5
9 changed files with 168 additions and 84 deletions

View File

@ -1,11 +1,15 @@
package tech.mercantec.easyeat package tech.mercantec.easyeat
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import tech.mercantec.easyeat.models.Ingredient
class CreateDishActivity : AppCompatActivity() { class CreateDishActivity : AppCompatActivity() {
@ -25,6 +29,19 @@ class CreateDishActivity : AppCompatActivity() {
addButton.setOnClickListener { addButton.setOnClickListener {
addIngredientRow() addIngredientRow()
} }
val saveButton: Button = findViewById(R.id.saveDishButton)
saveButton.setOnClickListener {
val ingredientList = collectIngredients()
// Debug/log example
for (ingredient in ingredientList) {
Log.d("INGREDIENT", "Name: ${ingredient.Element}, Amount: ${ingredient.Amount}, Unit: ${ingredient.Unit}")
}
// You can now save to DB, send to backend, etc.
}
} }
private fun addIngredientRow() { private fun addIngredientRow() {
@ -39,4 +56,29 @@ class CreateDishActivity : AppCompatActivity() {
ingredientContainer.addView(ingredientRow) ingredientContainer.addView(ingredientRow)
} }
private fun collectIngredients(): List<Ingredient> {
val ingredients = mutableListOf<Ingredient>()
for (i in 0 until ingredientContainer.childCount) {
val ingredientView = ingredientContainer.getChildAt(i)
// Find views inside this ingredient row
val nameEditText = ingredientView.findViewById<EditText>(R.id.ingredientNameEditText)
val amountEditText = ingredientView.findViewById<EditText>(R.id.ingredientAmountEditText)
val spinner = ingredientView.findViewById<Spinner>(R.id.spinner)
val element = nameEditText.text.toString().trim()
val amount = amountEditText.text.toString().trim()
val unit = spinner.selectedItem.toString()
// Optional: Only add non-empty rows
if (element.isNotEmpty() && amount.isNotEmpty()) {
ingredients.add(Ingredient(Element = element, Amount = amount.toInt(), Unit = unit))
}
}
return ingredients
}
} }

View File

@ -36,11 +36,5 @@ class MainActivity : AppCompatActivity() {
setupActionBarWithNavController(navController, appBarConfiguration) setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController) navView.setupWithNavController(navController)
findViewById<FloatingActionButton>(R.id.add_dish).setOnClickListener {
val intent = Intent(this, CreateDishActivity::class.java)
startActivity(intent)
}
} }
} }

View File

@ -0,0 +1,7 @@
package tech.mercantec.easyeat.models
data class Ingredient(
val Amount: Int,
val Unit: String,
val Element: String
)

View File

@ -1,11 +1,13 @@
package tech.mercantec.easyeat.ui.dishes package tech.mercantec.easyeat.ui.dishes
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import tech.mercantec.easyeat.CreateDishActivity
import tech.mercantec.easyeat.databinding.FragmentDishesBinding import tech.mercantec.easyeat.databinding.FragmentDishesBinding
import tech.mercantec.easyeat.models.Dish import tech.mercantec.easyeat.models.Dish
@ -25,6 +27,11 @@ class DishesFragment : Fragment() {
_binding = FragmentDishesBinding.inflate(inflater, container, false) _binding = FragmentDishesBinding.inflate(inflater, container, false)
val root: View = binding.root val root: View = binding.root
binding.addDish.setOnClickListener {
val intent = Intent(requireContext(), CreateDishActivity::class.java)
startActivity(intent)
}
context?.let { context -> context?.let { context ->
binding.dishesList.setOnItemClickListener { parent, view, position, id -> binding.dishesList.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position) as Dish val selectedItem = parent.getItemAtPosition(position) as Dish
@ -47,9 +54,13 @@ class DishesFragment : Fragment() {
binding.dishesList.adapter = DishAdapter(context, listItems) binding.dishesList.adapter = DishAdapter(context, listItems)
} }
return root return root
} }
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
_binding = null _binding = null

View File

@ -27,5 +27,13 @@
android:text="Add Ingredient" android:text="Add Ingredient"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"/> android:layout_marginTop="16dp"/>
<Button
android:id="@+id/saveDishButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_gravity="right"
android:layout_marginTop="16dp"/>
</LinearLayout> </LinearLayout>

View File

@ -1,80 +1,81 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp"> android:padding="8dp">
<!-- Ingredient Field -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/ingredient_label" />
<EditText
android:layout_width="match_parent"
android:layout_height="58dp"
android:hint="Ingredient name" />
<!-- Row: Amount + Measurement -->
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="vertical">
android:gravity="center_vertical"
android:layout_marginTop="8dp">
<!-- Amount field (fixed width) --> <!-- Ingredient Field -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/ingredient_label" />
<EditText
android:id="@+id/ingredientNameEditText"
android:layout_width="match_parent"
android:layout_height="58dp"
android:hint="Ingredient name" />
<!-- Row: Amount + Measurement -->
<LinearLayout <LinearLayout
android:layout_width="174dp" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="8dp">
<TextView <!-- Amount field -->
android:layout_width="wrap_content" <LinearLayout
android:layout_width="174dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/amount_label" /> android:orientation="vertical">
<EditText <TextView
android:layout_width="173dp" android:layout_width="wrap_content"
android:layout_height="48dp" android:layout_height="wrap_content"
android:hint="0" /> android:text="@string/amount_label" />
</LinearLayout>
<!-- Measurement field (fill rest) --> <EditText
<LinearLayout android:id="@+id/ingredientAmountEditText"
android:layout_width="0dp" android:layout_width="173dp"
android:layout_height="wrap_content" android:layout_height="48dp"
android:layout_weight="1" android:hint="0" />
android:orientation="vertical" </LinearLayout>
android:layout_marginStart="8dp">
<TextView <!-- Measurement field -->
android:layout_width="wrap_content" <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/measurement_label" /> android:layout_weight="1"
android:orientation="vertical"
android:layout_marginStart="8dp">
<Spinner <TextView
android:id="@+id/spinner" android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="48dp" /> android:text="@string/measurement_label" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="48dp" />
</LinearLayout>
<!-- Remove Button -->
<Button
android:id="@+id/removeButton"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="-"
android:textSize="20dp"
android:backgroundTint="@color/red" />
</LinearLayout> </LinearLayout>
<!-- Remove Button (fixed width) -->
<Button
android:id="@+id/removeButton"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="-"
android:textSize="20dp"
android:backgroundTint="@color/red"/>
</LinearLayout> </LinearLayout>
</ScrollView>
</LinearLayout>

View File

@ -8,19 +8,6 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"> android:paddingTop="?attr/actionBarSize">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_dish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:contentDescription="@string/add_label"
app:srcCompat="@android:drawable/ic_input_add"
app:backgroundTint="@color/cyan"
app:tint="@android:color/white"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintEnd_toEndOf="parent" />
<fragment <fragment
android:id="@+id/nav_host_fragment_activity_main" android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment" android:name="androidx.navigation.fragment.NavHostFragment"

View File

@ -1,12 +1,26 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout <FrameLayout 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"
android:id="@+id/dishes_fragment_root"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<!-- Your list view -->
<ListView <ListView
android:id="@+id/dishes_list" android:id="@+id/dishes_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="8dp" android:divider="@android:color/darker_gray"
/> android:dividerHeight="1dp" />
<!-- FloatingActionButton -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_dish"
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> </FrameLayout>

View File

@ -0,0 +1,20 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Database": "Data Source=database.sqlite3"
},
"JwtSettings": {
"Issuer": "TemperatureAlarmApi",
"Audience": "Customers",
"Key": ""
}
}