-
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Colorizer.kt
106 lines (90 loc) · 3.54 KB
/
Colorizer.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
package org.thoughtcrime.securesms.conversation.colors
import android.content.Context
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.recipients.RecipientId
import org.whispersystems.signalservice.api.push.ServiceId
/**
* Helper class for all things ChatColors.
*
* - Maintains a mapping for group recipient colors
* - Gives easy access to different bubble colors
* - Watches and responds to RecyclerView scroll and layout changes to update a ColorizerView
*/
class Colorizer {
private var colorsHaveBeenSet = false
@Deprecated("Not needed for CFv2")
private val groupSenderColors: MutableMap<RecipientId, NameColor> = mutableMapOf()
private val groupMembers: LinkedHashSet<ServiceId> = linkedSetOf()
@ColorInt
fun getOutgoingBodyTextColor(context: Context): Int {
return ContextCompat.getColor(context, R.color.conversation_outgoing_body_color)
}
@ColorInt
fun getOutgoingFooterTextColor(context: Context): Int {
return ContextCompat.getColor(context, R.color.conversation_outgoing_footer_color)
}
@ColorInt
fun getOutgoingFooterIconColor(context: Context): Int {
return ContextCompat.getColor(context, R.color.conversation_outgoing_footer_color)
}
@ColorInt
fun getIncomingBodyTextColor(context: Context, hasWallpaper: Boolean): Int {
return if (hasWallpaper) {
ContextCompat.getColor(context, R.color.signal_colorNeutralInverse)
} else {
ContextCompat.getColor(context, R.color.signal_colorOnSurface)
}
}
@ColorInt
fun getIncomingFooterTextColor(context: Context, hasWallpaper: Boolean): Int {
return if (hasWallpaper) {
ContextCompat.getColor(context, R.color.signal_colorNeutralVariantInverse)
} else {
ContextCompat.getColor(context, R.color.signal_colorOnSurfaceVariant)
}
}
@ColorInt
fun getIncomingFooterIconColor(context: Context, hasWallpaper: Boolean): Int {
return if (hasWallpaper) {
ContextCompat.getColor(context, R.color.signal_colorNeutralVariantInverse)
} else {
ContextCompat.getColor(context, R.color.signal_colorOnSurfaceVariant)
}
}
@ColorInt
fun getIncomingGroupSenderColor(context: Context, recipient: Recipient): Int {
return if (groupMembers.isEmpty()) {
groupSenderColors[recipient.id]?.getColor(context) ?: getDefaultColor(context, recipient)
} else {
val memberPosition = groupMembers.indexOf(recipient.requireServiceId())
if (memberPosition >= 0) {
val colorPosition = memberPosition % ChatColorsPalette.Names.all.size
ChatColorsPalette.Names.all[colorPosition].getColor(context)
} else {
getDefaultColor(context, recipient)
}
}
}
fun onGroupMembershipChanged(serviceIds: List<ServiceId>) {
groupMembers.addAll(serviceIds.sortedBy { it.toString() })
}
@Deprecated("Not needed for CFv2", ReplaceWith("onGroupMembershipChanged"))
fun onNameColorsChanged(nameColorMap: Map<RecipientId, NameColor>) {
groupSenderColors.clear()
groupSenderColors.putAll(nameColorMap)
colorsHaveBeenSet = true
}
@ColorInt
private fun getDefaultColor(context: Context, recipient: Recipient): Int {
return if (colorsHaveBeenSet) {
val color = ChatColorsPalette.Names.all[groupSenderColors.size % ChatColorsPalette.Names.all.size]
groupSenderColors[recipient.id] = color
return color.getColor(context)
} else {
getIncomingBodyTextColor(context, recipient.hasWallpaper())
}
}
}