Per-contact colours

Closes #1120
This commit is contained in:
Moez Bhatti
2019-12-15 20:46:33 -05:00
parent 5d62912508
commit 76e0f775d6
35 changed files with 315 additions and 153 deletions
@@ -30,6 +30,11 @@ import com.moez.QKSMS.R
import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.common.util.extensions.resolveThemeBoolean
import com.moez.QKSMS.common.util.extensions.resolveThemeColor
import com.moez.QKSMS.extensions.asObservable
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.util.PhoneNumberUtils
import com.moez.QKSMS.util.Preferences
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
@@ -51,6 +56,9 @@ import javax.inject.Inject
abstract class QkThemedActivity : QkActivity() {
@Inject lateinit var colors: Colors
@Inject lateinit var conversationRepo: ConversationRepository
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var phoneNumberUtils: PhoneNumberUtils
@Inject lateinit var prefs: Preferences
/**
@@ -61,10 +69,40 @@ abstract class QkThemedActivity : QkActivity() {
/**
* Switch the theme if the threadId changes
* Set it based on the latest message in the conversation
*/
val theme = threadId
val theme: Observable<Colors.Theme> = threadId
.distinctUntilChanged()
.switchMap { threadId -> colors.themeObservable(threadId) }
.switchMap { threadId ->
val conversation = conversationRepo.getConversation(threadId)
when {
conversation == null -> Observable.just(0L)
conversation.recipients.size == 1 -> Observable.just(conversation.recipients.first()?.id ?: 0L)
else -> messageRepo.getLastIncomingMessage(conversation.id)
.asObservable()
.mapNotNull { messages -> messages.firstOrNull() }
.distinctUntilChanged { message -> message.address }
.mapNotNull { message ->
conversation.recipients.find { recipient ->
phoneNumberUtils.compare(recipient.address, message.address)
}
}
.map { recipient -> recipient.id }
.startWith(conversation.recipients.firstOrNull()?.id ?: 0)
.distinctUntilChanged()
}
}
.switchMap { colors.themeObservable(it) }
/**
* Emits an event whenever any theme changes, whether it be global or for some recipient. This is useful
* for invalidating recyclerviews
*/
val allThemes by lazy {
prefs.keyChanges.filter { key -> key.contains("theme") }
}
@SuppressLint("InlinedApi")
override fun onCreate(savedInstanceState: Bundle?) {
@@ -66,10 +66,10 @@ class Colors @Inject constructor(private val context: Context, private val prefs
private val secondaryTextLuminance = measureLuminance(context.getColorCompat(R.color.textSecondaryDark))
private val tertiaryTextLuminance = measureLuminance(context.getColorCompat(R.color.textTertiaryDark))
fun theme(threadId: Long = 0): Theme = Theme(prefs.theme(threadId).get(), this)
fun theme(recipientId: Long = 0): Theme = Theme(prefs.theme(recipientId).get(), this)
fun themeObservable(threadId: Long = 0): Observable<Theme> {
return prefs.theme(threadId).asObservable()
fun themeObservable(recipientId: Long = 0): Observable<Theme> {
return prefs.theme(recipientId).asObservable()
.map { color -> Theme(color, this) }
}
@@ -113,6 +113,11 @@ class NotificationManagerImpl @Inject constructor(
}
val conversation = conversationRepo.getConversation(threadId) ?: return
val lastRecipient = conversation.lastMessage?.let { lastMessage ->
conversation.recipients.find { recipient ->
phoneNumberUtils.compare(recipient.address, lastMessage.address)
}
} ?: conversation.recipients.firstOrNull()
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
val taskStackBuilder = TaskStackBuilder.create(context)
@@ -134,7 +139,7 @@ class NotificationManagerImpl @Inject constructor(
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setColor(colors.theme(threadId).theme)
.setColor(colors.theme(lastRecipient?.id ?: 0).theme)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_notification)
.setNumber(messages.size)
@@ -158,8 +163,9 @@ class NotificationManagerImpl @Inject constructor(
val person = Person.Builder()
if (!message.isMe()) {
val recipient = conversation.recipients
.firstOrNull { phoneNumberUtils.compare(it.address, message.address) }
val recipient = conversation.recipients.find { recipient ->
phoneNumberUtils.compare(recipient.address, message.address)
}
person.setName(recipient?.getDisplayName() ?: message.address)
person.setIcon(GlideApp.with(context)
@@ -302,6 +308,12 @@ class NotificationManagerImpl @Inject constructor(
}
val conversation = conversationRepo.getConversation(message.threadId) ?: return
val lastRecipient = conversation.lastMessage?.let { lastMessage ->
conversation.recipients.find { recipient ->
phoneNumberUtils.compare(recipient.address, lastMessage.address)
}
} ?: conversation.recipients.firstOrNull()
val threadId = conversation.id
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
@@ -313,7 +325,7 @@ class NotificationManagerImpl @Inject constructor(
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
.setContentTitle(context.getString(R.string.notification_message_failed_title))
.setContentText(context.getString(R.string.notification_message_failed_text, conversation.getTitle()))
.setColor(colors.theme(threadId).theme)
.setColor(colors.theme(lastRecipient?.id ?: 0).theme)
.setPriority(NotificationManagerCompat.IMPORTANCE_MAX)
.setSmallIcon(R.drawable.ic_notification_failed)
.setAutoCancel(true)
@@ -43,7 +43,7 @@ class AvatarView @JvmOverloads constructor(
/**
* This value can be changes if we should use the theme from a particular conversation
*/
var threadId: Long = 0
var recipientId: Long = 0
set(value) {
if (field == value) return
field = value
@@ -81,13 +81,13 @@ class AvatarView @JvmOverloads constructor(
super.onFinishInflate()
if (!isInEditMode) {
applyTheme(threadId)
applyTheme(recipientId)
updateView()
}
}
private fun applyTheme(threadId: Long) {
colors.theme(threadId).run {
private fun applyTheme(recipientId: Long) {
colors.theme(recipientId).run {
setBackgroundTint(theme)
initial.setTextColor(textPrimary)
icon.setTint(textPrimary)
@@ -35,7 +35,7 @@ class GroupAvatarView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
var contacts: List<Recipient> = ArrayList()
var recipients: List<Recipient> = ArrayList()
set(value) {
field = value.sortedWith(compareByDescending { contact -> contact.contact?.lookupKey })
updateView()
@@ -54,17 +54,25 @@ class GroupAvatarView @JvmOverloads constructor(
}
private fun updateView() {
avatar1Frame.setBackgroundTint(when (contacts.size > 1) {
avatar1Frame.setBackgroundTint(when (recipients.size > 1) {
true -> context.resolveThemeColor(android.R.attr.windowBackground)
false -> context.getColorCompat(android.R.color.transparent)
})
avatar1Frame.updateLayoutParams<LayoutParams> {
matchConstraintPercentWidth = if (contacts.size > 1) 0.75f else 1.0f
matchConstraintPercentWidth = if (recipients.size > 1) 0.75f else 1.0f
}
avatar2.isVisible = contacts.size > 1
avatar2.isVisible = recipients.size > 1
avatar1.setContact(contacts.getOrNull(0)?.contact)
avatar2.setContact(contacts.getOrNull(1)?.contact)
recipients.getOrNull(0).let { recipient ->
avatar1.recipientId = recipient?.id ?: 0
avatar1.setContact(recipient?.contact)
}
recipients.getOrNull(1).let { recipient ->
avatar2.recipientId = recipient?.id ?: 0
avatar2.setContact(recipient?.contact)
}
}
}
@@ -41,7 +41,7 @@ class PagerTitleView @JvmOverloads constructor(context: Context, attrs: Attribut
@Inject lateinit var colors: Colors
private val threadId: Subject<Long> = BehaviorSubject.create()
private val recipientId: Subject<Long> = BehaviorSubject.create()
var pager: ViewPager? = null
set(value) {
@@ -55,8 +55,8 @@ class PagerTitleView @JvmOverloads constructor(context: Context, attrs: Attribut
if (!isInEditMode) appComponent.inject(this)
}
fun setThreadId(id: Long) {
threadId.onNext(id)
fun setRecipientId(id: Long) {
recipientId.onNext(id)
}
private fun recreate() {
@@ -90,9 +90,9 @@ class PagerTitleView @JvmOverloads constructor(context: Context, attrs: Attribut
intArrayOf(android.R.attr.state_activated),
intArrayOf(-android.R.attr.state_activated))
threadId
recipientId
.distinctUntilChanged()
.switchMap { threadId -> colors.themeObservable(threadId) }
.switchMap { recipientId -> colors.themeObservable(recipientId) }
.map { theme ->
val textSecondary = context.resolveThemeColor(android.R.attr.textColorSecondary)
ColorStateList(states, intArrayOf(theme.theme, textSecondary))