aboutsummaryrefslogtreecommitdiff
path: root/about/src/main/kotlin/ca/allanwang/kau/about/FaqIItem.kt
blob: 4495cc19ee78b46e4276013fa95ba4d664401997 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.about

import android.annotation.SuppressLint
import android.text.method.LinkMovementMethod
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
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.GenericItem
import com.mikepenz.fastadapter.listeners.ClickEventHook
import com.mikepenz.fastadapter.select.getSelectExtension

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

    companion object {
        fun bindEvents(fastAdapter: FastAdapter<GenericItem>) {
            fastAdapter.getSelectExtension().isSelectable = true
            fastAdapter.addEventHook(object : ClickEventHook<GenericItem>() {

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

                override fun onClick(
                    v: View,
                    position: Int,
                    fastAdapter: FastAdapter<GenericItem>,
                    item: GenericItem
                ) {
                    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: List<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) } // TODO not performant at all; and doesn't align across all items
            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()
        }
    }
}