code towards create manual and getallrecipies made but not working
This commit is contained in:
parent
80e52cd22b
commit
3b01992eed
@ -37,7 +37,7 @@ fun request(ctx: Context, method: String, path: String, data: String?, autoRefre
|
||||
outputStream.write(data.toByteArray())
|
||||
outputStream.flush()
|
||||
}
|
||||
|
||||
Log.i("http", responseCode.toString())
|
||||
if (responseCode == 401) {
|
||||
if (!autoRefresh || refreshToken == null || !refreshAuthToken(ctx, refreshToken)) {
|
||||
val intent = Intent(ctx, LoginActivity::class.java)
|
||||
|
@ -1,5 +1,6 @@
|
||||
package tech.mercantec.easyeat.helpers
|
||||
|
||||
import android.content.ClipDescription
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import kotlinx.serialization.Serializable
|
||||
@ -126,3 +127,11 @@ fun createRecipe(ctx: Context, recipe: Recipe) {
|
||||
}
|
||||
requestJson<CreateRecipeRequest, Boolean>(ctx, "POST", "/api/recipe/create", request)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
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 Dish(val name: String, val mainIngredient: String, val expense: Double)
|
||||
public data class DishListItem(val name: String, val description: String)
|
||||
|
@ -7,22 +7,20 @@ import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.TextView
|
||||
import tech.mercantec.easyeat.R
|
||||
import tech.mercantec.easyeat.models.Dish
|
||||
import tech.mercantec.easyeat.models.DishListItem
|
||||
|
||||
class DishAdapter(context: Context, dishes: Array<Dish>) :
|
||||
ArrayAdapter<Dish>(context, 0, dishes) {
|
||||
class DishAdapter(context: Context, dishes: List<DishListItem>) :
|
||||
ArrayAdapter<DishListItem>(context, 0, dishes) {
|
||||
|
||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||
val dish = getItem(position)
|
||||
val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.dish_list_item, parent, false)
|
||||
|
||||
val nameTextView = view.findViewById<TextView>(R.id.dishName)
|
||||
val ingredientTextView = view.findViewById<TextView>(R.id.mainIngredient)
|
||||
val expenseTextView = view.findViewById<TextView>(R.id.expense)
|
||||
val descriptionTextView = view.findViewById<TextView>(R.id.descriptionTextView)
|
||||
|
||||
nameTextView.text = "Name: ${dish?.name}"
|
||||
ingredientTextView.text = "Main Ingredient: ${dish?.mainIngredient}"
|
||||
expenseTextView.text = "Expense: ${dish?.expense} kr"
|
||||
descriptionTextView.text = "Description: ${dish?.description}"
|
||||
|
||||
return view
|
||||
}
|
||||
|
@ -8,9 +8,12 @@ import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.launch
|
||||
import tech.mercantec.easyeat.R
|
||||
import tech.mercantec.easyeat.databinding.FragmentDishesBinding
|
||||
import tech.mercantec.easyeat.models.Dish
|
||||
import tech.mercantec.easyeat.helpers.getRecipies
|
||||
import tech.mercantec.easyeat.models.DishListItem
|
||||
|
||||
class DishesFragment : Fragment() {
|
||||
|
||||
@ -28,6 +31,21 @@ class DishesFragment : Fragment() {
|
||||
_binding = FragmentDishesBinding.inflate(inflater, container, false)
|
||||
val root: View = binding.root
|
||||
|
||||
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) {
|
||||
Toast.makeText(requireContext(), "Failed to load recipes: ${e.message}", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
binding.addDish.setOnClickListener {
|
||||
val dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.create_dish_modal_dialog, null)
|
||||
|
||||
@ -54,29 +72,6 @@ class DishesFragment : Fragment() {
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -12,12 +12,7 @@
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/mainIngredient"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/expense"
|
||||
android:id="@+id/descriptionTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user