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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package ca.allanwang.kau.adapters
import android.content.res.ColorStateList
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import ca.allanwang.kau.utils.adjustAlpha
import ca.allanwang.kau.views.createSimpleRippleDrawable
import com.mikepenz.fastadapter.IExpandable
import com.mikepenz.fastadapter.IItem
import com.mikepenz.fastadapter.ISubItem
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
/**
* Created by Allan Wang on 2017-06-29.
*
* Adapter with a set of colors that will be added to all subsequent items
* Changing a color while the adapter is not empty will reload all items
*
* This adapter overrides every method where an item is added
* If that item extends [ThemableIItem], then the colors will be set
*/
class FastItemThemedAdapter<Item : IItem<*, *>>(
textColor: Int? = null,
backgroundColor: Int? = null,
accentColor: Int? = null
) : FastItemAdapter<Item>() {
constructor(colors: ThemableIItemColors) : this(colors.textColor, colors.backgroundColor, colors.accentColor)
var textColor: Int? = textColor
set(value) {
if (field == value) return
field = value
themeChanged()
}
var backgroundColor: Int? = backgroundColor
set(value) {
if (field == value) return
field = value
themeChanged()
}
var accentColor: Int? = accentColor
set(value) {
if (field == value) return
field = value
themeChanged()
}
fun setColors(colors: ThemableIItemColors) {
this.textColor = colors.textColor
this.backgroundColor = colors.backgroundColor
this.accentColor = colors.accentColor
}
fun themeChanged() {
if (adapterItemCount == 0) return
injectTheme(adapterItems)
notifyAdapterDataSetChanged()
}
override fun add(position: Int, items: MutableList<Item>): FastItemAdapter<Item> {
injectTheme(items)
return super.add(position, items)
}
override fun add(position: Int, item: Item): FastItemAdapter<Item> {
injectTheme(item)
return super.add(position, item)
}
override fun add(item: Item): FastItemAdapter<Item> {
injectTheme(item)
return super.add(item)
}
override fun add(items: MutableList<Item>): FastItemAdapter<Item> {
injectTheme(items)
injectTheme(items)
return super.add(items)
}
override fun set(items: MutableList<Item>?): FastItemAdapter<Item> {
injectTheme(items)
return super.set(items)
}
override fun set(position: Int, item: Item): FastItemAdapter<Item> {
injectTheme(item)
return super.set(position, item)
}
override fun setNewList(items: MutableList<Item>?, retainFilter: Boolean): FastItemAdapter<Item> {
injectTheme(items)
return super.setNewList(items, retainFilter)
}
override fun setNewList(items: MutableList<Item>?): FastItemAdapter<Item> {
injectTheme(items)
return super.setNewList(items)
}
override fun <T, S> setSubItems(collapsible: T, subItems: MutableList<S>?): T where S : IItem<*, *>?, T : IItem<*, *>?, T : IExpandable<T, S>?, S : ISubItem<Item, T>? {
injectTheme(subItems)
return super.setSubItems(collapsible, subItems)
}
internal fun injectTheme(items: Collection<IItem<*, *>?>?) {
items?.forEach { injectTheme(it) }
}
internal fun injectTheme(item: IItem<*, *>?) {
if (item is ThemableIItem && item.themeEnabled) {
item.textColor = textColor
item.backgroundColor = backgroundColor
item.accentColor = accentColor
}
}
}
interface ThemableIItemColors {
var textColor: Int?
var backgroundColor: Int?
var accentColor: Int?
}
class ThemableIItemColorsDelegate : ThemableIItemColors {
override var textColor: Int? = null
override var backgroundColor: Int? = null
override var accentColor: Int? = null
}
/**
* Interface that needs to be implemented by every iitem
* Holds the color values and has helper methods to inject the colors
*/
interface ThemableIItem : ThemableIItemColors {
var themeEnabled: Boolean
fun bindTextColor(vararg views: TextView?)
fun bindTextColorSecondary(vararg views: TextView?)
fun bindDividerColor(vararg views: View?)
fun bindAccentColor(vararg views: TextView?)
fun bindBackgroundColor(vararg views: View?)
fun bindBackgroundRipple(vararg views: View?)
fun bindIconColor(vararg views: ImageView?)
}
/**
* The delegate for [ThemableIItem]
*/
class ThemableIItemDelegate : ThemableIItem, ThemableIItemColors by ThemableIItemColorsDelegate() {
override var themeEnabled: Boolean = true
override fun bindTextColor(vararg views: TextView?) {
val color = textColor ?: return
views.forEach { it?.setTextColor(color) }
}
override fun bindTextColorSecondary(vararg views: TextView?) {
val color = textColor?.adjustAlpha(0.8f) ?: return
views.forEach { it?.setTextColor(color) }
}
override fun bindAccentColor(vararg views: TextView?) {
val color = accentColor ?: textColor ?: return
views.forEach { it?.setTextColor(color) }
}
override fun bindDividerColor(vararg views: View?) {
val color = (textColor ?: accentColor)?.adjustAlpha(0.1f) ?: return
views.forEach { it?.setBackgroundColor(color) }
}
override fun bindBackgroundColor(vararg views: View?) {
val color = backgroundColor ?: return
views.forEach { it?.setBackgroundColor(color) }
}
override fun bindBackgroundRipple(vararg views: View?) {
val foreground = accentColor ?: textColor ?: return
val background = backgroundColor ?: return
val ripple = createSimpleRippleDrawable(foreground, background)
views.forEach { it?.background = ripple }
}
override fun bindIconColor(vararg views: ImageView?) {
val color = accentColor ?: textColor ?: return
views.forEach { it?.drawable.setTintList(ColorStateList.valueOf(color)) }
}
}
|