ref: 6bc2a8f60649a48d070b11c662d228322388bf18
app/src/main/java/ml/adamsprogs/bimba/departures/Departures.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
package ml.adamsprogs.bimba.departures import android.annotation.SuppressLint import android.content.Context import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import ml.adamsprogs.bimba.R import ml.adamsprogs.bimba.api.Departure import java.util.* class BimbaDepartureViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val root: View = itemView.findViewById(R.id.departure) val lineIcon: ImageView = itemView.findViewById(R.id.line_icon) val departureTime: TextView = itemView.findViewById(R.id.departure_time) val lineName: TextView = itemView.findViewById(R.id.departure_line) val headsign: TextView = itemView.findViewById(R.id.departure_headsign) companion object { @SuppressLint("SetTextI18n") fun bind( departure: Departure, holder: BimbaDepartureViewHolder?, context: Context?, onClickListener: (Departure) -> Unit ) { holder?.root?.setOnClickListener { onClickListener(departure) } holder?.lineIcon?.setImageBitmap(departure.line.icon(context!!)) holder?.lineName?.text = departure.line.name holder?.headsign?.text = "ยป ${departure.headsign}" // todo >> is not a11y val now = Calendar.getInstance() val departureTime = Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, departure.time.Hour.toInt()) set(Calendar.MINUTE, departure.time.Minute.toInt()) set(Calendar.SECOND, departure.time.Second.toInt()) // todo zone roll(Calendar.DAY_OF_MONTH, departure.time.DayOffset.toInt()) } var duration = departureTime.timeInMillis - now.timeInMillis val hours = duration / (60 * 60 * 1000) duration %= (60 * 60 * 1000) val minutes = duration / (60 * 1000) duration %= (60 * 1000) holder?.departureTime?.text = when (departure.status.toInt()) { // todo(i18n) plurals 0 -> { "in " + if (hours > 0) { "$hours h " } else { "" } + "$minutes min" } 1 -> "momentarily" 2 -> "now" 3 -> "departed" else -> "" } } } } class BimbaDeparturesAdapter( private val inflater: LayoutInflater, private val context: Context?, private var departures: List<Departure>, private val onClickListener: ((Departure) -> Unit) ) : RecyclerView.Adapter<BimbaDepartureViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaDepartureViewHolder { val rowView = inflater.inflate(R.layout.departure, parent, false) return BimbaDepartureViewHolder(rowView) } override fun onBindViewHolder(holder: BimbaDepartureViewHolder, position: Int) { BimbaDepartureViewHolder.bind(departures[position], holder, context, onClickListener) } override fun getItemCount(): Int = departures.size fun update(items: List<Departure>) { departures = items Log.v("Departures", departures.toString()) notifyDataSetChanged() } } |