ref: c47403765e3fe723e11c9627d8f876a2ba1763f9
app/src/main/java/xyz/apiote/bimba/czwek/journeys/Journeys.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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.journeys import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder import com.google.android.material.card.MaterialCardView import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.repo.Journey import xyz.apiote.bimba.czwek.units.Metre import xyz.apiote.bimba.czwek.units.UnitSystem import java.util.zip.Inflater class JourneysViewHolder(itemView: View) : ViewHolder(itemView) { val root: MaterialCardView = itemView.findViewById(R.id.journey) val startTime: TextView = itemView.findViewById(R.id.start_time) val lines: TextView = itemView.findViewById(R.id.lines) val endTime: TextView = itemView.findViewById(R.id.end_time) val legs: LinearLayout = itemView.findViewById(R.id.legs) companion object { fun bind( holder: JourneysViewHolder, onClickListener: (Journey) -> Unit, journey: Journey, context: Context, inflater: LayoutInflater ) { holder.root.setOnClickListener { if (holder.legs.visibility == View.GONE) { holder.legs.visibility = View.VISIBLE } else { holder.legs.visibility = View.GONE } onClickListener(journey) } holder.startTime.text = context.getString(R.string.time, journey.startTime.hour, journey.startTime.minute) holder.endTime.text = context.getString(R.string.time, journey.endTime.hour, journey.endTime.minute) holder.lines.text = journey.legs.map { it.start.vehicle.Line.name }.filter { it.isNotBlank() }.joinToString() holder.legs.removeAllViews() journey.legs.forEach { val legView = inflater.inflate(R.layout.journey_leg, null, false) val legOrigin = legView.findViewById<TextView>(R.id.leg_origin) if (it.origin.stop.name.isBlank() || it.origin.stop.name == "START") { legOrigin.visibility = View.GONE } else { legOrigin.apply { text = it.origin.stop.name visibility = View.VISIBLE } } val legOriginTime = legView.findViewById<TextView>(R.id.leg_origin_time) legOriginTime.text = context.getString( R.string.time, it.start.departureTime!!.Hour.toInt(), it.start.departureTime.Minute.toInt() ) val legModeImage = legView.findViewById<ImageView>(R.id.leg_mode_image) legModeImage.setImageDrawable(it.start.vehicle.Line.icon(context)) // TODO legModeImage.contentDescription = translate "leg mode: $mode" val legLine = legView.findViewById<TextView>(R.id.leg_line) legLine.apply { if (it.start.vehicle.Line.name.isBlank()) { visibility = View.GONE } else { visibility = View.VISIBLE text = it.start.vehicle.Line.name } } val distance = legView.findViewById<TextView>(R.id.leg_distance) distance.text = if (it.start.vehicle.Line.name.isBlank() && it.distance != null) { val us = UnitSystem.getSelected(context) us.toString(context, it.distance) } else { context.resources.getQuantityString(R.plurals.number_stops, it.intermediateStops.size+1, it.intermediateStops.size+1) } val legDestination = legView.findViewById<TextView>(R.id.leg_destination) if (it.destination.stop.name.isBlank() || it.destination.stop.name == "END") { legDestination.visibility = View.GONE } else { legDestination.apply { text = it.destination.stop.name visibility = View.VISIBLE } } val legDestinationTime = legView.findViewById<TextView>(R.id.leg_destination_time) legDestinationTime.text = context.getString(R.string.time, it.end.arrivalTime!!.Hour.toInt(), it.end.arrivalTime.Minute.toInt()) holder.legs.addView(legView) } } } } class JourneysAdapter( private val inflater: LayoutInflater, private val context: Context, private var items: List<Journey>, private val onClickListener: ((Journey) -> Unit), ) : RecyclerView.Adapter<JourneysViewHolder>() { override fun onCreateViewHolder( parent: ViewGroup, viewType: Int ): JourneysViewHolder { val rowView = inflater.inflate(R.layout.journey, parent, false) return JourneysViewHolder(rowView) } override fun onBindViewHolder( holder: JourneysViewHolder, position: Int ) { JourneysViewHolder.bind(holder, onClickListener, items[position], context, inflater) } override fun getItemCount(): Int = items.size } |