ref: 54d258e6b9950dad48f62d2e57046a3cbcecddce
app/src/main/java/xyz/apiote/bimba/czwek/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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.departures import android.annotation.SuppressLint import android.content.Context import android.content.DialogInterface import android.content.res.Configuration.UI_MODE_NIGHT_MASK import android.content.res.Configuration.UI_MODE_NIGHT_UNDEFINED import android.content.res.Configuration.UI_MODE_NIGHT_YES 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.LinearLayout import android.widget.TextView import androidx.appcompat.widget.TooltipCompat import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.textview.MaterialTextView import org.osmdroid.tileprovider.tilesource.TileSourceFactory import org.osmdroid.util.GeoPoint import org.osmdroid.views.CustomZoomButtonsController import org.osmdroid.views.MapView import org.osmdroid.views.overlay.Marker import org.osmdroid.views.overlay.TilesOverlay import org.osmdroid.views.overlay.gestures.RotationGestureOverlay import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.dpToPixelI import xyz.apiote.bimba.czwek.repo.Alert import xyz.apiote.bimba.czwek.repo.CongestionLevel import xyz.apiote.bimba.czwek.repo.Departure import xyz.apiote.bimba.czwek.repo.DepartureItem import xyz.apiote.bimba.czwek.repo.OccupancyStatus import xyz.apiote.bimba.czwek.repo.Vehicle import xyz.apiote.bimba.czwek.units.UnitSystem import java.time.ZoneId import java.time.ZonedDateTime class BimbaDepartureViewHolder(itemView: View) : 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 { fun bind( departure: Departure, holder: BimbaDepartureViewHolder?, context: Context?, showAsTime: Boolean, onClickListener: (Departure) -> Unit ) { holder?.root?.setOnClickListener { onClickListener(departure) } holder?.lineIcon?.setImageDrawable(departure.vehicle.Line.icon(context!!)) holder?.lineIcon?.contentDescription = departure.vehicle.Line.kind.name holder?.lineName?.text = departure.vehicle.Line.name holder?.headsign?.text = context?.getString(R.string.departure_headsign, departure.vehicle.Headsign) holder?.headsign?.contentDescription = context?.getString( R.string.departure_headsign_content_description, departure.vehicle.Headsign ) holder?.departureTime?.text = departure.statusText(context, showAsTime) } } } class BimbaAlertViewHolder(itemView: View) : ViewHolder(itemView) { val root: View = itemView.findViewById(R.id.alerts) val text: TextView = itemView.findViewById(R.id.alerts_text) val moreButton: Button = itemView.findViewById(R.id.more_button) companion object { fun bind( alerts: List<Alert>, holder: BimbaAlertViewHolder?, context: Context? ) { val alertDescriptions = alerts.map { it.description }.filter { it != "" } .joinToString(separator = "\n") holder?.moreButton?.setOnClickListener { MaterialAlertDialogBuilder(context!!) .setTitle("Alerts") .setPositiveButton(R.string.ok) { _, _ -> } .setMessage(alertDescriptions) .show() } holder?.moreButton?.visibility = if (alertDescriptions == "") View.GONE else View.VISIBLE holder?.text?.text = alerts.map { it.header.ifEmpty { context!!.getString(R.string.alert_header) } }.toSet().joinToString(separator = "\n") } } } class BimbaDeparturesAdapter( private val inflater: LayoutInflater, private val context: Context?, private var items: List<DepartureItem>, private val onClickListener: ((Departure) -> Unit) ) : RecyclerView.Adapter<ViewHolder>() { var lastUpdate: ZonedDateTime = ZonedDateTime.of(0, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()) private set private var showAsTime: Boolean = false inner class DiffUtilCallback( private val oldDepartures: List<DepartureItem>, private val newDepartures: List<DepartureItem>, private val showAsTimeChanged: Boolean, ) : DiffUtil.Callback() { override fun getOldListSize() = oldDepartures.size override fun getNewListSize() = newDepartures.size override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) = (oldDepartures[oldItemPosition].departure?.ID ?: "alert") == (newDepartures[newItemPosition].departure?.ID ?: "alert") override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldDeparture = oldDepartures[oldItemPosition] val newDeparture = newDepartures[newItemPosition] return if (oldDeparture.departure != null && newDeparture.departure != null) { oldDeparture.departure.vehicle.Line == newDeparture.departure.vehicle.Line && oldDeparture.departure.vehicle.Headsign == newDeparture.departure.vehicle.Headsign && oldDeparture.departure.statusText( context, false, lastUpdate ) == newDeparture.departure.statusText(context, false) && !showAsTimeChanged } else if (oldDeparture.alert.isNotEmpty() && newDeparture.alert.isEmpty()) { oldDeparture.alert == newDeparture.alert } else { false } } } private var departuresPositions: MutableMap<String, Int> = HashMap() init { items.forEachIndexed { i, departure -> departuresPositions[departure.departure?.ID ?: "alert"] = i } } override fun getItemViewType(position: Int): Int { return if (items[position].departure != null) { 0 } else { 1 } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { if (viewType == 0) { val rowView = inflater.inflate(R.layout.departure, parent, false) return BimbaDepartureViewHolder(rowView) } else { val rowView = inflater.inflate(R.layout.alert, parent, false) return BimbaAlertViewHolder(rowView) } } override fun onBindViewHolder(holder: ViewHolder, position: Int) { if (holder is BimbaDepartureViewHolder) { BimbaDepartureViewHolder.bind( items[position].departure!!, holder, context, showAsTime, onClickListener ) } else { BimbaAlertViewHolder.bind(items[position].alert, holder as BimbaAlertViewHolder, context) } } override fun getItemCount(): Int = items.size fun get(id: String): DepartureItem? { val position = departuresPositions[id] return if (position == null) { null } else { items[position] } } fun update( departures: List<DepartureItem>, showAsTime: Boolean, areNewObserved: Boolean = false, leaveAlert: Boolean = false ) { val newDepartures = if (leaveAlert && items.getOrNull(0)?.alert?.isNotEmpty() == true) { listOf(items[0]) + departures } else { departures } val newPositions: MutableMap<String, Int> = HashMap() newDepartures.forEachIndexed { i, departure -> newPositions[departure.departure?.ID ?: "alert"] = i } val diff = DiffUtil.calculateDiff( DiffUtilCallback( this.items, newDepartures, this.showAsTime != showAsTime ) ) this.showAsTime = showAsTime this.items = newDepartures departuresPositions = newPositions if (areNewObserved) { lastUpdate = ZonedDateTime.now() } diff.dispatchUpdatesTo(this) } fun refreshItems() { update(this.items, showAsTime) } } class DepartureBottomSheet(private var departure: Departure) : BottomSheetDialogFragment() { companion object { const val TAG = "DepartureBottomSheet" } private var cancelCallback: (() -> Unit)? = null fun setOnCancel(callback: () -> Unit) { cancelCallback = callback } override fun onCancel(dialog: DialogInterface) { super.onCancel(dialog) cancelCallback?.let { it() } } fun departureID(): String { return departure.ID } fun update(departure: Departure) { this.departure = departure view?.let { context?.let { ctx -> setContent(it, ctx, true) } } } private fun setContent(view: View, ctx: Context, updating: Boolean = false) { view.apply { findViewById<TextView>(R.id.time).text = departure.timeString(ctx) 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 = getString( R.string.vehicle_headsign_content_description, departure.vehicle.Line.name, departure.vehicle.Headsign ) text = getString( R.string.vehicle_headsign, departure.vehicle.Line.name, departure.vehicle.Headsign ) } findViewById<TextView>(R.id.boarding_text).text = departure.boardingText(ctx) UnitSystem.getSelected(requireContext()).let { us -> findViewById<TextView>(R.id.speed_text).apply { text = us.toString(context, us.speedUnit(departure.vehicle.Speed)) contentDescription = us.speedUnit(departure.vehicle.Speed).contentDescription(requireContext(), us.base) } } findViewById<LinearLayout>(R.id.congestion).visibility = if (departure.vehicle.congestionLevel == CongestionLevel.UNKNOWN) View.GONE else View.VISIBLE findViewById<TextView>(R.id.congestion_text).text = departure.vehicle.congestion(ctx) findViewById<LinearLayout>(R.id.occupancy).visibility = if (departure.vehicle.occupancyStatus == OccupancyStatus.UNKNOWN) View.GONE else View.VISIBLE findViewById<TextView>(R.id.occupancy_text).text = departure.vehicle.occupancy(ctx) findViewById<ImageView>(R.id.ac).let { TooltipCompat.setTooltipText( it, getString(R.string.air_condition_content_description) ) it.visibility = if (departure.vehicle.getCapability(Vehicle.Capability.AC)) View.VISIBLE else View.GONE } findViewById<ImageView>(R.id.bike).let { TooltipCompat.setTooltipText( it, getString(R.string.bicycles_allowed_content_description) ) it.visibility = if (departure.vehicle.getCapability(Vehicle.Capability.BIKE)) { View.VISIBLE } else { View.GONE } } findViewById<ImageView>(R.id.voice).let { TooltipCompat.setTooltipText( it, getString(R.string.voice_announcements_content_description) ) it.visibility = if (departure.vehicle.getCapability(Vehicle.Capability.VOICE)) { View.VISIBLE } else { View.GONE } } findViewById<ImageView>(R.id.ticket).let { ticketImage -> TooltipCompat.setTooltipText( ticketImage, getString(R.string.tickets_sold_content_description) ) ticketImage.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).let { TooltipCompat.setTooltipText( it, getString(R.string.usb_charging_content_description) ) it.visibility = if (departure.vehicle.getCapability(Vehicle.Capability.USB_CHARGING)) { View.VISIBLE } else { View.GONE } } if (departure.alerts.isNotEmpty()) { findViewById<MaterialTextView>(R.id.alerts_text).text = departure.alerts.map { it.header.ifEmpty { getString(R.string.alert_header) } }.toSet().joinToString(separator = "\n") findViewById<LinearLayout>(R.id.alerts).apply { visibility = View.VISIBLE setOnClickListener { MaterialAlertDialogBuilder(context) .setTitle("Alerts") .setPositiveButton(R.string.ok) { _, _ -> } .setMessage(departure.alerts.map { it.description }.filter { it != "" } .joinToString(separator = "\n")) .show() } } } findViewById<MapView>(R.id.map).let { map -> if (departure.vehicle.Position.isZero()) { map.visibility = View.GONE return@let } map.controller.apply { GeoPoint( departure.vehicle.location().latitude, departure.vehicle.location().longitude ).let { geoPoint -> if (updating) { animateTo( geoPoint, 19.0f.toDouble(), 3 * 1000 ) } else { setCenter(geoPoint) setZoom(19f.toDouble()) } } } map.overlays.removeAll { marker -> marker is Marker } val marker = Marker(map).apply { position = GeoPoint( departure.vehicle.location().latitude, departure.vehicle.location().longitude ) setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER) icon = context?.let { ctx -> departure.vehicle.icon(ctx, 2f) } setOnClickListener {} } map.overlays.add(marker) map.invalidate() } } } @SuppressLint("ClickableViewAccessibility") override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { val content = inflater.inflate(R.layout.departure_bottom_sheet, container, false) context?.let { ctx -> content.apply { findViewById<MapView>(R.id.map).let { map -> map.setTileSource(TileSourceFactory.MAPNIK) if (((context?.resources?.configuration?.uiMode ?: UI_MODE_NIGHT_UNDEFINED) and UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES ) { map.overlayManager.tilesOverlay.setColorFilter(TilesOverlay.INVERT_COLORS) } map.zoomController.setVisibility(CustomZoomButtonsController.Visibility.NEVER) map.setOnTouchListener { _, _ -> true } map.setMultiTouchControls(true) map.overlays.add(RotationGestureOverlay(map).apply { isEnabled = true }) } setContent(this, ctx) (dialog as BottomSheetDialog).behavior.peekHeight = dpToPixelI(180f) } } return content } } |