From 684237425472fe8cee23e8cef8e138c748f60b39 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Thu, 26 Dec 2019 18:19:41 -0500 Subject: [PATCH] Automatic contact colours Closes #133 --- .../moez/QKSMS/migration/QkRealmMigration.kt | 11 +- .../java/com/moez/QKSMS/util/Preferences.kt | 12 +- .../QKSMS/common/base/QkThemedActivity.kt | 19 +- .../java/com/moez/QKSMS/common/util/Colors.kt | 83 ++++-- .../common/util/NotificationManagerImpl.kt | 4 +- .../moez/QKSMS/common/widget/AvatarView.kt | 41 +-- .../QKSMS/common/widget/GroupAvatarView.kt | 11 +- .../QKSMS/common/widget/PagerTitleView.kt | 6 +- .../QKSMS/feature/compose/ComposeActivity.kt | 8 +- .../moez/QKSMS/feature/compose/ComposeView.kt | 1 + .../QKSMS/feature/compose/ComposeViewModel.kt | 7 + .../QKSMS/feature/compose/MessagesAdapter.kt | 5 +- .../feature/compose/editing/ChipsAdapter.kt | 3 +- .../compose/editing/DetailedChipView.kt | 3 +- .../ConversationRecipientAdapter.kt | 5 +- .../conversations/ConversationsAdapter.kt | 2 +- .../moez/QKSMS/feature/main/MainActivity.kt | 17 +- .../com/moez/QKSMS/feature/main/MainView.kt | 3 +- .../moez/QKSMS/feature/main/MainViewModel.kt | 15 + .../feature/settings/SettingsController.kt | 3 + .../feature/settings/SettingsPresenter.kt | 5 + .../QKSMS/feature/settings/SettingsState.kt | 1 + .../main/res/layout/settings_controller.xml | 8 + presentation/src/main/res/values/colors.xml | 260 ++++++++++++++++++ presentation/src/main/res/values/strings.xml | 1 + 25 files changed, 428 insertions(+), 106 deletions(-) diff --git a/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt b/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt index be9f9687..6e19486f 100644 --- a/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt +++ b/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt @@ -19,9 +19,9 @@ package com.moez.QKSMS.migration import android.annotation.SuppressLint -import com.f2prateek.rx.preferences2.RxSharedPreferences import com.moez.QKSMS.extensions.map import com.moez.QKSMS.mapper.CursorToContactImpl +import com.moez.QKSMS.util.Preferences import io.realm.DynamicRealm import io.realm.DynamicRealmObject import io.realm.FieldAttribute @@ -32,7 +32,7 @@ import javax.inject.Inject class QkRealmMigration @Inject constructor( private val cursorToContact: CursorToContactImpl, - private val prefs: RxSharedPreferences + private val prefs: Preferences ) : RealmMigration { companion object { @@ -176,7 +176,7 @@ class QkRealmMigration @Inject constructor( // Migrate conversation themes val recipients = mutableMapOf() // Map of recipientId:theme realm.where("Conversation").findAll().forEach { conversation -> - val pref = prefs.getInteger("theme_${conversation.getLong("id")}") + val pref = prefs.theme(conversation.getLong("id")) if (pref.isSet) { conversation.getList("Recipient").forEach { recipient -> recipients[recipient.getLong("id")] = pref.get() @@ -187,9 +187,12 @@ class QkRealmMigration @Inject constructor( } recipients.forEach { (recipientId, theme) -> - prefs.getInteger("theme_$recipientId").set(theme) + prefs.theme(recipientId).set(theme) } + // This is enabled for new users, but the behaviour shouldn't change automatically for old users + prefs.autoColor.set(false) + version++ } diff --git a/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt b/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt index 4dee8900..f21adcc9 100644 --- a/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt +++ b/domain/src/main/java/com/moez/QKSMS/util/Preferences.kt @@ -90,6 +90,7 @@ class Preferences @Inject constructor( val nightStart = rxPrefs.getString("nightStart", "18:00") val nightEnd = rxPrefs.getString("nightEnd", "6:00") val black = rxPrefs.getBoolean("black", false) + val autoColor = rxPrefs.getBoolean("autoColor", true) val systemFont = rxPrefs.getBoolean("systemFont", false) val textSize = rxPrefs.getInteger("textSize", TEXT_SIZE_NORMAL) val blockingManager = rxPrefs.getInteger("blockingManager", BLOCKING_MANAGER_QKSMS) @@ -140,12 +141,13 @@ class Preferences @Inject constructor( sharedPrefs.registerOnSharedPreferenceChangeListener(listener) }.share() - fun theme(recipientId: Long = 0): Preference { - val default = rxPrefs.getInteger("theme", 0xFF0097A7.toInt()) - + fun theme( + recipientId: Long = 0, + default: Int = rxPrefs.getInteger("theme", 0xFF0097A7.toInt()).get() + ): Preference { return when (recipientId) { - 0L -> default - else -> rxPrefs.getInteger("theme_$recipientId", default.get()) + 0L -> rxPrefs.getInteger("theme", 0xFF0097A7.toInt()) + else -> rxPrefs.getInteger("theme_$recipientId", default) } } diff --git a/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt b/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt index 93a39aa3..14b858b2 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt @@ -30,6 +30,7 @@ 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.Optional import com.moez.QKSMS.extensions.asObservable import com.moez.QKSMS.extensions.mapNotNull import com.moez.QKSMS.repository.ConversationRepository @@ -76,9 +77,9 @@ abstract class QkThemedActivity : QkActivity() { .switchMap { threadId -> val conversation = conversationRepo.getConversation(threadId) when { - conversation == null -> Observable.just(0L) + conversation == null -> Observable.just(Optional(null)) - conversation.recipients.size == 1 -> Observable.just(conversation.recipients.first()?.id ?: 0L) + conversation.recipients.size == 1 -> Observable.just(Optional(conversation.recipients.first())) else -> messageRepo.getLastIncomingMessage(conversation.id) .asObservable() @@ -89,20 +90,12 @@ abstract class QkThemedActivity : QkActivity() { phoneNumberUtils.compare(recipient.address, message.address) } } - .map { recipient -> recipient.id } - .startWith(conversation.recipients.firstOrNull()?.id ?: 0) + .map { recipient -> Optional(recipient) } + .startWith(Optional(conversation.recipients.firstOrNull())) .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") } - } + .switchMap { colors.themeObservable(it.value) } @SuppressLint("InlinedApi") override fun onCreate(savedInstanceState: Bundle?) { diff --git a/presentation/src/main/java/com/moez/QKSMS/common/util/Colors.kt b/presentation/src/main/java/com/moez/QKSMS/common/util/Colors.kt index 5c94561d..7eed9260 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/util/Colors.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/util/Colors.kt @@ -20,15 +20,23 @@ package com.moez.QKSMS.common.util import android.content.Context import android.graphics.Color +import androidx.core.content.res.getColorOrThrow import com.moez.QKSMS.R import com.moez.QKSMS.common.util.extensions.getColorCompat +import com.moez.QKSMS.model.Recipient +import com.moez.QKSMS.util.PhoneNumberUtils import com.moez.QKSMS.util.Preferences import io.reactivex.Observable import javax.inject.Inject import javax.inject.Singleton +import kotlin.math.absoluteValue @Singleton -class Colors @Inject constructor(private val context: Context, private val prefs: Preferences) { +class Colors @Inject constructor( + private val context: Context, + private val phoneNumberUtils: PhoneNumberUtils, + private val prefs: Preferences +) { data class Theme(val theme: Int, private val colors: Colors) { val highlight by lazy { colors.highlightColorForTheme(theme) } @@ -37,27 +45,31 @@ class Colors @Inject constructor(private val context: Context, private val prefs val textTertiary by lazy { colors.textTertiaryOnThemeForColor(theme) } } - val materialColors = listOf( - listOf(0xffffebee, 0xffffcdd2, 0xffef9a9a, 0xffe57373, 0xffef5350, 0xfff44336, 0xffe53935, 0xffd32f2f, 0xffc62828, 0xffb71c1c), - listOf(0xffFCE4EC, 0xffF8BBD0, 0xffF48FB1, 0xffF06292, 0xffEC407A, 0xffE91E63, 0xffD81B60, 0xffC2185B, 0xffAD1457, 0xff880E4F), - listOf(0xffF3E5F5, 0xffE1BEE7, 0xffCE93D8, 0xffBA68C8, 0xffAB47BC, 0xff9C27B0, 0xff8E24AA, 0xff7B1FA2, 0xff6A1B9A, 0xff4A148C), - listOf(0xffEDE7F6, 0xffD1C4E9, 0xffB39DDB, 0xff9575CD, 0xff7E57C2, 0xff673AB7, 0xff5E35B1, 0xff512DA8, 0xff4527A0, 0xff311B92), - listOf(0xffE8EAF6, 0xffC5CAE9, 0xff9FA8DA, 0xff7986CB, 0xff5C6BC0, 0xff3F51B5, 0xff3949AB, 0xff303F9F, 0xff283593, 0xff1A237E), - listOf(0xffE3F2FD, 0xffBBDEFB, 0xff90CAF9, 0xff64B5F6, 0xff42A5F5, 0xff2196F3, 0xff1E88E5, 0xff1976D2, 0xff1565C0, 0xff0D47A1), - listOf(0xffE1F5FE, 0xffB3E5FC, 0xff81D4FA, 0xff4FC3F7, 0xff29B6F6, 0xff03A9F4, 0xff039BE5, 0xff0288D1, 0xff0277BD, 0xff01579B), - listOf(0xffE0F7FA, 0xffB2EBF2, 0xff80DEEA, 0xff4DD0E1, 0xff26C6DA, 0xff00BCD4, 0xff00ACC1, 0xff0097A7, 0xff00838F, 0xff006064), - listOf(0xffE0F2F1, 0xffB2DFDB, 0xff80CBC4, 0xff4DB6AC, 0xff26A69A, 0xff009688, 0xff00897B, 0xff00796B, 0xff00695C, 0xff004D40), - listOf(0xffE8F5E9, 0xffC8E6C9, 0xffA5D6A7, 0xff81C784, 0xff66BB6A, 0xff4CAF50, 0xff43A047, 0xff388E3C, 0xff2E7D32, 0xff1B5E20), - listOf(0xffF1F8E9, 0xffDCEDC8, 0xffC5E1A5, 0xffAED581, 0xff9CCC65, 0xff8BC34A, 0xff7CB342, 0xff689F38, 0xff558B2F, 0xff33691E), - listOf(0xffF9FBE7, 0xffF0F4C3, 0xffE6EE9C, 0xffDCE775, 0xffD4E157, 0xffCDDC39, 0xffC0CA33, 0xffAFB42B, 0xff9E9D24, 0xff827717), - listOf(0xffFFFDE7, 0xffFFF9C4, 0xffFFF59D, 0xffFFF176, 0xffFFEE58, 0xffFFEB3B, 0xffFDD835, 0xffFBC02D, 0xffF9A825, 0xffF57F17), - listOf(0xffFFF8E1, 0xffFFECB3, 0xffFFE082, 0xffFFD54F, 0xffFFCA28, 0xffFFC107, 0xffFFB300, 0xffFFA000, 0xffFF8F00, 0xffFF6F00), - listOf(0xffFFF3E0, 0xffFFE0B2, 0xffFFCC80, 0xffFFB74D, 0xffFFA726, 0xffFF9800, 0xffFB8C00, 0xffF57C00, 0xffEF6C00, 0xffE65100), - listOf(0xffFBE9E7, 0xffFFCCBC, 0xffFFAB91, 0xffFF8A65, 0xffFF7043, 0xffFF5722, 0xffF4511E, 0xffE64A19, 0xffD84315, 0xffBF360C), - listOf(0xffEFEBE9, 0xffD7CCC8, 0xffBCAAA4, 0xffA1887F, 0xff8D6E63, 0xff795548, 0xff6D4C41, 0xff5D4037, 0xff4E342E, 0xff3E2723), - listOf(0xffFAFAFA, 0xffF5F5F5, 0xffEEEEEE, 0xffE0E0E0, 0xffBDBDBD, 0xff9E9E9E, 0xff757575, 0xff616161, 0xff424242, 0xff212121), - listOf(0xffECEFF1, 0xffCFD8DC, 0xffB0BEC5, 0xff90A4AE, 0xff78909C, 0xff607D8B, 0xff546E7A, 0xff455A64, 0xff37474F, 0xff263238)) - .map { it.map { it.toInt() } } + val materialColors: List> = listOf( + R.array.material_red, + R.array.material_pink, + R.array.material_purple, + R.array.material_deep_purple, + R.array.material_indigo, + R.array.material_blue, + R.array.material_light_blue, + R.array.material_cyan, + R.array.material_teal, + R.array.material_green, + R.array.material_light_green, + R.array.material_lime, + R.array.material_yellow, + R.array.material_amber, + R.array.material_orange, + R.array.material_deep_orange, + R.array.material_brown, + R.array.material_gray, + R.array.material_blue_gray) + .map { res -> context.resources.obtainTypedArray(res) } + .map { typedArray -> (0 until typedArray.length()).map(typedArray::getColorOrThrow) } + + private val randomColors: List = context.resources.obtainTypedArray(R.array.random_colors) + .let { typedArray -> (0 until typedArray.length()).map(typedArray::getColorOrThrow) } private val minimumContrastRatio = 2 @@ -66,10 +78,21 @@ 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(recipientId: Long = 0): Theme = Theme(prefs.theme(recipientId).get(), this) + fun theme(recipient: Recipient? = null): Theme { + val pref = prefs.theme(recipient?.id ?: 0) + val color = when { + recipient == null || !prefs.autoColor.get() || pref.isSet -> pref.get() + else -> generateColor(recipient) + } + return Theme(color, this) + } - fun themeObservable(recipientId: Long = 0): Observable { - return prefs.theme(recipientId).asObservable() + fun themeObservable(recipient: Recipient? = null): Observable { + val pref = when { + recipient == null || !prefs.autoColor.get() -> prefs.theme() + else -> prefs.theme(recipient.id, generateColor(recipient)) + } + return pref.asObservable() .map { color -> Theme(color, this) } } @@ -110,4 +133,12 @@ class Colors @Inject constructor(private val context: Context, private val prefs return 0.2126 * array[0] + 0.7152 * array[1] + 0.0722 * array[2] + 0.05 } -} \ No newline at end of file + private fun generateColor(recipient: Recipient): Int { + val first = recipient.contact?.name?.firstOrNull() + ?: phoneNumberUtils.normalizeNumber(recipient.address).firstOrNull() + ?: '#' + + val index = first.hashCode().absoluteValue % randomColors.size + return randomColors[index] + } +} diff --git a/presentation/src/main/java/com/moez/QKSMS/common/util/NotificationManagerImpl.kt b/presentation/src/main/java/com/moez/QKSMS/common/util/NotificationManagerImpl.kt index 3d6625fe..22b47ff5 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/util/NotificationManagerImpl.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/util/NotificationManagerImpl.kt @@ -139,7 +139,7 @@ class NotificationManagerImpl @Inject constructor( val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId)) .setCategory(NotificationCompat.CATEGORY_MESSAGE) - .setColor(colors.theme(lastRecipient?.id ?: 0).theme) + .setColor(colors.theme(lastRecipient).theme) .setPriority(NotificationCompat.PRIORITY_MAX) .setSmallIcon(R.drawable.ic_notification) .setNumber(messages.size) @@ -325,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(lastRecipient?.id ?: 0).theme) + .setColor(colors.theme(lastRecipient).theme) .setPriority(NotificationManagerCompat.IMPORTANCE_MAX) .setSmallIcon(R.drawable.ic_notification_failed) .setAutoCancel(true) diff --git a/presentation/src/main/java/com/moez/QKSMS/common/widget/AvatarView.kt b/presentation/src/main/java/com/moez/QKSMS/common/widget/AvatarView.kt index 1a289b11..054f2095 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/widget/AvatarView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/widget/AvatarView.kt @@ -28,7 +28,7 @@ import com.moez.QKSMS.common.util.Colors import com.moez.QKSMS.common.util.extensions.setBackgroundTint import com.moez.QKSMS.common.util.extensions.setTint import com.moez.QKSMS.injection.appComponent -import com.moez.QKSMS.model.Contact +import com.moez.QKSMS.model.Recipient import com.moez.QKSMS.util.GlideApp import kotlinx.android.synthetic.main.avatar_view.view.* import javax.inject.Inject @@ -40,28 +40,20 @@ class AvatarView @JvmOverloads constructor( @Inject lateinit var colors: Colors @Inject lateinit var navigator: Navigator - /** - * This value can be changes if we should use the theme from a particular conversation - */ - var recipientId: Long = 0 - set(value) { - if (field == value) return - field = value - applyTheme(value) - } - private var lookupKey: String? = null private var name: String? = null private var photoUri: String? = null private var lastUpdated: Long? = null + private var theme: Colors.Theme init { if (!isInEditMode) { appComponent.inject(this) } - View.inflate(context, R.layout.avatar_view, this) + theme = colors.theme() + View.inflate(context, R.layout.avatar_view, this) setBackgroundResource(R.drawable.circle) clipToOutline = true } @@ -69,11 +61,12 @@ class AvatarView @JvmOverloads constructor( /** * Use the [contact] information to display the avatar. */ - fun setContact(contact: Contact?) { - lookupKey = contact?.lookupKey - name = contact?.name - photoUri = contact?.photoUri - lastUpdated = contact?.lastUpdate + fun setRecipient(recipient: Recipient?) { + lookupKey = recipient?.contact?.lookupKey + name = recipient?.contact?.name + photoUri = recipient?.contact?.photoUri + lastUpdated = recipient?.contact?.lastUpdate + theme = colors.theme(recipient) updateView() } @@ -81,20 +74,16 @@ class AvatarView @JvmOverloads constructor( super.onFinishInflate() if (!isInEditMode) { - applyTheme(recipientId) updateView() } } - private fun applyTheme(recipientId: Long) { - colors.theme(recipientId).run { - setBackgroundTint(theme) - initial.setTextColor(textPrimary) - icon.setTint(textPrimary) - } - } - private fun updateView() { + // Apply theme + setBackgroundTint(theme.theme) + initial.setTextColor(theme.textPrimary) + icon.setTint(theme.textPrimary) + if (name?.isNotEmpty() == true) { val initials = name?.split(" ").orEmpty() .filter { name -> name.isNotEmpty() } diff --git a/presentation/src/main/java/com/moez/QKSMS/common/widget/GroupAvatarView.kt b/presentation/src/main/java/com/moez/QKSMS/common/widget/GroupAvatarView.kt index 695abe81..85b240a1 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/widget/GroupAvatarView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/widget/GroupAvatarView.kt @@ -64,15 +64,8 @@ class GroupAvatarView @JvmOverloads constructor( avatar2.isVisible = recipients.size > 1 - 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) - } + recipients.getOrNull(0).run(avatar1::setRecipient) + recipients.getOrNull(1).run(avatar2::setRecipient) } } diff --git a/presentation/src/main/java/com/moez/QKSMS/common/widget/PagerTitleView.kt b/presentation/src/main/java/com/moez/QKSMS/common/widget/PagerTitleView.kt index aab9fccc..0d3d5db6 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/widget/PagerTitleView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/widget/PagerTitleView.kt @@ -29,7 +29,9 @@ import com.moez.QKSMS.R import com.moez.QKSMS.common.util.Colors import com.moez.QKSMS.common.util.extensions.forEach import com.moez.QKSMS.common.util.extensions.resolveThemeColor +import com.moez.QKSMS.extensions.Optional import com.moez.QKSMS.injection.appComponent +import com.moez.QKSMS.repository.ConversationRepository import com.uber.autodispose.android.ViewScopeProvider import com.uber.autodispose.autoDisposable import io.reactivex.subjects.BehaviorSubject @@ -40,6 +42,7 @@ import javax.inject.Inject class PagerTitleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs) { @Inject lateinit var colors: Colors + @Inject lateinit var conversationRepo: ConversationRepository private val recipientId: Subject = BehaviorSubject.create() @@ -92,7 +95,8 @@ class PagerTitleView @JvmOverloads constructor(context: Context, attrs: Attribut recipientId .distinctUntilChanged() - .switchMap { recipientId -> colors.themeObservable(recipientId) } + .map { recipientId -> Optional(conversationRepo.getRecipient(recipientId)) } + .switchMap { recipient -> colors.themeObservable(recipient.value) } .map { theme -> val textSecondary = context.resolveThemeColor(android.R.attr.textColorSecondary) ColorStateList(states, intArrayOf(theme.theme, textSecondary)) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt index fcc4239e..ad524c85 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt @@ -153,10 +153,6 @@ class ComposeActivity : QkThemedActivity(), ComposeView { .autoDisposable(scope()) .subscribe() - allThemes - .autoDisposable(scope()) - .subscribe { messageList.scrapViews() } - window.callback = ComposeWindowCallback(window.callback, this) // These theme attributes don't apply themselves on API 21 @@ -299,6 +295,10 @@ class ComposeActivity : QkThemedActivity(), ComposeView { startActivityForResult(intent, SelectContactRequestCode) } + override fun themeChanged() { + messageList.scrapViews() + } + override fun showKeyboard() { message.postDelayed({ message.showKeyboard() diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt index f4adc28b..56fe0f22 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt @@ -62,6 +62,7 @@ interface ComposeView : QkView { fun requestStoragePermission() fun requestSmsPermission() fun showContacts(sharing: Boolean, chips: List) + fun themeChanged() fun showKeyboard() fun requestCamera() fun requestGallery() diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt index 04108060..3a24af87 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt @@ -394,6 +394,13 @@ class ComposeViewModel @Inject constructor( .autoDisposable(view.scope()) .subscribe(view::scrollToMessage) + // Theme changes + prefs.keyChanges + .filter { key -> key.contains("theme") } + .doOnNext { view.themeChanged() } + .autoDisposable(view.scope()) + .subscribe() + // Retry sending view.messageClickIntent .mapNotNull(messageRepo::getMessage) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt index ffd83a4b..88fbfb0b 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt @@ -183,7 +183,7 @@ class MessagesAdapter @Inject constructor( val theme = when (message.isOutgoingMessage()) { true -> colors.theme() - false -> colors.theme(contactCache[message.address]?.id ?: 0) + false -> colors.theme(contactCache[message.address]) } // Update the selected state @@ -232,8 +232,7 @@ class MessagesAdapter @Inject constructor( // Bind the avatar and bubble colour if (!message.isMe()) { - view.avatar.recipientId = contactCache[message.address]?.id ?: 0 - view.avatar.setContact(contactCache[message.address]?.contact) + view.avatar.setRecipient(contactCache[message.address]) view.avatar.setVisible(!canGroup(message, next), View.INVISIBLE) view.body.setTextColor(theme.textPrimary) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt index 50cc7eb2..0bd7f084 100755 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt @@ -30,6 +30,7 @@ import com.moez.QKSMS.common.base.QkViewHolder import com.moez.QKSMS.common.util.extensions.dpToPx import com.moez.QKSMS.common.util.extensions.resolveThemeColor import com.moez.QKSMS.common.util.extensions.setBackgroundTint +import com.moez.QKSMS.model.Recipient import io.reactivex.subjects.PublishSubject import kotlinx.android.synthetic.main.contact_chip.* import kotlinx.android.synthetic.main.contact_chip.view.* @@ -60,7 +61,7 @@ class ChipsAdapter @Inject constructor() : QkAdapter() { val chip = getItem(position) val view = holder.containerView - view.avatar.setContact(chip.contact) + view.avatar.setRecipient(Recipient(id = -1, contact = chip.contact)) view.name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt index ce35202a..370d1a59 100755 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt @@ -27,6 +27,7 @@ import com.moez.QKSMS.common.util.Colors import com.moez.QKSMS.common.util.extensions.setBackgroundTint import com.moez.QKSMS.common.util.extensions.setTint import com.moez.QKSMS.injection.appComponent +import com.moez.QKSMS.model.Recipient import kotlinx.android.synthetic.main.contact_chip_detailed.view.* import javax.inject.Inject @@ -54,7 +55,7 @@ class DetailedChipView(context: Context) : RelativeLayout(context) { } fun setChip(chip: Chip) { - avatar.setContact(chip.contact) + avatar.setRecipient(Recipient(id = -1, contact = chip.contact)) name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address info.text = chip.address } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/conversationinfo/ConversationRecipientAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/conversationinfo/ConversationRecipientAdapter.kt index 9a6eb399..35baf7be 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/conversationinfo/ConversationRecipientAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/conversationinfo/ConversationRecipientAdapter.kt @@ -59,8 +59,7 @@ class ConversationRecipientAdapter @Inject constructor( val recipient = getItem(position) ?: return val view = holder.containerView - view.avatar.recipientId = recipient.id - view.avatar.setContact(recipient.contact) + view.avatar.setRecipient(recipient) view.name.text = recipient.contact?.name ?: recipient.address @@ -69,7 +68,7 @@ class ConversationRecipientAdapter @Inject constructor( view.add.setVisible(recipient.contact == null) - val theme = colors.theme(recipient.id) + val theme = colors.theme(recipient) view.theme.setTint(theme.theme) } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt index de51d8b5..0b32f81d 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/conversations/ConversationsAdapter.kt @@ -110,7 +110,7 @@ class ConversationsAdapter @Inject constructor( } } - view.unread.setTint(colors.theme(recipient?.id ?: 0).theme) + view.unread.setTint(colors.theme(recipient).theme) } override fun getItemId(index: Int): Long { diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt index e8e5e58f..c0816b72 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainActivity.kt @@ -83,7 +83,7 @@ class MainActivity : QkThemedActivity(), MainView { @Inject lateinit var viewModelFactory: ViewModelProvider.Factory override val onNewIntentIntent: Subject = PublishSubject.create() - override val activityResumedIntent: Subject = PublishSubject.create() + override val activityResumedIntent: Subject = PublishSubject.create() override val queryChangedIntent by lazy { toolbarSearch.textChanges() } override val composeIntent by lazy { compose.clicks() } override val drawerOpenIntent: Observable by lazy { @@ -182,10 +182,6 @@ class MainActivity : QkThemedActivity(), MainView { compose.setTint(theme.textPrimary) } - allThemes - .autoDisposable(scope()) - .subscribe { recyclerView.scrapViews() } - itemTouchCallback.adapter = conversationsAdapter conversationsAdapter.autoScrollToStart(recyclerView) @@ -330,7 +326,12 @@ class MainActivity : QkThemedActivity(), MainView { override fun onResume() { super.onResume() - activityResumedIntent.onNext(Unit) + activityResumedIntent.onNext(true) + } + + override fun onPause() { + super.onPause() + activityResumedIntent.onNext(false) } override fun onDestroy() { @@ -366,6 +367,10 @@ class MainActivity : QkThemedActivity(), MainView { conversationsAdapter.clearSelection() } + override fun themeChanged() { + recyclerView.scrapViews() + } + override fun showBlockingDialog(conversations: List, block: Boolean) { blockingDialog.show(this, conversations, block) } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt index f774a28c..47511e09 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainView.kt @@ -26,7 +26,7 @@ import io.reactivex.Observable interface MainView : QkView { val onNewIntentIntent: Observable - val activityResumedIntent: Observable<*> + val activityResumedIntent: Observable val queryChangedIntent: Observable val composeIntent: Observable val drawerOpenIntent: Observable @@ -47,6 +47,7 @@ interface MainView : QkView { fun requestPermissions() fun clearSearch() fun clearSelection() + fun themeChanged() fun showBlockingDialog(conversations: List, block: Boolean) fun showDeleteDialog(conversations: List) fun showChangelog(changelog: ChangelogManager.Changelog) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt index dd934084..37f90b9f 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/main/MainViewModel.kt @@ -131,6 +131,7 @@ class MainViewModel @Inject constructor( } val permissions = view.activityResumedIntent + .filter { resumed -> resumed } .observeOn(Schedulers.io()) .map { Triple(permissionManager.isDefaultSms(), permissionManager.hasReadSms(), permissionManager.hasContacts()) } .distinctUntilChanged() @@ -202,6 +203,20 @@ class MainViewModel @Inject constructor( .autoDisposable(view.scope()) .subscribe { data -> newState { copy(page = Searching(loading = false, data = data)) } } + view.activityResumedIntent + .filter { resumed -> !resumed } + .switchMap { + // Take until the activity is resumed + prefs.keyChanges + .filter { key -> key.contains("theme") } + .map { true } + .mergeWith(prefs.autoColor.asObservable()) + .doOnNext { view.themeChanged() } + .takeUntil(view.activityResumedIntent.filter { resumed -> resumed }) + } + .autoDisposable(view.scope()) + .subscribe() + view.composeIntent .autoDisposable(view.scope()) .subscribe { navigator.showCompose() } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt index c29b0e69..083abd8c 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt @@ -161,6 +161,9 @@ class SettingsController : QkController newState { copy(autoColor = autoColor) } } + disposables += prefs.systemFont.asObservable() .subscribe { enabled -> newState { copy(systemFontEnabled = enabled) } } @@ -164,6 +167,8 @@ class SettingsPresenter @Inject constructor( R.id.textSize -> view.showTextSizePicker() + R.id.autoColor -> prefs.autoColor.set(!prefs.autoColor.get()) + R.id.systemFont -> prefs.systemFont.set(!prefs.systemFont.get()) R.id.unicode -> prefs.unicode.set(!prefs.unicode.get()) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsState.kt b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsState.kt index f78a841f..1119b737 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsState.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsState.kt @@ -28,6 +28,7 @@ data class SettingsState( val nightStart: String = "", val nightEnd: String = "", val black: Boolean = false, + val autoColor: Boolean = true, val autoEmojiEnabled: Boolean = true, val notificationsEnabled: Boolean = true, val sendDelaySummary: String = "", diff --git a/presentation/src/main/res/layout/settings_controller.xml b/presentation/src/main/res/layout/settings_controller.xml index b96f8b83..8e34c5e9 100644 --- a/presentation/src/main/res/layout/settings_controller.xml +++ b/presentation/src/main/res/layout/settings_controller.xml @@ -84,6 +84,14 @@ app:icon="@drawable/ic_format_size_black_24dp" app:title="@string/settings_text_size_title" /> + + #00838F + + + #e57373 + #7986CB + #00BFA5 + #81C784 + #FFAB40 + #FF8A65 + #8d6e63 + #448AFF + #78909c + + + + #FFEBEE + #FFCDD2 + #EF9A9A + #E57373 + #EF5350 + #F44336 + #E53935 + #D32F2F + #C62828 + #B71C1C + + + + #FCE4EC + #F8BBD0 + #F48FB1 + #F06292 + #EC407A + #E91E63 + #D81B60 + #C2185B + #AD1457 + #880E4F + + + + #F3E5F5 + #E1BEE7 + #CE93D8 + #BA68C8 + #AB47BC + #9C27B0 + #8E24AA + #7B1FA2 + #6A1B9A + #4A148C + + + + #EDE7F6 + #D1C4E9 + #B39DDB + #9575CD + #7E57C2 + #673AB7 + #5E35B1 + #512DA8 + #4527A0 + #311B92 + + + + #E8EAF6 + #C5CAE9 + #9FA8DA + #7986CB + #5C6BC0 + #3F51B5 + #3949AB + #303F9F + #283593 + #1A237E + + + + #E3F2FD + #BBDEFB + #90CAF9 + #64B5F6 + #42A5F5 + #2196F3 + #1E88E5 + #1976D2 + #1565C0 + #0D47A1 + + + + #E1F5FE + #B3E5FC + #81D4FA + #4FC3F7 + #29B6F6 + #03A9F4 + #039BE5 + #0288D1 + #0277BD + #01579B + + + + #E0F7FA + #B2EBF2 + #80DEEA + #4DD0E1 + #26C6DA + #00BCD4 + #00ACC1 + #0097A7 + #00838F + #006064 + + + + #E0F2F1 + #B2DFDB + #80CBC4 + #4DB6AC + #26A69A + #009688 + #00897B + #00796B + #00695C + #004D40 + + + + #E8F5E9 + #C8E6C9 + #A5D6A7 + #81C784 + #66BB6A + #4CAF50 + #43A047 + #388E3C + #2E7D32 + #1B5E20 + + + + #F1F8E9 + #DCEDC8 + #C5E1A5 + #AED581 + #9CCC65 + #8BC34A + #7CB342 + #689F38 + #558B2F + #33691E + + + + #F9FBE7 + #F0F4C3 + #E6EE9C + #DCE775 + #D4E157 + #CDDC39 + #C0CA33 + #AFB42B + #9E9D24 + #827717 + + + + #FFFDE7 + #FFF9C4 + #FFF59D + #FFF176 + #FFEE58 + #FFEB3B + #FDD835 + #FBC02D + #F9A825 + #F57F17 + + + + #FFF8E1 + #FFECB3 + #FFE082 + #FFD54F + #FFCA28 + #FFC107 + #FFB300 + #FFA000 + #FF8F00 + #FF6F00 + + + + #FFF3E0 + #FFE0B2 + #FFCC80 + #FFB74D + #FFA726 + #FF9800 + #FB8C00 + #F57C00 + #EF6C00 + #E65100 + + + + #FBE9E7 + #FFCCBC + #FFAB91 + #FF8A65 + #FF7043 + #FF5722 + #F4511E + #E64A19 + #D84315 + #BF360C + + + + #EFEBE9 + #D7CCC8 + #BCAAA4 + #A1887F + #8D6E63 + #795548 + #6D4C41 + #5D4037 + #4E342E + #3E2723 + + + + #FAFAFA + #F5F5F5 + #EEEEEE + #E0E0E0 + #BDBDBD + #9E9E9E + #757575 + #616161 + #424242 + #212121 + + + + #ECEFF1 + #CFD8DC + #B0BEC5 + #90A4AE + #78909C + #607D8B + #546E7A + #455A64 + #37474F + #263238 + + diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index e1ee899d..35c6011a 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -213,6 +213,7 @@ Pure black night mode Start time End time + Automatic contact colors Font size Use system font Automatic emoji