create and diushlist works

This commit is contained in:
LilleBRG 2025-05-09 17:03:50 +02:00
parent 5c1a569906
commit b882bc09a7
6 changed files with 59 additions and 64 deletions

View File

@ -4,6 +4,8 @@ import android.content.ClipDescription
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import tech.mercantec.easyeat.models.Direction
import tech.mercantec.easyeat.models.Ingredient
import tech.mercantec.easyeat.models.Recipe import tech.mercantec.easyeat.models.Recipe
@Serializable @Serializable
data class LoginRequest(val emailUsr: String, val password: String) data class LoginRequest(val emailUsr: String, val password: String)
@ -112,20 +114,11 @@ fun changePassword(ctx: Context, oldPassword: String, newPassword: String) {
} }
@Serializable @Serializable
data class CreateRecipeRequest(val recipe: Recipe) data class CreateRecipeRequest(val name: String, val description: String, val directions: List<Direction>, val ingredients: List<Ingredient>)
fun createRecipe(ctx: Context, recipe: Recipe) { fun createRecipe(ctx: Context, recipe: Recipe) {
val request = CreateRecipeRequest(recipe) val request = CreateRecipeRequest(recipe.name, recipe.description, recipe.directions, recipe.ingredients)
Log.i("recipe name:", recipe.name)
Log.i("recipe name:", recipe.description)
for (x in recipe.directions) {
Log.i("recipe name:", x.instructions)
}
for(x in recipe.ingredients){
Log.i("recipe name:", x.name)
Log.i("recipe name:", x.amount.toString())
Log.i("recipe name:", x.unit.toString())
}
requestJson<CreateRecipeRequest, Boolean>(ctx, "POST", "/api/recipe/create", request) requestJson<CreateRecipeRequest, Boolean>(ctx, "POST", "/api/recipe/create", request)
} }
@ -133,6 +126,5 @@ fun createRecipe(ctx: Context, recipe: Recipe) {
data class RecipeResponse(val id: Int, val name: String, val description: String) data class RecipeResponse(val id: Int, val name: String, val description: String)
fun getRecipies(ctx: Context): List<RecipeResponse> { fun getRecipies(ctx: Context): List<RecipeResponse> {
Log.i("ello", "yes")
return requestJson<Unit, List<RecipeResponse>>(ctx, "GET", "/api/Recipe/getall", null) return requestJson<Unit, List<RecipeResponse>>(ctx, "GET", "/api/Recipe/getall", null)
} }

View File

@ -1,3 +1,3 @@
package tech.mercantec.easyeat.models package tech.mercantec.easyeat.models
public data class DishListItem(val name: String, val description: String) public data class DishListItem(val id: Int,val name: String, val description: String)

View File

@ -1,5 +1,6 @@
package tech.mercantec.easyeat.ui.dishes package tech.mercantec.easyeat.ui.dishes
import android.app.Activity
import android.app.ProgressDialog import android.app.ProgressDialog
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
@ -83,15 +84,10 @@ class CreateDishActivity : AppCompatActivity() {
Toast.makeText(this, "Password changed successfully", Toast.LENGTH_LONG).show() Toast.makeText(this, "Password changed successfully", Toast.LENGTH_LONG).show()
} }
setResult(Activity.RESULT_OK)
finish() finish()
} }
} }
} }
private fun addIngredientRow() { private fun addIngredientRow() {

View File

@ -1,5 +1,6 @@
package tech.mercantec.easyeat.ui.dishes package tech.mercantec.easyeat.ui.dishes
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -8,6 +9,8 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -24,9 +27,7 @@ import kotlin.concurrent.thread
class DishesFragment : Fragment() { class DishesFragment : Fragment() {
private var _binding: FragmentDishesBinding? = null private var _binding: FragmentDishesBinding? = null
private lateinit var createDishLauncher: ActivityResultLauncher<Intent>
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!! private val binding get() = _binding!!
override fun onCreateView( override fun onCreateView(
@ -37,41 +38,7 @@ 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
loadRecipes()
thread {
val recipes: List<RecipeResponse>
try {
recipes = getRecipies(requireContext())
} catch (e: ApiRequestException) {
activity?.runOnUiThread {
Toast.makeText(requireContext(), e.message, Toast.LENGTH_LONG).show()
}
return@thread
}
val dishes = recipes.map {
DishListItem(
it.name,
it.description
) // assuming description is the main ingredient and no price
}
}
// lifecycleScope.launch {
// try {
// val recipes = getRecipies(requireContext()) // this is now suspend
// val dishes = recipes.map {
// DishListItem(it.name, it.description) // assuming description is the main ingredient and no price
// }
//
// val adapter = DishAdapter(requireContext(), dishes)
// binding.dishesList.adapter = adapter
// } catch (e: Exception) {
// Log.i("nice", e.message.toString())
// Toast.makeText(requireContext(), "Failed to load recipes: ${e.message}", Toast.LENGTH_LONG).show()
// }
// }
binding.addDish.setOnClickListener { binding.addDish.setOnClickListener {
val dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.create_dish_modal_dialog, null) val dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.create_dish_modal_dialog, null)
@ -85,13 +52,13 @@ class DishesFragment : Fragment() {
dialogView.findViewById<Button>(R.id.createManualBtn).setOnClickListener { dialogView.findViewById<Button>(R.id.createManualBtn).setOnClickListener {
val intent = Intent(requireContext(), CreateDishActivity::class.java) val intent = Intent(requireContext(), CreateDishActivity::class.java)
startActivity(intent) createDishLauncher.launch(intent)
dialog.dismiss() dialog.dismiss()
} }
dialogView.findViewById<Button>(R.id.createAIBtn).setOnClickListener { dialogView.findViewById<Button>(R.id.createAIBtn).setOnClickListener {
val intent = Intent(requireContext(), CreateDishAIActivity::class.java) val intent = Intent(requireContext(), CreateDishAIActivity::class.java)
startActivity(intent) createDishLauncher.launch(intent)
dialog.dismiss() dialog.dismiss()
} }
@ -100,10 +67,40 @@ class DishesFragment : Fragment() {
return root return root
}
fun DishesFragment.loadRecipes() {
thread {
val recipes: List<RecipeResponse>
try {
recipes = getRecipies(requireContext())
} catch (e: ApiRequestException) {
activity?.runOnUiThread {
Toast.makeText(requireContext(), e.message, Toast.LENGTH_LONG).show()
}
return@thread
}
val dishes = recipes.map {
DishListItem(it.id, it.name, it.description)
}
activity?.runOnUiThread {
val adapter = DishAdapter(requireContext(), dishes)
binding.dishesList.adapter = adapter
}
}
} }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
createDishLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
loadRecipes()
}
}
}
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()

View File

@ -8,12 +8,19 @@
android:padding="0dp" android:padding="0dp"
android:gravity="center"> android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="@string/create_dish"
android:textAlignment="center"
android:textSize="30sp"/>
<Button <Button
android:id="@+id/createManualBtn" android:id="@+id/createManualBtn"
android:layout_width="200dp" android:layout_width="200dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" android:layout_weight="1"
android:text="Top Button" /> android:text="@string/manually" />
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"
@ -25,5 +32,5 @@
android:layout_width="200dp" android:layout_width="200dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" android:layout_weight="1"
android:text="Bottom Button" /> android:text="@string/search_for_dishes" />
</LinearLayout> </LinearLayout>

View File

@ -41,6 +41,9 @@
<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 name="empty_shopping_list">Your shopping list is empty</string>
<string name="search_for_dishes">Search For Dishes</string>
<string name="manually">Manually</string>
<string name="create_dish">Create Dish</string>
<string-array name="units"> <string-array name="units">
<item></item> <item></item>
<item>g</item> <item>g</item>