Bimba.git

ref: 73cee3f10cf4b89f2e742db807d8f12bf4484548

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
package xyz.apiote.bimba.czwek.departures

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.content.res.Configuration.*
import android.os.Bundle
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 com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
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.Departure
import xyz.apiote.bimba.czwek.repo.Vehicle
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 {
		fun bind(
			departure: Departure,
			holder: BimbaDepartureViewHolder?,
			context: Context?,
			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)
		}
	}
}

class BimbaDeparturesAdapter(
	private val inflater: LayoutInflater,
	private val context: Context?,
	private var departures: List<Departure>,
	private val onClickListener: ((Departure) -> Unit)
) :
	RecyclerView.Adapter<BimbaDepartureViewHolder>() {

	private var departuresPositions: MutableMap<String, Int> = HashMap()

	init {
		departures.forEachIndexed { i, departure ->
			departuresPositions[departure.ID] = i
		}
	}

	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 get(ID: String): Departure? {
		val position = departuresPositions[ID]
		return if (position == null) {
			null
		} else {
			departures[position]
		}
	}

	@SuppressLint("NotifyDataSetChanged") // todo [3.1] DiffUtil
	fun update(departures: List<Departure>) {
		val newPositions: MutableMap<String, Int> = HashMap()
		departures.forEachIndexed { i, departure ->
			newPositions[departure.ID] = i
		}

		this.departures = departures
		departuresPositions = newPositions
		notifyDataSetChanged()
	}
}

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
		this.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)
			// todo units -- [3.1] settings or system-based
			findViewById<TextView>(R.id.speed_text).text =
				getString(R.string.speed_in_km_per_h, departure.vehicle.Speed * 3.6)
			findViewById<TextView>(R.id.congestion_text).text = departure.vehicle.congestion(ctx)
			findViewById<TextView>(R.id.occupancy_text).text = departure.vehicle.occupancy(ctx)

			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
				}
			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 {
						if (updating) {
							animateTo(
								it, 19.0f.toDouble(), 3 * 1000
							)
						} else {
							setCenter(it)
							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
	}
}