Bimba.git

ref: ef9c50aed3aab8d733a725c2aeb539cba7d3880e

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

import android.content.Context
import android.graphics.PorterDuff
import android.os.Build
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat.getColor
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.getDrawable
import ml.adamsprogs.bimba.models.suggestions.EmptySuggestion
import ml.adamsprogs.bimba.models.suggestions.GtfsSuggestion
import ml.adamsprogs.bimba.models.suggestions.LineSuggestion
import ml.adamsprogs.bimba.models.suggestions.StopSuggestion
import com.mancj.materialsearchbar.adapter.SuggestionsAdapter as SearchBarSuggestionsAdapter

class SuggestionsAdapter(inflater: LayoutInflater, private val onSuggestionClickListener: OnSuggestionClickListener, private val context: Context) :
        SearchBarSuggestionsAdapter<GtfsSuggestion, SuggestionsAdapter.ViewHolder>(inflater) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val rowView = layoutInflater.inflate(R.layout.row_suggestion, parent, false)
        return ViewHolder(rowView)
    }

    override fun getSingleViewHeight(): Int = 48

    override fun onBindSuggestionHolder(suggestion: GtfsSuggestion, holder: ViewHolder?, pos: Int) {
        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)

    }

    fun updateSuggestions(newSuggestions: List<GtfsSuggestion>, query: String) {
        suggestions = sort(newSuggestions, query).take(6)
        suggestions_clone = suggestions
        notifyDataSetChanged()
    }

    private fun sort(suggestions: List<GtfsSuggestion>, query: String): List<GtfsSuggestion> {
        val r = Regex(query, RegexOption.IGNORE_CASE)
        return suggestions.sortedBy {
            (r.find(it.name)?.range?.start?.toString()?.padStart(128, '0') ?: "")+it.name
        }
    }

    operator fun contains(suggestion: GtfsSuggestion): Boolean {
        return suggestion in suggestions //|| suggestion in suggestions_clone
    }

    inner class ViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        val root: View = itemView.findViewById(R.id.row_suggestion)
        val icon: ImageView = itemView.findViewById(R.id.suggestion_row_image)
        val text: TextView = itemView.findViewById(R.id.suggestion_row_text)
    }

    interface OnSuggestionClickListener {
        fun onSuggestionClickListener(suggestion: GtfsSuggestion)
    }

    fun equals(other: List<GtfsSuggestion>): Boolean {
        if ((suggestions.containsAll(other) and other.containsAll(suggestions)))
            return true
        if (other.isEmpty())
            if ((suggestions.isEmpty()) or (suggestions[0] is EmptySuggestion))
                return true
        return false
    }
}