Bimba.git

ref: 5d029c3915c5561fa70da7501930a6d58b8ce8c0

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
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.GtfsSuggestion
import ml.adamsprogs.bimba.models.suggestions.LineSuggestion
import ml.adamsprogs.bimba.models.suggestions.StopSuggestion
import com.mancj.materialsearchbar.adapter.SuggestionsAdapter as SearchBarSuggestionsAdapter

// todo on click ripple

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(), Html.FROM_HTML_MODE_LEGACY)
        } else {
            @Suppress("DEPRECATION")
            holder.text.text = Html.fromHtml(suggestion.getBody())
        }

        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)

    }

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