Bimba.git

ref: 2919df3a7321d4e904d6ed9cdd1c3c8c32a5ef6c

app/src/main/java/ml/adamsprogs/bimba/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
package ml.adamsprogs.bimba.search

import android.content.Context
import android.graphics.PorterDuff
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.RecyclerView
import com.mancj.materialsearchbar.adapter.SuggestionsAdapter
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.api.Item
import ml.adamsprogs.bimba.api.Line
import ml.adamsprogs.bimba.api.LineType
import ml.adamsprogs.bimba.api.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(
			item: Item,
			holder: BimbaViewHolder?,
			context: Context?,
			onClickListener: (Item) -> Unit
		) {
			when (item) {
				is Stop -> {
					holder?.icon?.setImageResource(R.drawable.ic_stop_black_24dp) // todo stop icon // todo dark theme colour
					holder?.title?.text = "${item.name} [${item.code}]"
					holder?.description?.text =
						item.changeOptions.joinToString { "${it.line} » ${it.headsign}" }
				}
				is Line -> {
					val icon = item.icon(context!!)
					holder?.icon?.apply {
						setImageBitmap(icon)
						colorFilter = null
						Log.v("Colour", "${item.name}: ${item.colour}, ${item.colour.toInt().toString(16)}")
						Log.v("Colour", "${item.name}: ${item.textColour()}, ${item.textColour().toString(16)}")
					}
					holder?.title?.text = item.name
					holder?.description?.text =
						"${item.headsignsThere.joinToString { it }} «» ${item.headsignsBack.joinToString { it }}"
				}
				else -> TODO("Shouldn't happen")
			}
			holder?.root?.setOnClickListener {
				onClickListener(item)
			}
			/*holder!!.root.setOnClickListener {
				onSuggestionClickListener.onSuggestionClickListener(suggestion)
			}
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
				holder.text.text = Html.fromHtml(suggestion.getBody(context), Html.FROM_HTML_MODE_LEGACY)
			} else {
				@Suppress("DEPRECATION")
				holder.text.text = Html.fromHtml(suggestion.getBody(context))
			}

			holder.text.setTextColor(getColor(context, R.color.textDark))

			val icon = getDrawable(suggestion.getIcon(), context)
			icon.mutate()
			icon.colorFilter = null
			if (suggestion is StopSuggestion)
				when (suggestion.zone) {
					"A" -> icon.setColorFilter(getColor(context, R.color.zoneA), PorterDuff.Mode.SRC_IN)
					"B" -> icon.setColorFilter(getColor(context, R.color.zoneB), PorterDuff.Mode.SRC_IN)
					"C" -> icon.setColorFilter(getColor(context, R.color.zoneC), PorterDuff.Mode.SRC_IN)
					else -> icon.setColorFilter(getColor(context, R.color.textDark), PorterDuff.Mode.SRC_IN)
				}
			else if (suggestion is LineSuggestion) {
				icon.setColorFilter(suggestion.getColour(), PorterDuff.Mode.SRC_IN)
				holder.icon.setBackgroundColor(suggestion.getBgColour())
			}
			holder.icon.setImageDrawable(icon)*/
		}
	}
}

class BimbaResultsAdapter(
	private val inflater: LayoutInflater,
	private val context: Context?,
	private var items: List<Item>,
	private val onClickListener: ((Item) -> Unit)
) :
	RecyclerView.Adapter<BimbaViewHolder>() {
	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaViewHolder {
		val rowView = inflater.inflate(R.layout.suggestion, parent, false)
		return BimbaViewHolder(rowView)
	}

	override fun onBindViewHolder(holder: BimbaViewHolder, position: Int) {
		BimbaViewHolder.bind(items[position], holder, context, onClickListener)
	}

	override fun getItemCount(): Int = items.size

	fun update(items: List<Item>) {
		this.items = items
		notifyDataSetChanged()
	}
}

class BimbaSuggestionsAdapter(
	inflater: LayoutInflater,
	private val context: Context?,
	private val onClickListener: ((Item) -> Unit)
) :
	SuggestionsAdapter<Item, BimbaViewHolder>(inflater) {

	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaViewHolder {
		val rowView = layoutInflater.inflate(R.layout.suggestion, parent, false)
		return BimbaViewHolder(rowView)
	}

	override fun getSingleViewHeight(): Int = 64  // todo calculate actual height

	override fun onBindSuggestionHolder(item: Item, holder: BimbaViewHolder?, pos: Int) {
		BimbaViewHolder.bind(item, holder, context, onClickListener)
	}
}