Bimba.git

ref: 2f14b9ea523c41743d6e0ed0da2d2f3cba903e26

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
 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package ml.adamsprogs.bimba.departures

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.api.Departure
import ml.adamsprogs.bimba.api.Vehicle
import ml.adamsprogs.bimba.dpToPixelI
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
		notifyDataSetChanged()
	}
}

class ModalBottomSheet(private val departure: Departure) : BottomSheetDialogFragment() {
	companion object {
		const val TAG = "ModalBottomSheet"
	}

	override fun onCreateView(
		inflater: LayoutInflater,
		container: ViewGroup?,
		savedInstanceState: Bundle?
	): View {
		val content = inflater.inflate(R.layout.departure_bottom_sheet, container, false)

		var timeText = "at ${departure.time.Hour.toString().padStart(2, '0')}:${
			departure.time.Minute.toString().padStart(2, '0')
		}"
		if (departure.isRealtime) {
			timeText += ":${departure.time.Second.toString().padStart(2, '0')}"
		}
		content.apply {
			findViewById<TextView>(R.id.time).text = timeText

			findViewById<ImageView>(R.id.rt_icon).apply {
				visibility = if (departure.isRealtime) {
					View.VISIBLE
				} else {
					View.GONE
				}
			}
			findViewById<ImageView>(R.id.wheelchair_icon).apply {
				visibility = if (departure.vehicle.let {
						it.getCapability(Vehicle.Capability.LOW_FLOOR) || it.getCapability(Vehicle.Capability.LOW_ENTRY) || it.getCapability(
							Vehicle.Capability.RAMP
						)
					}) {
					View.VISIBLE
				} else {
					View.GONE
				}
			}

			findViewById<TextView>(R.id.line).apply {
				contentDescription = "${departure.line.name} towards ${departure.headsign}"
				text = "${departure.line.name} » ${departure.headsign}"
			}

			findViewById<Button>(R.id.map_button).apply {
				setOnClickListener {
					// todo show on map
					// todo(szczanieckiej) vehicleID
				}
			}
			findViewById<TextView>(R.id.boarding_text).apply {
				text = if (departure.boarding.and(0b1010u) != (0b0).toUByte()) {
					"on demand"
				} else if (departure.boarding.and(0b0101u) != (0b0).toUByte()) {
					"no boarding"
				} else {
					"can board"
				}
			}
			findViewById<TextView>(R.id.speed_text).apply {
				// todo units
				val speed = departure.vehicle.Speed * 1.703
				text = "%.3f Vl".format(speed)
			}
			findViewById<TextView>(R.id.congestion_text).apply {
				text = when (departure.vehicle.CongestionLevel.toUInt()) {
					0u -> "unknown"
					1u -> "smooth traffic"
					2u -> "stop and go"
					3u -> "congestion"
					4u -> "severe jams"
					else -> TODO("throw invalid congestion")
				}
			}
			findViewById<TextView>(R.id.occupancy_text).apply {
				text = when (departure.vehicle.OccupancyStatus.toUInt()) {
					0u -> "unknown"
					1u -> "empty"
					2u -> "many seats"
					3u -> "few seats"
					4u -> "standing only"
					5u -> "crowded"
					6u -> "full"
					7u -> "won’t accept passengers"  // todo shorten
					else -> TODO("throw invalid congestion")
				}
			}

			findViewById<ImageView>(R.id.ac).visibility =
				if (departure.vehicle.getCapability(Vehicle.Capability.AC)) {
					View.VISIBLE
				} else {
					View.GONE
				}
			findViewById<ImageView>(R.id.bike).visibility =
				if (departure.vehicle.getCapability(Vehicle.Capability.BIKE)) {
					View.VISIBLE
				} else {
					View.GONE
				}
			findViewById<ImageView>(R.id.voice).visibility =
				if (departure.vehicle.getCapability(Vehicle.Capability.VOICE)) {
					View.VISIBLE
				} else {
					View.GONE
				}
			findViewById<ImageView>(R.id.ticket).visibility =
				if (departure.vehicle.let {
						it.getCapability(Vehicle.Capability.TICKET_DRIVER) || it.getCapability(Vehicle.Capability.TICKET_MACHINE)
					}) {
					View.VISIBLE
				} else {
					View.GONE
				}
			findViewById<ImageView>(R.id.usb).visibility =
				if (departure.vehicle.getCapability(Vehicle.Capability.USB_CHARGING)) {
					View.VISIBLE
				} else {
					View.GONE
				}
		}

		(dialog as BottomSheetDialog).behavior.peekHeight = dpToPixelI(150f)

		return content
	}
}