Bimba.git

ref: 65df18cd9983af2f0dc626bfe4dedc343eb470dc

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
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>) {
        suggestions = newSuggestions
        suggestions_clone = suggestions
        notifyDataSetChanged()
    }

    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
    }
}