Bimba.git

ref: b6f77aff78086720a391aa7381a404242f02470a

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
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
package ml.adamsprogs.bimba.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 ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.api.Departure
import ml.adamsprogs.bimba.api.Vehicle
import ml.adamsprogs.bimba.dpToPixelI
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 java.util.*
import kotlin.collections.HashMap


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?.setImageBitmap(departure.line.icon(context!!))
			holder?.lineIcon?.contentDescription = departure.line.type.name
			holder?.lineName?.text = departure.line.name
			holder?.headsign?.text = context?.getString(R.string.departure_headsign, departure.headsign)
			holder?.headsign?.contentDescription =
				context?.getString(R.string.departure_headsign_content_description, departure.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 =
			departures.map { // fixme (!!) does szczanieckiej not populate departure.vehicle.(line,headsign)?
				Departure(
					it.ID, it.line, it.headsign, it.time, it.status, it.isRealtime,
					it.stopOrder, Vehicle(
						it.vehicle.ID,
						it.vehicle.Position,
						it.vehicle.Capabilities,
						it.vehicle.Speed,
						it.line,
						it.headsign,
						it.vehicle.CongestionLevel,
						it.vehicle.OccupancyStatus
					),
					it.boarding
				)
			}
		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(items: List<Departure>) {
		val newPositions: MutableMap<String, Int> = HashMap()
		items.forEachIndexed { i, departure ->
			newPositions[departure.ID] = i
		}

		departures = items.map { // fixme (!!)
			Departure(
				it.ID, it.line, it.headsign, it.time, it.status, it.isRealtime,
				it.stopOrder, Vehicle(
					it.vehicle.ID,
					it.vehicle.Position,
					it.vehicle.Capabilities,
					it.vehicle.Speed,
					it.line,
					it.headsign,
					it.vehicle.CongestionLevel,
					it.vehicle.OccupancyStatus
				),
				it.boarding
			)
		}
		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) } }
	}

	private fun setContent(view: View, ctx: Context) {
		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.line.name,
					departure.headsign
				)
				text = getString(R.string.vehicle_headsign, departure.line.name, departure.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 { // todo[3.1] glide to centre, not jump
					setZoom(19.0f.toDouble())
					setCenter(
						GeoPoint(
							departure.vehicle.location().latitude,
							departure.vehicle.location().longitude
						)
					)
				}

				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
	}
}