/
ReactionIconFactory.kt
174 lines (160 loc) · 6.15 KB
/
ReactionIconFactory.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
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
/*
* Copyright (c) 2014-2022 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* 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 io.getstream.chat.android.compose.ui.util
import androidx.annotation.DrawableRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import io.getstream.chat.android.compose.R
/**
* An interface that allows the creation of reaction icons for reaction types.
*/
public interface ReactionIconFactory {
/**
* Checks if the factory is capable of creating an icon for the given reaction type.
*
* @return If the given reaction type is supported by this factory.
*/
public fun isReactionSupported(type: String): Boolean
/**
* Creates an instance of [ReactionIcon] for the given reaction type.
*
* @param type The reaction type used to create an icon.
* @return [ReactionIcon] that contains [Painter]s for normal and selected icon states.
*/
@Composable
public fun createReactionIcon(type: String): ReactionIcon
/**
* Creates [ReactionIcon]s for all the supported reaction types.
*
* @return A map with [ReactionIcon]s for all the supported reaction types.
*/
@Composable
public fun createReactionIcons(): Map<String, ReactionIcon>
public companion object {
/**
* Builds the default reaction icon factory that creates reaction icons from
* drawable resources.
*
* @return The default implementation of [ReactionIconFactory].
*/
public fun defaultFactory(): ReactionIconFactory = DefaultReactionIconFactory()
}
}
/**
* The default implementation of [ReactionIconFactory] that uses drawable resources
* to create reaction icons.
*
* @param supportedReactions The map of supported reaction types with corresponding drawable resources.
*/
private class DefaultReactionIconFactory(
private val supportedReactions: Map<String, ReactionDrawable> = mapOf(
THUMBS_UP to ReactionDrawable(
iconResId = R.drawable.stream_compose_ic_reaction_thumbs_up,
selectedIconResId = R.drawable.stream_compose_ic_reaction_thumbs_up_selected
),
LOVE to ReactionDrawable(
iconResId = R.drawable.stream_compose_ic_reaction_love,
selectedIconResId = R.drawable.stream_compose_ic_reaction_love_selected
),
LOL to ReactionDrawable(
iconResId = R.drawable.stream_compose_ic_reaction_lol,
selectedIconResId = R.drawable.stream_compose_ic_reaction_lol_selected
),
WUT to ReactionDrawable(
iconResId = R.drawable.stream_compose_ic_reaction_wut,
selectedIconResId = R.drawable.stream_compose_ic_reaction_wut_selected
),
THUMBS_DOWN to ReactionDrawable(
iconResId = R.drawable.stream_compose_ic_reaction_thumbs_down,
selectedIconResId = R.drawable.stream_compose_ic_reaction_thumbs_down_selected
)
),
) : ReactionIconFactory {
/**
* Checks if the factory is capable of creating an icon for the given reaction type.
*
* @return If the given reaction type is supported by this factory.
*/
override fun isReactionSupported(type: String): Boolean {
return supportedReactions.containsKey(type)
}
/**
* Creates an instance of [ReactionIcon] for the given reaction type.
*
* @param type The reaction type used to create an icon.
* @return [ReactionIcon] that contains [Painter]s for normal and selected icon states.
*/
@Composable
override fun createReactionIcon(type: String): ReactionIcon {
val reactionDrawable = requireNotNull(supportedReactions[type])
return ReactionIcon(
painter = painterResource(reactionDrawable.iconResId),
selectedPainter = painterResource(reactionDrawable.selectedIconResId)
)
}
/**
* Creates [ReactionIcon]s for all the supported reaction types.
*
* @return A map with [ReactionIcon]s for all the supported reaction types.
*/
@Composable
override fun createReactionIcons(): Map<String, ReactionIcon> {
return supportedReactions.mapValues {
createReactionIcon(it.key)
}
}
companion object {
/**
* The reaction types we support by default.
*/
private const val LOVE: String = "love"
private const val THUMBS_UP: String = "like"
private const val THUMBS_DOWN: String = "sad"
private const val LOL: String = "haha"
private const val WUT: String = "wow"
}
}
/**
* Contains drawable resources for normal and selected reaction states.
*
* @param iconResId The drawable resource id for the normal icon.
* @param selectedIconResId The drawable resource id for the selected state icon.
*/
public data class ReactionDrawable(
@DrawableRes public val iconResId: Int,
@DrawableRes public val selectedIconResId: Int,
)
/**
* Contains [Painter]s for normal and selected states of the reaction icon.
*
* @param painter The [Painter] used to render the reaction in its normal state.
* @param selectedPainter The [Painter] used to render the reaction in its selected state.
*/
public data class ReactionIcon(
val painter: Painter,
val selectedPainter: Painter,
) {
/**
* Returns either one of the [Painter]s depending on the reaction state.
*
* @param isSelected If the reaction is selected.
* @return Either normal or selected [Painter] depending on the reaction state.
*/
public fun getPainter(isSelected: Boolean): Painter {
return if (isSelected) selectedPainter else painter
}
}