https://kotlinlang.org logo
#android
Title
# android
s

Sangeeth Amirthanathan

08/22/2022, 2:50 AM
Copy code
package com.zasa.newcaloriy

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

private const val BASE_URL = "<https://api.spoonacular.com/>"
private const val TAG = "MainActivity"
private const val API_KEY = "5daeba7d6f3045b3affa64e67a2329c3"

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val meals = mutableListOf<Meal>()
        val mealAdapter = MealsAdapter(this, meals)

        val retrofit =
            Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create())
                .build()

        val spoonacularService = retrofit.create(SpoonacularService::class.java)
        val targetCalories = intent.getStringExtra("targetCalories")


        spoonacularService.getMeals("$API_KEY", "day", "$targetCalories")
            .enqueue(object : Callback<SpoonacularData> {
                override fun onResponse(
                    call: Call<SpoonacularData>,
                    response: Response<SpoonacularData>
                ) {
                    val body = response.body()
                    if (body == null) {
                        Log.w(TAG, "failed to fetch data..")
                        return
                    }
                    Log.i(TAG, "$response")
                    rvMeals.apply {
                        meals.addAll(body.meals)
                        layoutManager = LinearLayoutManager(this@MainActivity)
                        adapter = mealAdapter
                        mealAdapter.notifyDataSetChanged()
                    }
                }

                override fun onFailure(call: Call<SpoonacularData>, t: Throwable) {
                    Log.i(TAG, "$t")
                }

            })

    }
}
class MealsAdapter(val context: Context, private val meals : List<Meal>) : RecyclerView.Adapter<MealsAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_meal, parent,false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val meal =meals[position]
        holder.bindMeal(meal)
    }

    override fun getItemCount() = meals.size

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        fun bindMeal(meal: Meal) {
            itemView.findViewById<TextView>(R.id.tvMealTitle).text = meal.title
            itemView.findViewById<Button>(R.id.btnGetLink).isClickable = true
            itemView.findViewById<TextView>(R.id.tvReadyInMinutes).text = "Ready in Minutes : ${meal.readyInMinutes}"
            itemView.findViewById<TextView>(R.id.tvServings).text = "Servings can be ${meal.servings} Members"
        }
    }
}
e

ephemient

08/22/2022, 9:04 AM
https://kotlinlang.org/community/slackccugl.html
If the code you are pasting is longer than a few lines, use "Code or text snippet" available from + menu next to message input. Only first few lines will be displayed to all users and people interested in the code can expand your code to look into details.
3 Views