ref: e4d7079147c16b0d67cd39e35f5d943644d24c68
app/src/main/java/xyz/apiote/bimba/czwek/search/Results.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 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.search import android.content.Context import android.content.Intent import android.location.Location import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.departures.DeparturesActivity import xyz.apiote.bimba.czwek.repo.FeedInfo import xyz.apiote.bimba.czwek.repo.Line import xyz.apiote.bimba.czwek.repo.Queryable import xyz.apiote.bimba.czwek.repo.Stop import xyz.apiote.bimba.czwek.repo.StopStub import xyz.apiote.bimba.czwek.settings.feeds.FeedsSettings import xyz.apiote.bimba.czwek.units.Metre import xyz.apiote.bimba.czwek.units.UnitSystem import kotlin.math.abs class BimbaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val root: View = itemView.findViewById(R.id.suggestion) val icon: ImageView = itemView.findViewById(R.id.suggestion_image) val title: TextView = itemView.findViewById(R.id.suggestion_title) val description: TextView = itemView.findViewById(R.id.suggestion_description) val feedName: TextView = itemView.findViewById(R.id.feed_name) val distance: TextView = itemView.findViewById(R.id.distance) val arrow: ImageView = itemView.findViewById(R.id.arrow) companion object { fun bind( queryable: Queryable, holder: BimbaViewHolder?, context: Context?, feeds: Map<String, FeedInfo>?, feedsSettings: FeedsSettings?, onClickListener: (Queryable) -> Unit, position: Location?, heading: Float?, showArrow: Boolean ) { when (queryable) { is Stop -> bindStop( queryable, holder, context, feeds, feedsSettings, position, heading, showArrow ) is Line -> bindLine(queryable, holder, context, feeds, feedsSettings) } holder?.root?.setOnClickListener { onClickListener(queryable) } } fun bind( stopStub: StopStub, holder: BimbaViewHolder?, context: Context?, onClickListener: (StopStub) -> Unit ) { holder?.title?.text = stopStub.name holder?.icon?.apply { setImageDrawable(stopStub.icon(context!!)) contentDescription = context.getString(R.string.stop_content_description) } holder?.description?.text = when { stopStub.zone != "" && stopStub.onDemand -> context?.getString( R.string.stop_stub_on_demand_in_zone, stopStub.zone ) stopStub.zone == "" && stopStub.onDemand -> context?.getString(R.string.stop_stub_on_demand) stopStub.zone != "" && !stopStub.onDemand -> context?.getString( R.string.stop_stub_in_zone, stopStub.zone ) else -> "" } holder?.root?.setOnClickListener { onClickListener(stopStub) } } private fun bindStop( stop: Stop, holder: BimbaViewHolder?, context: Context?, feeds: Map<String, FeedInfo>?, feedsSettings: FeedsSettings?, position: Location?, heading: Float?, showArrow: Boolean ) { if (showArrow && position != null && heading != null) { Location(null).apply { latitude = stop.position.latitude longitude = stop.position.longitude }.let { val angle = (360 + ((position.bearingTo(it) + 360).mod(360f)) - heading).mod(360f) val distance = position.distanceTo(it) holder?.arrow?.apply { setImageResource(R.drawable.arrow) rotation = angle visibility = View.VISIBLE contentDescription = "Arrow" // TODO } holder?.distance?.apply { val us = UnitSystem.getSelected(context!!) text = us.toString(context, us.distanceUnit(Metre(distance.toDouble()))) contentDescription = us.distanceUnit(Metre(distance.toDouble())).contentDescription(context, us.base) visibility = View.VISIBLE } } } else { holder?.arrow?.visibility = View.GONE holder?.distance?.visibility = View.GONE } holder?.icon?.apply { setImageDrawable(stop.icon(context!!)) contentDescription = context.getString(R.string.stop_content_description) } holder?.title?.text = stop.name if ((feedsSettings?.activeFeedsCount() ?: 0) > 1) { holder?.feedName?.visibility = View.VISIBLE holder?.feedName?.text = feeds?.get(stop.feedID)?.name ?: "" } context?.let { stop.changeOptions(it, Stop.LineDecoration.fromPreferences(context)).let { changeOptions -> holder?.description?.apply { text = changeOptions.first contentDescription = changeOptions.second } } } } private fun bindLine( line: Line, holder: BimbaViewHolder?, context: Context?, feeds: Map<String, FeedInfo>?, feedsSettings: FeedsSettings? ) { holder?.icon?.apply { setImageDrawable(line.icon(context!!)) contentDescription = line.type.name colorFilter = null } if ((feedsSettings?.activeFeedsCount() ?: 0) > 1) { holder?.feedName?.visibility = View.VISIBLE holder?.feedName?.text = feeds?.get(line.feedID)?.name ?: "" } holder?.title?.text = line.name holder?.description?.text = if (line.headsigns.size == 1) { context?.getString( R.string.line_headsign, line.headsigns[0].joinToString { it }) } else { context?.getString( R.string.line_headsigns, line.headsigns[0].joinToString { it }, line.headsigns[1].joinToString { it }) } holder?.description?.contentDescription = if (line.headsigns.size == 1) { context?.getString( R.string.line_headsign_content_description, line.headsigns[0].joinToString { it }) } else { context?.getString( R.string.line_headsigns_content_description, line.headsigns[0].joinToString { it }, line.headsigns[1].joinToString { it }) } } } } class BimbaResultsAdapter( private val inflater: LayoutInflater, private val context: Context?, private var queryables: List<Queryable>, private var position: Location?, private var heading: Float?, private var showArrow: Boolean ) : RecyclerView.Adapter<BimbaViewHolder>() { class DiffUtilCallback( private val oldQueryables: List<Queryable>, private val newQueryables: List<Queryable>, private val oldPosition: Location?, private val newPosition: Location?, private val oldHeading: Float?, private val newHeading: Float?, private val oldShowArrow: Boolean, private val newShowArrow: Boolean ) : DiffUtil.Callback() { override fun getOldListSize() = oldQueryables.size override fun getNewListSize() = newQueryables.size override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldQueryable = oldQueryables[oldItemPosition] val newQueryable = newQueryables[newItemPosition] return if (oldQueryable::class.java != newQueryable::class.java) { false } else { when (oldQueryable) { is Line -> { assert(newQueryable is Line) oldQueryable.name == (newQueryable as Line).name // TODO compare line.ID when struct is updated } is Stop -> { assert(newQueryable is Stop) oldQueryable.code == (newQueryable as Stop).code } else -> false // XXX unreachable } } } override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldQueryable = oldQueryables[oldItemPosition] val newQueryable = newQueryables[newItemPosition] return when (oldQueryable) { is Line -> { assert(newQueryable is Line) val oldHeadsigns = oldQueryable.headsigns.joinToString { hsList -> hsList.joinToString { it } } val newHeadsigns = (newQueryable as Line).headsigns.joinToString { hsList -> hsList.joinToString { it } } oldQueryable.name == newQueryable.name && oldQueryable.type == newQueryable.type && oldQueryable.colour == newQueryable.colour && oldHeadsigns == newHeadsigns } is Stop -> { assert(newQueryable is Stop) val oldChangeOptions = oldQueryable.changeOptionsString() val newChangeOptions = (newQueryable as Stop).changeOptionsString() oldQueryable.name == newQueryable.name && oldChangeOptions == newChangeOptions && oldPosition?.latitude == newPosition?.latitude && oldPosition?.longitude == newPosition?.longitude && oldHeading == newHeading && oldShowArrow == newShowArrow } else -> false // XXX unreachable } } } var feeds: Map<String, FeedInfo>? = null var feedsSettings: FeedsSettings? = null private val onClickListener: ((Queryable) -> Unit) = { when (it) { is Stop -> { val intent = Intent(context, DeparturesActivity::class.java).apply { putExtra("code", it.code) putExtra("name", it.name) putExtra("feedID", it.feedID) } context!!.startActivity(intent) } is Line -> { val intent = Intent(context, LineGraphActivity::class.java).apply { putExtra("lineName", it.name) putExtra("lineID", it.id) putExtra("feedID", it.feedID) } context!!.startActivity(intent) } } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaViewHolder { val rowView = inflater.inflate(R.layout.result, parent, false) return BimbaViewHolder(rowView) } override fun onBindViewHolder(holder: BimbaViewHolder, position: Int) { BimbaViewHolder.bind( queryables[position], holder, context, feeds, feedsSettings, onClickListener, this.position, heading, showArrow ) } override fun getItemCount(): Int = queryables.size fun update( queryables: List<Queryable>?, position: Location?, heading: Float?, showArrow: Boolean ) { val diff = DiffUtil.calculateDiff( DiffUtilCallback( this.queryables, queryables ?: emptyList(), this.position, position, this.heading, heading, this.showArrow, showArrow ) ) this.position = position this.heading = heading this.showArrow = showArrow this.queryables = queryables ?: emptyList() diff.dispatchUpdatesTo(this) } fun update( heading: Float?, ) { if (abs((heading ?: 0f) - (this.heading ?: 0f)) < 15) { return } val diff = DiffUtil.calculateDiff( DiffUtilCallback( queryables, queryables, position, position, this.heading, heading, showArrow, showArrow ) ) this.heading = heading diff.dispatchUpdatesTo(this) } fun update( queryables: List<Queryable>?, position: Location?, showArrow: Boolean ) { val diff = DiffUtil.calculateDiff( DiffUtilCallback( this.queryables, queryables ?: emptyList(), this.position, position, heading, heading, this.showArrow, showArrow ) ) this.position = position this.showArrow = showArrow this.queryables = queryables ?: emptyList() diff.dispatchUpdatesTo(this) } fun click(position: Int) { onClickListener(queryables[position]) } } |