MainViewAdapter.kt 1.65 KB
Newer Older
Dylan Jones's avatar
Dylan Jones committed
1 2
package srct.whatsopen.util

3
import android.content.Intent
Dylan Jones's avatar
Dylan Jones committed
4 5 6 7 8
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
9
import srct.whatsopen.FacilityDetailActivity
Dylan Jones's avatar
Dylan Jones committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import srct.whatsopen.R
import srct.whatsopen.model.Facility

class MainViewAdapter(var facilities: List<Facility>) : RecyclerView.Adapter<Holder>() {

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

    override fun getItemCount(): Int {
        return facilities.size
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val facility = facilities[position]
        holder.itemView.findViewById<TextView>(R.id.facility_title).text = facility.name
Dylan Jones's avatar
Dylan Jones committed
32 33 34
        val text: String = if (facility.currentSchedule().isOpen24Hours) {
            "Open 24 hours"
        } else {
35
            "${if (facility.isOpen()) "Today: " else "Next Open: ${facility.currentHours()!!.getDay()} "}${facility.currentHours()}"
Dylan Jones's avatar
Dylan Jones committed
36 37
        }
        holder.itemView.findViewById<TextView>(R.id.change_text).text = text
38 39 40
        holder.itemView.setOnClickListener {
            run {
                val intent = Intent(holder.itemView.context, FacilityDetailActivity::class.java)
41
                intent.putExtra("slug", facility.slug)
42 43 44
                holder.itemView.context.startActivity(intent)
            }
        }
Dylan Jones's avatar
Dylan Jones committed
45 46 47 48
    }

}

49
class Holder(view: View) : RecyclerView.ViewHolder(view)