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.write(data.toByteArray())
|
||||||
outputStream.flush()
|
outputStream.flush()
|
||||||
}
|
}
|
||||||
|
Log.i("http", responseCode.toString())
|
||||||
if (responseCode == 401) {
|
if (responseCode == 401) {
|
||||||
if (!autoRefresh || refreshToken == null || !refreshAuthToken(ctx, refreshToken)) {
|
if (!autoRefresh || refreshToken == null || !refreshAuthToken(ctx, refreshToken)) {
|
||||||
val intent = Intent(ctx, LoginActivity::class.java)
|
val intent = Intent(ctx, LoginActivity::class.java)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package tech.mercantec.easyeat.helpers
|
package tech.mercantec.easyeat.helpers
|
||||||
|
|
||||||
|
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
|
||||||
@ -126,3 +127,11 @@ fun createRecipe(ctx: Context, recipe: Recipe) {
|
|||||||
}
|
}
|
||||||
requestJson<CreateRecipeRequest, Boolean>(ctx, "POST", "/api/recipe/create", request)
|
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
|
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.ArrayAdapter
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import tech.mercantec.easyeat.R
|
import tech.mercantec.easyeat.R
|
||||||
import tech.mercantec.easyeat.models.Dish
|
import tech.mercantec.easyeat.models.DishListItem
|
||||||
|
|
||||||
class DishAdapter(context: Context, dishes: Array<Dish>) :
|
class DishAdapter(context: Context, dishes: List<DishListItem>) :
|
||||||
ArrayAdapter<Dish>(context, 0, dishes) {
|
ArrayAdapter<DishListItem>(context, 0, dishes) {
|
||||||
|
|
||||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||||
val dish = getItem(position)
|
val dish = getItem(position)
|
||||||
val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.dish_list_item, parent, false)
|
val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.dish_list_item, parent, false)
|
||||||
|
|
||||||
val nameTextView = view.findViewById<TextView>(R.id.dishName)
|
val nameTextView = view.findViewById<TextView>(R.id.dishName)
|
||||||
val ingredientTextView = view.findViewById<TextView>(R.id.mainIngredient)
|
val descriptionTextView = view.findViewById<TextView>(R.id.descriptionTextView)
|
||||||
val expenseTextView = view.findViewById<TextView>(R.id.expense)
|
|
||||||
|
|
||||||
nameTextView.text = "Name: ${dish?.name}"
|
nameTextView.text = "Name: ${dish?.name}"
|
||||||
ingredientTextView.text = "Main Ingredient: ${dish?.mainIngredient}"
|
descriptionTextView.text = "Description: ${dish?.description}"
|
||||||
expenseTextView.text = "Expense: ${dish?.expense} kr"
|
|
||||||
|
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,12 @@ import android.view.ViewGroup
|
|||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import tech.mercantec.easyeat.R
|
import tech.mercantec.easyeat.R
|
||||||
import tech.mercantec.easyeat.databinding.FragmentDishesBinding
|
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() {
|
class DishesFragment : Fragment() {
|
||||||
|
|
||||||
@ -28,6 +31,21 @@ 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
|
||||||
|
|
||||||
|
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 {
|
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)
|
||||||
|
|
||||||
@ -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
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,12 +12,7 @@
|
|||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/mainIngredient"
|
android:id="@+id/descriptionTextView"
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/expense"
|
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user