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
@@ -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) }
}
}