aboutsummaryrefslogtreecommitdiff
path: root/about/src/main/kotlin/ca/allanwang/kau/about/FaqIItem.kt
blob: 5cf046e3014af62fa3cc7f54e2113cfb849e8082 (plain)
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
package ca.allanwang.kau.about

import android.annotation.SuppressLint
import android.support.v7.widget.RecyclerView
import android.text.method.LinkMovementMethod
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import ca.allanwang.kau.adapters.ThemableIItem
import ca.allanwang.kau.adapters.ThemableIItemDelegate
import ca.allanwang.kau.iitems.KauIItem
import ca.allanwang.kau.utils.colorToForeground
import ca.allanwang.kau.utils.dpToPx
import ca.allanwang.kau.utils.parentViewGroup
import ca.allanwang.kau.utils.setPaddingLeft
import ca.allanwang.kau.xml.FaqItem
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.IItem
import com.mikepenz.fastadapter.listeners.ClickEventHook

/**
 * Created by Allan Wang on 2017-08-02.
 */
class FaqIItem(val content: FaqItem) : KauIItem<LibraryIItem, FaqIItem.ViewHolder>(
        R.layout.kau_iitem_faq, ::ViewHolder, R.id.kau_item_faq
), ThemableIItem by ThemableIItemDelegate() {

    companion object {
        fun bindEvents(fastAdapter: FastAdapter<IItem<*, *>>) {
            fastAdapter.withSelectable(false)
                    .withEventHook(object : ClickEventHook<IItem<*, *>>() {

                        override fun onBind(viewHolder: RecyclerView.ViewHolder): View? = (viewHolder as? ViewHolder)?.questionContainer

                        override fun onClick(v: View, position: Int, adapter: FastAdapter<IItem<*, *>>, item: IItem<*, *>) {
                            if (item !is FaqIItem) return
                            item.isExpanded = !item.isExpanded
                            v.parentViewGroup.findViewById<CollapsibleTextView>(R.id.faq_item_answer).setExpanded(item.isExpanded)
                        }

                    })
        }
    }

    private var isExpanded = false

    @SuppressLint("SetTextI18n")
    override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) {
        super.bindView(holder, payloads)
        with(holder) {
            number.text = "${content.number}."
            question.text = content.question
            answer.setExpanded(isExpanded, false)
            if (accentColor != null) answer.setLinkTextColor(accentColor!!)
            answer.text = content.answer
            answer.post { answer.setPaddingLeft(16.dpToPx + number.width) }
            bindTextColor(number, question)
            bindTextColorSecondary(answer)
            val bg2 = backgroundColor?.colorToForeground(0.1f)
            if (bg2 != null)
                answer.setBackgroundColor(bg2)
            bindBackgroundRipple(questionContainer)
        }
    }

    override fun unbindView(holder: ViewHolder) {
        super.unbindView(holder)
        with(holder) {
            number.text = null
            question.text = null
            answer.text = null
        }
    }

    class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        val container: ViewGroup = v.findViewById(R.id.faq_item)
        val questionContainer: ViewGroup = v.findViewById(R.id.faq_item_question_container)
        val number: TextView = v.findViewById(R.id.faq_item_number)
        val question: TextView = v.findViewById(R.id.faq_item_question)
        val answer: CollapsibleTextView = v.findViewById(R.id.faq_item_answer)

        init {
            answer.movementMethod = LinkMovementMethod.getInstance()
        }
    }

}