create and diushlist works
This commit is contained in:
parent
5c1a569906
commit
b882bc09a7
@ -4,6 +4,8 @@ import android.content.ClipDescription
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import kotlinx.serialization.Serializable
|
||||
import tech.mercantec.easyeat.models.Direction
|
||||
import tech.mercantec.easyeat.models.Ingredient
|
||||
import tech.mercantec.easyeat.models.Recipe
|
||||
@Serializable
|
||||
data class LoginRequest(val emailUsr: String, val password: String)
|
||||
@ -112,20 +114,11 @@ fun changePassword(ctx: Context, oldPassword: String, newPassword: String) {
|
||||
}
|
||||
|
||||
@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) {
|
||||
val request = CreateRecipeRequest(recipe)
|
||||
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())
|
||||
}
|
||||
val request = CreateRecipeRequest(recipe.name, recipe.description, recipe.directions, recipe.ingredients)
|
||||
|
||||
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)
|
||||
|
||||
fun getRecipies(ctx: Context): List<RecipeResponse> {
|
||||
Log.i("ello", "yes")
|
||||
return requestJson<Unit, List<RecipeResponse>>(ctx, "GET", "/api/Recipe/getall", null)
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
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)
|
||||
|
@ -1,5 +1,6 @@
|
||||
package tech.mercantec.easyeat.ui.dishes
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.ProgressDialog
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
@ -83,15 +84,10 @@ class CreateDishActivity : AppCompatActivity() {
|
||||
Toast.makeText(this, "Password changed successfully", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
setResult(Activity.RESULT_OK)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private fun addIngredientRow() {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package tech.mercantec.easyeat.ui.dishes
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
@ -8,6 +9,8 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.launch
|
||||
@ -24,9 +27,7 @@ import kotlin.concurrent.thread
|
||||
class DishesFragment : Fragment() {
|
||||
|
||||
private var _binding: FragmentDishesBinding? = null
|
||||
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private lateinit var createDishLauncher: ActivityResultLauncher<Intent>
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreateView(
|
||||
@ -37,41 +38,7 @@ class DishesFragment : Fragment() {
|
||||
_binding = FragmentDishesBinding.inflate(inflater, container, false)
|
||||
val root: View = binding.root
|
||||
|
||||
|
||||
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()
|
||||
// }
|
||||
// }
|
||||
|
||||
loadRecipes()
|
||||
|
||||
binding.addDish.setOnClickListener {
|
||||
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 {
|
||||
val intent = Intent(requireContext(), CreateDishActivity::class.java)
|
||||
startActivity(intent)
|
||||
createDishLauncher.launch(intent)
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
dialogView.findViewById<Button>(R.id.createAIBtn).setOnClickListener {
|
||||
val intent = Intent(requireContext(), CreateDishAIActivity::class.java)
|
||||
startActivity(intent)
|
||||
createDishLauncher.launch(intent)
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
@ -100,13 +67,43 @@ class DishesFragment : Fragment() {
|
||||
|
||||
|
||||
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() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,12 +8,19 @@
|
||||
android:padding="0dp"
|
||||
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
|
||||
android:id="@+id/createManualBtn"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:text="Top Button" />
|
||||
android:text="@string/manually" />
|
||||
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
@ -25,5 +32,5 @@
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:text="Bottom Button" />
|
||||
android:text="@string/search_for_dishes" />
|
||||
</LinearLayout>
|
||||
|
@ -41,6 +41,9 @@
|
||||
<string name="checked_desc">Checked</string>
|
||||
<string name="delete_label">Delete</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">
|
||||
<item></item>
|
||||
<item>g</item>
|
||||
|
Loading…
Reference in New Issue
Block a user