SuggestionsAdapter.kt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package ml.adamsprogs.bimba.models.adapters
  2. import android.content.Context
  3. import android.graphics.PorterDuff
  4. import android.os.Build
  5. import android.text.Html
  6. import android.view.LayoutInflater
  7. import android.view.View
  8. import android.view.ViewGroup
  9. import android.widget.ImageView
  10. import android.widget.TextView
  11. import androidx.core.content.ContextCompat.getColor
  12. import ml.adamsprogs.bimba.R
  13. import ml.adamsprogs.bimba.getDrawable
  14. import ml.adamsprogs.bimba.models.suggestions.EmptySuggestion
  15. import ml.adamsprogs.bimba.models.suggestions.GtfsSuggestion
  16. import ml.adamsprogs.bimba.models.suggestions.LineSuggestion
  17. import ml.adamsprogs.bimba.models.suggestions.StopSuggestion
  18. import com.mancj.materialsearchbar.adapter.SuggestionsAdapter as SearchBarSuggestionsAdapter
  19. class SuggestionsAdapter(inflater: LayoutInflater, private val onSuggestionClickListener: OnSuggestionClickListener, private val context: Context) :
  20. SearchBarSuggestionsAdapter<GtfsSuggestion, SuggestionsAdapter.ViewHolder>(inflater) {
  21. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  22. val rowView = layoutInflater.inflate(R.layout.row_suggestion, parent, false)
  23. return ViewHolder(rowView)
  24. }
  25. override fun getSingleViewHeight(): Int = 48
  26. override fun onBindSuggestionHolder(suggestion: GtfsSuggestion, holder: ViewHolder?, pos: Int) {
  27. holder!!.root.setOnClickListener {
  28. onSuggestionClickListener.onSuggestionClickListener(suggestion)
  29. }
  30. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  31. holder.text.text = Html.fromHtml(suggestion.getBody(context), Html.FROM_HTML_MODE_LEGACY)
  32. } else {
  33. @Suppress("DEPRECATION")
  34. holder.text.text = Html.fromHtml(suggestion.getBody(context))
  35. }
  36. holder.text.setTextColor(getColor(context, R.color.textDark))
  37. val icon = getDrawable(suggestion.getIcon(), context)
  38. icon.mutate()
  39. icon.colorFilter = null
  40. if (suggestion is StopSuggestion)
  41. when (suggestion.zone) {
  42. "A" -> icon.setColorFilter(getColor(context, R.color.zoneA), PorterDuff.Mode.SRC_IN)
  43. "B" -> icon.setColorFilter(getColor(context, R.color.zoneB), PorterDuff.Mode.SRC_IN)
  44. "C" -> icon.setColorFilter(getColor(context, R.color.zoneC), PorterDuff.Mode.SRC_IN)
  45. else -> icon.setColorFilter(getColor(context, R.color.textDark), PorterDuff.Mode.SRC_IN)
  46. }
  47. else if (suggestion is LineSuggestion) {
  48. icon.setColorFilter(suggestion.getColour(), PorterDuff.Mode.SRC_IN)
  49. holder.icon.setBackgroundColor(suggestion.getBgColour())
  50. }
  51. holder.icon.setImageDrawable(icon)
  52. }
  53. fun updateSuggestions(newSuggestions: List<GtfsSuggestion>) {
  54. suggestions = newSuggestions
  55. suggestions_clone = suggestions
  56. notifyDataSetChanged()
  57. }
  58. operator fun contains(suggestion: GtfsSuggestion): Boolean {
  59. return suggestion in suggestions //|| suggestion in suggestions_clone
  60. }
  61. inner class ViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
  62. val root: View = itemView.findViewById(R.id.row_suggestion)
  63. val icon: ImageView = itemView.findViewById(R.id.suggestion_row_image)
  64. val text: TextView = itemView.findViewById(R.id.suggestion_row_text)
  65. }
  66. interface OnSuggestionClickListener {
  67. fun onSuggestionClickListener(suggestion: GtfsSuggestion)
  68. }
  69. fun equals(other: List<GtfsSuggestion>): Boolean {
  70. if ((suggestions.containsAll(other) and other.containsAll(suggestions)))
  71. return true
  72. if (other.isEmpty())
  73. if ((suggestions.isEmpty()) or (suggestions[0] is EmptySuggestion))
  74. return true
  75. return false
  76. }
  77. }