aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/views/Keywords.kt
blob: a0a7e5b1747b40b3b70450bb9923390a3d5a96ec (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.views

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import ca.allanwang.kau.utils.bindView
import ca.allanwang.kau.utils.string
import ca.allanwang.kau.utils.tint
import ca.allanwang.kau.utils.toDrawable
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.adapters.FastItemAdapter
import com.mikepenz.fastadapter.items.AbstractItem
import com.mikepenz.fastadapter.listeners.ClickEventHook
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.pitchedapps.frost.R
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.prefs.Prefs
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * Created by Allan Wang on 2017-06-19.
 */
@AndroidEntryPoint
class Keywords @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    @Inject
    lateinit var prefs: Prefs

    @Inject
    lateinit var themeProvider: ThemeProvider

    val editText: AppCompatEditText by bindView(R.id.edit_text)
    val addIcon: ImageView by bindView(R.id.add_icon)
    val recycler: RecyclerView by bindView(R.id.recycler)
    val adapter = FastItemAdapter<KeywordItem>()

    init {
        inflate(context, R.layout.view_keywords, this)
        editText.tint(themeProvider.textColor)
        addIcon.setImageDrawable(
            GoogleMaterial.Icon.gmd_add.keywordDrawable(
                context,
                themeProvider
            )
        )
        addIcon.setOnClickListener {
            if (editText.text.isNullOrEmpty()) editText.error =
                context.string(R.string.empty_keyword)
            else {
                adapter.add(0, KeywordItem(editText.text.toString(), themeProvider))
                editText.text?.clear()
            }
        }
        adapter.add(prefs.notificationKeywords.map { KeywordItem(it, themeProvider) })
        recycler.layoutManager = LinearLayoutManager(context)
        recycler.adapter = adapter
        adapter.addEventHook(object : ClickEventHook<KeywordItem>() {
            override fun onBind(viewHolder: RecyclerView.ViewHolder): View? =
                (viewHolder as? KeywordItem.ViewHolder)?.delete

            override fun onClick(
                v: View,
                position: Int,
                fastAdapter: FastAdapter<KeywordItem>,
                item: KeywordItem
            ) {
                adapter.remove(position)
            }
        })
    }

    fun save() {
        prefs.notificationKeywords = adapter.adapterItems.mapTo(mutableSetOf()) { it.keyword }
    }
}

private fun IIcon.keywordDrawable(context: Context, themeProvider: ThemeProvider): Drawable =
    toDrawable(context, 20, themeProvider.textColor)

class KeywordItem(
    val keyword: String,
    private val themeProvider: ThemeProvider
) : AbstractItem<KeywordItem.ViewHolder>() {

    override fun getViewHolder(v: View): ViewHolder = ViewHolder(v, themeProvider)

    override val layoutRes: Int
        get() = R.layout.item_keyword

    override val type: Int
        get() = R.id.item_keyword

    override fun bindView(holder: ViewHolder, payloads: List<Any>) {
        super.bindView(holder, payloads)
        holder.text.text = keyword
    }

    override fun unbindView(holder: ViewHolder) {
        super.unbindView(holder)
        holder.text.text = null
    }

    class ViewHolder(
        v: View,
        themeProvider: ThemeProvider
    ) : RecyclerView.ViewHolder(v) {

        val text: AppCompatTextView by bindView(R.id.keyword_text)
        val delete: ImageView by bindView(R.id.keyword_delete)

        init {
            text.setTextColor(themeProvider.textColor)
            delete.setImageDrawable(
                GoogleMaterial.Icon.gmd_delete.keywordDrawable(
                    itemView.context,
                    themeProvider
                )
            )
        }
    }
}