Bimba.git

ref: 73cee3f10cf4b89f2e742db807d8f12bf4484548

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

import android.annotation.SuppressLint
import android.content.Context
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 xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.repo.Line
import xyz.apiote.bimba.czwek.repo.Queryable
import xyz.apiote.bimba.czwek.repo.Stop

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)

	companion object {
		fun bind(
			queryable: Queryable,
			holder: BimbaViewHolder?,
			context: Context?,
			onClickListener: (Queryable) -> Unit
		) {
			when (queryable) {
				is Stop -> bindStop(queryable, holder, context)
				//is Line -> bindLine(queryable, holder, context)
			}
			holder?.root?.setOnClickListener {
				onClickListener(queryable)
			}
		}

		private fun bindStop(stop: Stop, holder: BimbaViewHolder?, context: Context?) {
			holder?.icon?.apply {
				setImageDrawable(stop.icon(context!!))
				contentDescription = context.getString(R.string.stop_content_description)
			}
			holder?.title?.text = stop.name
			context?.let {
				stop.changeOptions(it).let { changeOptions ->
					holder?.description?.apply {
						text = changeOptions.first
						contentDescription = changeOptions.second
					}
				}
			}
		}

		private fun bindLine(line: Line, holder: BimbaViewHolder?, context: Context?) {
			holder?.icon?.apply {
				setImageDrawable(line.icon(context!!))
				contentDescription = line.type.name
				colorFilter = null
			}
			holder?.title?.text = line.name
			holder?.description?.text = context?.getString(
				R.string.line_headsigns,
				line.headsignsThere.joinToString { it },
				line.headsignsBack.joinToString { it })
			holder?.description?.contentDescription = context?.getString(
				R.string.line_headsigns_content_description,
				line.headsignsThere.joinToString { it },
				line.headsignsBack.joinToString { it })
		}
	}
}


class BimbaResultsAdapter(
	private val inflater: LayoutInflater,
	private val context: Context?,
	private var queryables: List<Queryable>,
	private val onClickListener: ((Queryable) -> Unit)
) :
	RecyclerView.Adapter<BimbaViewHolder>() {
	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, onClickListener)
	}

	override fun getItemCount(): Int = queryables.size

	@SuppressLint("NotifyDataSetChanged") // todo [3.1] DiffUtil
	fun update(queryables: List<Queryable>?) {
		this.queryables = queryables ?: emptyList()
		notifyDataSetChanged()
	}

	fun click(position: Int) {
		onClickListener(queryables[position])
	}
}