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
@@ -29,10 +29,14 @@ import com.moez.QKSMS.R
import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint
import common.util.extensions.within
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.hsv_picker_view.view.*
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)
.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.y = (event.rawY + swatchY + min).within(min, max)
val range = max - min
val hsv = floatArrayOf(hue, (swatch.x - min) / range, 1 - (swatch.y - min) / range)
swatch.setTint(Color.HSVToColor(hsv))
updateSelectedColor()
}
MotionEvent.ACTION_UP -> {
@@ -95,9 +97,7 @@ class HSVPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
hueThumb.x = x
hue = (hueThumb.x - min) / (max - min) * 360
val range = max - min
val hsv = floatArrayOf(hue, (swatch.x - min) / range, 1 - (swatch.y - min) / range)
swatch.setTint(Color.HSVToColor(hsv))
updateSelectedColor()
}
MotionEvent.ACTION_UP -> {
@@ -120,9 +120,18 @@ class HSVPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
}
}
fun setColor(color: Int) {
swatch.setTint(color)
private fun updateSelectedColor() {
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
val hsv = FloatArray(3).apply {
Color.colorToHSV(color, this)
@@ -130,12 +139,16 @@ class HSVPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
}
// Set the position of the swatch
setupBounds()
val range = max - min
post {
setupBounds()
val range = max - min
hueThumb.x = range * hsv[0] / 360 + min
swatch.x = range * hsv[1] + min
swatch.y = range * (1 - hsv[2]) + min
hueThumb.x = range * hsv[0] / 360 + min
swatch.x = range * hsv[1] + min
swatch.y = range * (1 - hsv[2]) + min
updateSelectedColor()
}
}
private fun updateHue() {
@@ -20,10 +20,14 @@ package feature.themepicker
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.base.QkThemedActivity
import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint
import common.util.extensions.setVisible
import injection.appComponent
import kotlinx.android.synthetic.main.theme_picker_activity.*
import kotlinx.android.synthetic.main.theme_picker_hsv.*
@@ -34,6 +38,9 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
override val viewModelClass = ThemePickerViewModel::class
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 themePagerAdapter: ThemePagerAdapter
@@ -61,12 +68,21 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
colors.background
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
colors.textSecondary
.autoDisposable(scope())
.subscribe { color -> clear.setTint(color) }
}
override fun render(state: ThemePickerState) {
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(
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> {
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.kotlin.autoDisposable
import common.base.QkViewModel
import common.util.Colors
import injection.appComponent
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.withLatestFrom
import util.Preferences
import javax.inject.Inject
class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) {
@Inject lateinit var colors: Colors
@Inject lateinit var prefs: Preferences
private val threadId = intent.extras?.getLong("threadId") ?: 0L
@@ -41,17 +44,44 @@ class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemeP
newState { it.copy(threadId = threadId) }
theme = prefs.theme(threadId)
disposables += theme.asObservable()
.subscribe { color -> newState { it.copy(selectedColor = color) } }
}
override fun bindView(view: ThemePickerView) {
super.bindView(view)
theme.asObservable()
.autoDisposable(view.scope())
.subscribe { color -> view.setCurrentTheme(color) }
// Update the theme when a material theme is clicked
view.themeSelectedIntent
.autoDisposable(view.scope())
.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) }
}
}