Add ability to apply custom color

This commit is contained in:
Moez Bhatti
2018-03-27 04:05:57 -04:00
parent bb8b2e6cdc
commit 8fc19bd355
9 changed files with 141 additions and 22 deletions
@@ -194,6 +194,9 @@ class Colors @Inject constructor(private val context: Context, private val prefs
.distinctUntilChanged() .distinctUntilChanged()
fun textPrimaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId) fun textPrimaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.switchMap { color -> textPrimaryOnThemeForColor(color) }
fun textPrimaryOnThemeForColor(color: Int): Observable<Int> = Observable.just(color)
.map { theme -> measureLuminance(theme) } .map { theme -> measureLuminance(theme) }
.map { themeLuminance -> primaryTextLuminance / themeLuminance } .map { themeLuminance -> primaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio } .map { contrastRatio -> contrastRatio < minimumContrastRatio }
@@ -202,6 +205,9 @@ class Colors @Inject constructor(private val context: Context, private val prefs
.distinctUntilChanged() .distinctUntilChanged()
fun textSecondaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId) fun textSecondaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.switchMap { color -> textSecondaryOnThemeForColor(color) }
fun textSecondaryOnThemeForColor(color: Int): Observable<Int> = Observable.just(color)
.map { theme -> measureLuminance(theme) } .map { theme -> measureLuminance(theme) }
.map { themeLuminance -> secondaryTextLuminance / themeLuminance } .map { themeLuminance -> secondaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio } .map { contrastRatio -> contrastRatio < minimumContrastRatio }
@@ -210,6 +216,9 @@ class Colors @Inject constructor(private val context: Context, private val prefs
.distinctUntilChanged() .distinctUntilChanged()
fun textTertiaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId) fun textTertiaryOnThemeForConversation(threadId: Long = 0): Observable<Int> = themeForConversation(threadId)
.switchMap { color -> textTertiaryOnThemeForColor(color) }
fun textTertiaryOnThemeForColor(color: Int): Observable<Int> = Observable.just(color)
.map { theme -> measureLuminance(theme) } .map { theme -> measureLuminance(theme) }
.map { themeLuminance -> tertiaryTextLuminance / themeLuminance } .map { themeLuminance -> tertiaryTextLuminance / themeLuminance }
.map { contrastRatio -> contrastRatio < minimumContrastRatio } .map { contrastRatio -> contrastRatio < minimumContrastRatio }
@@ -29,10 +29,14 @@ import com.moez.QKSMS.R
import common.util.extensions.setBackgroundTint import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint import common.util.extensions.setTint
import common.util.extensions.within import common.util.extensions.within
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.hsv_picker_view.view.* import kotlinx.android.synthetic.main.hsv_picker_view.view.*
class HSVPickerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) { class HSVPickerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) {
val selectedColor: Subject<Int> = BehaviorSubject.create()
private val hues = arrayOf(0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000) private val hues = arrayOf(0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000)
.map { it.toInt() }.toIntArray() .map { it.toInt() }.toIntArray()
@@ -65,9 +69,7 @@ class HSVPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
swatch.x = (event.rawX + swatchX + min).within(min, max) swatch.x = (event.rawX + swatchX + min).within(min, max)
swatch.y = (event.rawY + swatchY + min).within(min, max) swatch.y = (event.rawY + swatchY + min).within(min, max)
val range = max - min updateSelectedColor()
val hsv = floatArrayOf(hue, (swatch.x - min) / range, 1 - (swatch.y - min) / range)
swatch.setTint(Color.HSVToColor(hsv))
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
@@ -95,9 +97,7 @@ class HSVPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
hueThumb.x = x hueThumb.x = x
hue = (hueThumb.x - min) / (max - min) * 360 hue = (hueThumb.x - min) / (max - min) * 360
val range = max - min updateSelectedColor()
val hsv = floatArrayOf(hue, (swatch.x - min) / range, 1 - (swatch.y - min) / range)
swatch.setTint(Color.HSVToColor(hsv))
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
@@ -120,9 +120,18 @@ class HSVPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
} }
} }
fun setColor(color: Int) { private fun updateSelectedColor() {
swatch.setTint(color) setupBounds()
val range = max - min
val hsv = floatArrayOf(hue, (swatch.x - min) / range, 1 - (swatch.y - min) / range)
val color = Color.HSVToColor(hsv)
swatch.setTint(color)
selectedColor.onNext(color)
}
fun setColor(color: Int) {
// Convert the rgb color to HSV // Convert the rgb color to HSV
val hsv = FloatArray(3).apply { val hsv = FloatArray(3).apply {
Color.colorToHSV(color, this) Color.colorToHSV(color, this)
@@ -130,12 +139,16 @@ class HSVPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
} }
// Set the position of the swatch // Set the position of the swatch
setupBounds() post {
val range = max - min setupBounds()
val range = max - min
hueThumb.x = range * hsv[0] / 360 + min hueThumb.x = range * hsv[0] / 360 + min
swatch.x = range * hsv[1] + min swatch.x = range * hsv[1] + min
swatch.y = range * (1 - hsv[2]) + min swatch.y = range * (1 - hsv[2]) + min
updateSelectedColor()
}
} }
private fun updateHue() { private fun updateHue() {
@@ -20,10 +20,14 @@ package feature.themepicker
import android.os.Bundle import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable import com.uber.autodispose.kotlin.autoDisposable
import common.base.QkThemedActivity import common.base.QkThemedActivity
import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint
import common.util.extensions.setVisible
import injection.appComponent import injection.appComponent
import kotlinx.android.synthetic.main.theme_picker_activity.* import kotlinx.android.synthetic.main.theme_picker_activity.*
import kotlinx.android.synthetic.main.theme_picker_hsv.* import kotlinx.android.synthetic.main.theme_picker_hsv.*
@@ -34,6 +38,9 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
override val viewModelClass = ThemePickerViewModel::class override val viewModelClass = ThemePickerViewModel::class
override val themeSelectedIntent by lazy { themeAdapter.colorSelected } override val themeSelectedIntent by lazy { themeAdapter.colorSelected }
override val hsvThemeSelectedIntent by lazy { picker.selectedColor }
override val hsvThemeClearedIntent by lazy { clear.clicks() }
override val hsvThemeAppliedIntent by lazy { apply.clicks() }
@Inject lateinit var themeAdapter: ThemeAdapter @Inject lateinit var themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter @Inject lateinit var themePagerAdapter: ThemePagerAdapter
@@ -61,12 +68,21 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
colors.background colors.background
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) } .subscribe { color -> window.decorView.setBackgroundColor(color) }
colors.textSecondary
.autoDisposable(scope())
.subscribe { color -> clear.setTint(color) }
} }
override fun render(state: ThemePickerState) { override fun render(state: ThemePickerState) {
themeAdapter.threadId = state.threadId themeAdapter.threadId = state.threadId
themeAdapter.selectedColor = state.selectedColor
picker.setColor(state.selectedColor) applyGroup.setVisible(state.applyThemeVisible)
apply.setBackgroundTint(state.newColor)
apply.setTextColor(state.newTextColor)
}
override fun setCurrentTheme(color: Int) {
picker.setColor(color)
} }
} }
@@ -20,5 +20,7 @@ package feature.themepicker
data class ThemePickerState( data class ThemePickerState(
val threadId: Long = 0, val threadId: Long = 0,
val selectedColor: Int = -1 val applyThemeVisible: Boolean = false,
val newColor: Int = -1,
val newTextColor: Int = -1
) )
@@ -24,5 +24,10 @@ import io.reactivex.Observable
interface ThemePickerView : QkView<ThemePickerState> { interface ThemePickerView : QkView<ThemePickerState> {
val themeSelectedIntent: Observable<Int> val themeSelectedIntent: Observable<Int>
val hsvThemeSelectedIntent: Observable<Int>
val hsvThemeClearedIntent: Observable<*>
val hsvThemeAppliedIntent: Observable<*>
fun setCurrentTheme(color: Int)
} }
@@ -23,13 +23,16 @@ import com.f2prateek.rx.preferences2.Preference
import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable import com.uber.autodispose.kotlin.autoDisposable
import common.base.QkViewModel import common.base.QkViewModel
import common.util.Colors
import injection.appComponent import injection.appComponent
import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.withLatestFrom
import util.Preferences import util.Preferences
import javax.inject.Inject import javax.inject.Inject
class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) { class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) {
@Inject lateinit var colors: Colors
@Inject lateinit var prefs: Preferences @Inject lateinit var prefs: Preferences
private val threadId = intent.extras?.getLong("threadId") ?: 0L private val threadId = intent.extras?.getLong("threadId") ?: 0L
@@ -41,17 +44,44 @@ class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemeP
newState { it.copy(threadId = threadId) } newState { it.copy(threadId = threadId) }
theme = prefs.theme(threadId) theme = prefs.theme(threadId)
disposables += theme.asObservable()
.subscribe { color -> newState { it.copy(selectedColor = color) } }
} }
override fun bindView(view: ThemePickerView) { override fun bindView(view: ThemePickerView) {
super.bindView(view) super.bindView(view)
theme.asObservable()
.autoDisposable(view.scope())
.subscribe { color -> view.setCurrentTheme(color) }
// Update the theme when a material theme is clicked
view.themeSelectedIntent view.themeSelectedIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { color -> theme.set(color) } .subscribe { color -> theme.set(color) }
// Update the color of the apply button
view.hsvThemeSelectedIntent
.doOnNext { color -> newState { it.copy(newColor = color) } }
.switchMap { color -> colors.textPrimaryOnThemeForColor(color) }
.doOnNext { color -> newState { it.copy(newTextColor = color) } }
.autoDisposable(view.scope())
.subscribe()
// Toggle the visibility of the apply group
Observables.combineLatest(theme.asObservable(), view.hsvThemeSelectedIntent, { old, new -> old != new })
.autoDisposable(view.scope())
.subscribe { themeChanged -> newState { it.copy(applyThemeVisible = themeChanged) } }
// Update the theme, when apply is clicked
view.hsvThemeAppliedIntent
.withLatestFrom(view.hsvThemeSelectedIntent, { _, color -> color })
.autoDisposable(view.scope())
.subscribe { color -> theme.set(color) }
// Reset the theme
view.hsvThemeClearedIntent
.withLatestFrom(theme.asObservable(), { _, color -> color })
.autoDisposable(view.scope())
.subscribe { color -> view.setCurrentTheme(color) }
} }
} }
@@ -49,7 +49,9 @@
android:elevation="4dp" android:elevation="4dp"
android:padding="2dp" android:padding="2dp"
android:src="@drawable/circle" android:src="@drawable/circle"
tools:tint="@color/tools_theme" /> tools:tint="@color/tools_theme"
tools:translationX="240dp"
tools:translationY="120dp" />
<FrameLayout <FrameLayout
android:id="@+id/hueGroup" android:id="@+id/hueGroup"
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:background="@color/white">
<feature.themepicker.HSVPickerView <feature.themepicker.HSVPickerView
android:id="@+id/picker" android:id="@+id/picker"
@@ -12,4 +15,42 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Group
android:id="@+id/applyGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="applyDivider, clear, apply" />
<common.widget.Separator
android:id="@+id/applyDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintBottom_toTopOf="@id/clear" />
<ImageView
android:id="@+id/clear"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="@drawable/ripple"
android:padding="16dp"
android:src="@drawable/ic_cancel_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:tint="@color/textSecondary" />
<common.widget.QkTextView
android:id="@+id/apply"
style="@style/TextButton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_rectangle_2dp"
android:elevation="2dp"
android:text="@string/theme_apply"
app:layout_constraintBottom_toBottomOf="@id/clear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/clear"
tools:backgroundTint="@color/tools_theme"
tools:textColor="@color/textPrimaryDark" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
@@ -142,6 +142,7 @@
<string name="theme_material">Material</string> <string name="theme_material">Material</string>
<string name="theme_rgb">RGB</string> <string name="theme_rgb">RGB</string>
<string name="theme_apply">Apply</string>
<string name="notification_reply">Reply</string> <string name="notification_reply">Reply</string>
<string name="notification_read">Mark read</string> <string name="notification_read">Mark read</string>