ref: 2919df3a7321d4e904d6ed9cdd1c3c8c32a5ef6c
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 94 95 |
package ml.adamsprogs.bimba.departures 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 lineName: TextView = itemView.findViewById(R.id.line_name) val departureTime: TextView = itemView.findViewById(R.id.departure_time) val headsign: TextView = itemView.findViewById(R.id.departure_headsign) companion object { 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}" 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 days = duration / (24 * 60 * 60 * 1000) duration %= (24 * 60 * 60 * 1000) val hours = duration / (60 * 60 * 1000) duration %= (60 * 60 * 1000) val minutes = duration / (60 * 1000) duration %= (60 * 1000) val seconds = duration / 1000 holder?.departureTime?.text = when (departure.status.toInt()) { 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() } } |