Add per-conversation theme support

This commit is contained in:
Moez Bhatti
2018-03-12 17:42:33 -07:00
parent 6d6b3fbf77
commit b1625d7cd6
19 changed files with 166 additions and 76 deletions
+33 -24
View File
@@ -31,7 +31,7 @@ import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class Colors @Inject constructor(private val context: Context, prefs: Preferences) {
class Colors @Inject constructor(private val context: Context, private val prefs: Preferences) {
val materialColors = listOf(
listOf(0xffffebee, 0xffffcdd2, 0xffef9a9a, 0xffe57373, 0xffef5350, 0xfff44336, 0xffe53935, 0xffd32f2f, 0xffc62828, 0xffb71c1c),
@@ -62,8 +62,7 @@ class Colors @Inject constructor(private val context: Context, prefs: Preference
private val secondaryTextLuminance = measureLuminance(getColor(R.color.textSecondaryDark))
private val tertiaryTextLuminance = measureLuminance(getColor(R.color.textTertiaryDark))
val theme: Observable<Int> = prefs.theme.asObservable()
.distinctUntilChanged()
val theme = themeForConversation()
val appThemeResources = Observables.combineLatest(prefs.night.asObservable(), prefs.black.asObservable(),
{ night, black ->
@@ -158,29 +157,11 @@ class Colors @Inject constructor(private val context: Context, prefs: Preference
.map { res -> getColor(res) }
.distinctUntilChanged()
val textPrimaryOnTheme: Observable<Int> = prefs.theme.asObservable()
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> primaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textPrimary else R.color.textPrimaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
val textPrimaryOnTheme = textPrimaryOnThemeForConversation()
val textSecondaryOnTheme: Observable<Int> = prefs.theme.asObservable()
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> secondaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textSecondary else R.color.textSecondaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
val textSecondaryOnTheme = textSecondaryOnThemeForConversation()
val textTertiaryOnTheme: Observable<Int> = prefs.theme.asObservable()
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> tertiaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textTertiary else R.color.textTertiaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
val textTertiaryOnTheme = textTertiaryOnThemeForConversation()
val bubble: Observable<Int> = prefs.night.asObservable()
.map { night -> if (night) R.color.bubbleDark else R.color.bubbleLight }
@@ -207,6 +188,34 @@ class Colors @Inject constructor(private val context: Context, prefs: Preference
.map { res -> getColor(res) }
.distinctUntilChanged()
fun themeForConversation(threadId: Long = 0): Observable<Int> = prefs.theme(threadId)
.asObservable()
.distinctUntilChanged()
fun textPrimaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> primaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textPrimary else R.color.textPrimaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
fun textSecondaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> secondaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textSecondary else R.color.textSecondaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
fun textTertiaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.map { theme -> measureLuminance(theme) }
.map { themeLuminance -> tertiaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio }
.map { contrastRatio -> if (contrastRatio) R.color.textTertiary else R.color.textTertiaryDark }
.map { res -> getColor(res) }
.distinctUntilChanged()
private fun getColor(@ColorRes res: Int): Int {
return context.resources.getColor(res)
}
@@ -110,7 +110,7 @@ class NotificationManager @Inject constructor(
val readAction = NotificationCompat.Action(R.drawable.ic_done_black_24dp, context.getString(R.string.notification_read), readPI)
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
.setColor(colors.theme.blockingFirst())
.setColor(colors.themeForConversation(threadId).blockingFirst())
.setPriority(NotificationManagerCompat.IMPORTANCE_MAX)
.setSmallIcon(R.drawable.ic_notification)
.setNumber(messages.size)
@@ -149,7 +149,7 @@ class NotificationManager @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.blockingFirst())
.setColor(colors.themeForConversation(threadId).blockingFirst())
.setPriority(NotificationManagerCompat.IMPORTANCE_MAX)
.setSmallIcon(R.drawable.ic_notification_failed)
.setAutoCancel(true)
@@ -34,7 +34,6 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
}
val defaultSms = rxPrefs.getBoolean("defaultSms", false)
val theme = rxPrefs.getInteger("theme", 0xFF0097A7.toInt())
val night = rxPrefs.getBoolean("night", false)
val nightMode = rxPrefs.getInteger("nightModeSummary", NIGHT_MODE_OFF)
val nightStart = rxPrefs.getString("nightStart", "6:00 PM")
@@ -46,6 +45,15 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
val mms = rxPrefs.getBoolean("mms", true)
val mmsSize = rxPrefs.getInteger("mmsSize", 100)
fun theme(threadId: Long = 0): Preference<Int> {
val default = rxPrefs.getInteger("theme", 0xFF0097A7.toInt())
return when (threadId) {
0L -> default
else -> rxPrefs.getInteger("theme_$threadId", default.get())
}
}
fun notifications(threadId: Long = 0): Preference<Boolean> {
val default = rxPrefs.getBoolean("notifications", true)
@@ -133,8 +133,9 @@ class Navigator @Inject constructor(private val context: Context, private val no
startActivity(intent)
}
fun showThemePicker() {
fun showThemePicker(threadId: Long = 0) {
val intent = Intent(context, ThemePickerActivity::class.java)
intent.putExtra("threadId", threadId)
startActivity(intent)
}
@@ -188,7 +189,7 @@ class Navigator @Inject constructor(private val context: Context, private val no
NotificationPrefsViewModel::class.java -> NotificationPrefsViewModel(intent)
SettingsViewModel::class.java -> SettingsViewModel()
BlockedViewModel::class.java -> BlockedViewModel()
ThemePickerViewModel::class.java -> ThemePickerViewModel()
ThemePickerViewModel::class.java -> ThemePickerViewModel(intent)
else -> throw IllegalArgumentException("Invalid ViewModel class. If this is a new ViewModel, please add it to Navigator.kt")
} as T
}
@@ -26,6 +26,8 @@ import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.util.Colors
import io.reactivex.rxkotlin.Observables
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.toolbar.*
import javax.inject.Inject
@@ -39,6 +41,19 @@ abstract class QkThemedActivity<VM : QkViewModel<*, *>> : QkActivity<VM>() {
@Inject lateinit var colors: Colors
/**
* In case the activity should be themed for a specific conversation, the selected conversation
* can be changed by pushing the threadId to this subject
*/
protected val threadId: Subject<Long> = BehaviorSubject.createDefault(0)
/**
* Switch the theme if the threadId changes
*/
protected val theme = threadId
.distinctUntilChanged()
.switchMap { threadId -> colors.themeForConversation(threadId) }
@SuppressLint("InlinedApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -59,12 +74,12 @@ abstract class QkThemedActivity<VM : QkViewModel<*, *>> : QkActivity<VM>() {
super.onPostCreate(savedInstanceState)
// Update the colours of the menu items
Observables.combineLatest(menu, colors.theme, colors.textTertiary, { menu, theme, textTertiary ->
Observables.combineLatest(menu, theme, colors.textTertiary, { menu, theme, textTertiary ->
(0 until menu.size())
.map { position -> menu.getItem(position) }
.forEach { menuItem ->
menuItem?.icon?.run {
setTint(when(menuItem.itemId) {
setTint(when (menuItem.itemId) {
R.id.info -> textTertiary
else -> theme
})
@@ -23,13 +23,12 @@ import android.graphics.Typeface
import android.support.v7.widget.AppCompatTextView
import android.util.AttributeSet
import com.moez.QKSMS.R
import com.uber.autodispose.android.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.di.appComponent
import common.util.Colors
import common.util.FontProvider
import common.util.extensions.getColorCompat
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import javax.inject.Inject
open class QkTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null)
@@ -47,7 +46,17 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu
@Inject lateinit var colors: Colors
@Inject lateinit var fontProvider: FontProvider
private var textColorObservable: Observable<Int>? = null
var textColorObservable: Observable<Int>? = null
set(value) {
field = value
updateSubscription()
}
private var textColorDisposable: Disposable? = null
set(value) {
field?.dispose()
field = value
}
init {
if (!isInEditMode) {
@@ -83,14 +92,22 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu
}
}
private fun updateSubscription() {
if (isAttachedToWindow) {
textColorDisposable = textColorObservable?.subscribe { color ->
setTextColor(color)
setLinkTextColor(color)
}
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
updateSubscription()
}
textColorObservable
?.autoDisposable(scope())
?.subscribe { color ->
setTextColor(color)
setLinkTextColor(color)
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
textColorDisposable?.dispose()
}
}
@@ -136,14 +136,22 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
intArrayOf(android.R.attr.state_enabled),
intArrayOf(-android.R.attr.state_enabled))
val iconEnabled = threadId
.distinctUntilChanged()
.switchMap { threadId -> colors.textPrimaryOnThemeForConversation(threadId) }
val iconDisabled = threadId
.distinctUntilChanged()
.switchMap { threadId -> colors.textTertiaryOnThemeForConversation(threadId) }
Observables
.combineLatest(colors.textPrimaryOnTheme, colors.textTertiaryOnTheme, { primary, tertiary ->
.combineLatest(iconEnabled, iconDisabled, { primary, tertiary ->
ColorStateList(states, intArrayOf(primary, tertiary))
})
.autoDisposable(scope())
.subscribe { tintList -> sendIcon.imageTintList = tintList }
colors.theme
theme
.autoDisposable(scope())
.subscribe { color -> send.setBackgroundTint(color) }
@@ -184,6 +192,8 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
return
}
threadId.onNext(state.selectedConversation)
toolbarTitle.setVisible(!state.editingMode)
chips.setVisible(state.editingMode)
contacts.setVisible(state.contactsVisible)
@@ -29,6 +29,7 @@ data class ComposeState(
val editingMode: Boolean = false,
val contacts: List<Contact> = ArrayList(),
val contactsVisible: Boolean = false,
val selectedConversation: Long = 0,
val selectedContacts: List<Contact> = ArrayList(),
val title: String = "",
val archived: Boolean = false,
@@ -110,17 +110,17 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
val initialConversation: Observable<Conversation> = when {
threadId != 0L -> {
newState { ComposeState(editingMode = false) }
newState { it.copy(selectedConversation = threadId, editingMode = false) }
messageRepo.getConversationAsync(threadId).asObservable()
}
address.isNotBlank() -> {
newState { ComposeState(editingMode = false) }
newState { it.copy(editingMode = false) }
messageRepo.getOrCreateConversation(address).toObservable()
}
else -> {
newState { ComposeState(editingMode = true) }
newState { it.copy(editingMode = true) }
Observable.empty()
}
}
@@ -129,18 +129,15 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
.doOnNext { contacts -> newState { it.copy(selectedContacts = contacts) } }
// Map the selected contacts to a conversation so that we can display the message history
val selectedConversation = selectedContacts
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single
// stream of conversations. If the conversation was deleted, notify the activity to shut down
conversation = selectedContacts
.skipUntil(state.filter { state -> state.editingMode })
.takeUntil(state.filter { state -> !state.editingMode })
.map { contacts -> contacts.map { it.numbers.firstOrNull()?.address ?: "" } }
.flatMapMaybe { addresses -> messageRepo.getOrCreateConversation(addresses) }
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single
// stream of conversations. If the conversation was deleted, notify the activity to shut down
conversation = initialConversation
.mergeWith(initialConversation)
.filter { conversation -> conversation.isLoaded }
.mergeWith(selectedConversation)
.doOnNext { conversation ->
if (!conversation.isValid) {
newState { it.copy(hasError = true) }
@@ -150,13 +147,13 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
.filter { conversation -> conversation.id != 0L }
.distinctUntilChanged()
// When the conversation changes, update the messages for the adapter
// When the conversation changes, update the threadId and the messages for the adapter
messages = conversation
.distinctUntilChanged { conversation -> conversation.id }
.observeOn(AndroidSchedulers.mainThread())
.map { conversation ->
val messages = messageRepo.getMessages(conversation.id)
newState { it.copy(messages = Pair(conversation, messages)) }
newState { it.copy(selectedConversation = conversation.id, messages = Pair(conversation, messages)) }
messages
}
.switchMap { messages -> messages.asObservable() }
@@ -67,6 +67,12 @@ class MessagesAdapter @Inject constructor(
field = value
people.clear()
contactMap.clear()
// Update the theme
val threadId = value?.first?.id ?: 0
theme = colors.themeForConversation(threadId)
textPrimaryOnTheme = colors.textPrimaryOnThemeForConversation(threadId)
updateData(value?.second)
}
@@ -75,6 +81,9 @@ class MessagesAdapter @Inject constructor(
private val selected = HashMap<Long, Boolean>()
private val disposables = CompositeDisposable()
private var theme = colors.theme
private var textPrimaryOnTheme = colors.textPrimaryOnTheme
/**
* If the viewType is negative, then the viewHolder has an attachment. We'll consider
* this a unique viewType even though it uses the same view, so that regular messages
@@ -88,17 +97,16 @@ class MessagesAdapter @Inject constructor(
val hasThumbnail = viewType < 0
val absViewType = Math.abs(viewType)
when (absViewType) {
VIEW_TYPE_ME -> {
view = layoutInflater.inflate(R.layout.message_list_item_out, parent, false)
disposables += colors.bubble
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
}
else -> {
view = layoutInflater.inflate(R.layout.message_list_item_in, parent, false)
disposables += colors.theme
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
}
if (absViewType == VIEW_TYPE_ME) {
view = layoutInflater.inflate(R.layout.message_list_item_out, parent, false)
disposables += colors.bubble
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
} else {
view = layoutInflater.inflate(R.layout.message_list_item_in, parent, false)
view.subject.textColorObservable = textPrimaryOnTheme
view.body.textColorObservable = textPrimaryOnTheme
disposables += theme
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
}
if (hasThumbnail) {
@@ -36,6 +36,7 @@ class ConversationInfoActivity : QkThemedActivity<ConversationInfoViewModel>(),
override val viewModelClass = ConversationInfoViewModel::class
override val notificationsIntent by lazy { notifications.clicks() }
override val themeIntent by lazy { themePrefs.clicks() }
override val archiveIntent by lazy { archive.clicks() }
override val blockIntent by lazy { block.clicks() }
override val deleteIntent by lazy { delete.clicks() }
@@ -24,6 +24,7 @@ import presentation.common.base.QkView
interface ConversationInfoView : QkView<ConversationInfoState> {
val notificationsIntent: Observable<Unit>
val themeIntent: Observable<Unit>
val archiveIntent: Observable<Unit>
val blockIntent: Observable<Unit>
val deleteIntent: Observable<Unit>
@@ -103,6 +103,12 @@ class ConversationInfoViewModel(intent: Intent) : QkViewModel<ConversationInfoVi
.autoDisposable(view.scope())
.subscribe { conversation -> navigator.showNotificationSettings(conversation.id) }
// Show the theme settings for the conversation
view.themeIntent
.withLatestFrom(conversation, { _, conversation -> conversation })
.autoDisposable(view.scope())
.subscribe { conversation -> navigator.showThemePicker(conversation.id) }
// Toggle the archived state of the conversation
view.archiveIntent
.withLatestFrom(conversation, { _, conversation -> conversation })
@@ -46,8 +46,11 @@ class NotificationPrefsActivity : QkThemedActivity<NotificationPrefsViewModel>()
override val preferenceClickIntent: Subject<PreferenceView> = PublishSubject.create()
override val ringtoneSelectedIntent: Subject<String> = PublishSubject.create()
override fun onCreate(savedInstanceState: Bundle?) {
init {
appComponent.inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.notification_prefs_activity)
setTitle(R.string.title_notification_prefs)
@@ -38,8 +38,11 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
@Inject lateinit var themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
init {
appComponent.inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.theme_picker_activity)
setTitle(R.string.title_theme)
@@ -58,14 +61,11 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
colors.background
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
colors.theme
.autoDisposable(scope())
.subscribe { color -> rgbPicker.setBackgroundColor(color) }
}
override fun render(state: ThemePickerState) {
rgbPicker.alpha = state.rgbAlpha
rgbPicker.setBackgroundColor(state.selectedColor)
themeAdapter.selectedColor = state.selectedColor
}
@@ -18,6 +18,8 @@
*/
package presentation.feature.themepicker
import android.content.Intent
import com.f2prateek.rx.preferences2.Preference
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.di.appComponent
@@ -27,14 +29,19 @@ import presentation.common.base.QkViewModel
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ThemePickerViewModel : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) {
class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) {
@Inject lateinit var prefs: Preferences
private val threadId = intent.extras?.getLong("threadId") ?: 0L
private val theme: Preference<Int>
init {
appComponent.inject(this)
disposables += prefs.theme.asObservable()
theme = prefs.theme(threadId)
disposables += theme.asObservable()
.subscribe { color -> newState { it.copy(selectedColor = color) } }
}
@@ -49,7 +56,7 @@ class ThemePickerViewModel : QkViewModel<ThemePickerView, ThemePickerState>(Them
view.themeSelectedIntent
.autoDisposable(view.scope())
.subscribe { color -> prefs.theme.set(color) }
.subscribe { color -> theme.set(color) }
}
}
@@ -67,6 +67,13 @@
app:icon="@drawable/ic_notifications_black_24dp"
app:title="@string/info_notifications" />
<presentation.common.widget.PreferenceView
android:id="@+id/themePrefs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:icon="@drawable/ic_palette_black_24dp"
app:title="@string/info_theme" />
<presentation.common.widget.PreferenceView
android:id="@+id/archive"
android:layout_width="match_parent"
@@ -88,7 +88,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/mmsPreview"
app:layout_goneMarginBottom="8dp"
app:textColor="primaryOnTheme"
tools:text="@tools:sample/lorem" />
<presentation.common.widget.TightTextView
@@ -108,7 +107,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/subject"
app:layout_goneMarginTop="8dp"
app:textColor="primaryOnTheme"
tools:text="@tools:sample/lorem" />
</android.support.constraint.ConstraintLayout>
+1
View File
@@ -67,6 +67,7 @@
<string name="info_title">Details</string>
<string name="info_notifications">Notifications</string>
<string name="info_theme">Theme</string>
<string name="info_archive">Archive</string>
<string name="info_unarchive">Unarchive</string>
<string name="info_block">Block</string>