From 8fc19bd35531750c6080f34179d90b72e4739d1e Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Tue, 27 Mar 2018 04:05:57 -0400 Subject: [PATCH] Add ability to apply custom color --- .../src/main/java/common/util/Colors.kt | 9 ++++ .../java/feature/themepicker/HSVPickerView.kt | 39 +++++++++++------ .../themepicker/ThemePickerActivity.kt | 20 ++++++++- .../feature/themepicker/ThemePickerState.kt | 4 +- .../feature/themepicker/ThemePickerView.kt | 5 +++ .../themepicker/ThemePickerViewModel.kt | 38 ++++++++++++++-- .../src/main/res/layout/hsv_picker_view.xml | 4 +- .../src/main/res/layout/theme_picker_hsv.xml | 43 ++++++++++++++++++- presentation/src/main/res/values/strings.xml | 1 + 9 files changed, 141 insertions(+), 22 deletions(-) diff --git a/presentation/src/main/java/common/util/Colors.kt b/presentation/src/main/java/common/util/Colors.kt index 299c8540..306f62ef 100644 --- a/presentation/src/main/java/common/util/Colors.kt +++ b/presentation/src/main/java/common/util/Colors.kt @@ -194,6 +194,9 @@ class Colors @Inject constructor(private val context: Context, private val prefs .distinctUntilChanged() fun textPrimaryOnThemeForConversation(threadId: Long = 0): Observable = themeForConversation(threadId) + .switchMap { color -> textPrimaryOnThemeForColor(color) } + + fun textPrimaryOnThemeForColor(color: Int): Observable = Observable.just(color) .map { theme -> measureLuminance(theme) } .map { themeLuminance -> primaryTextLuminance / themeLuminance } .map { contrastRatio -> contrastRatio < minimumContrastRatio } @@ -202,6 +205,9 @@ class Colors @Inject constructor(private val context: Context, private val prefs .distinctUntilChanged() fun textSecondaryOnThemeForConversation(threadId: Long = 0): Observable = themeForConversation(threadId) + .switchMap { color -> textSecondaryOnThemeForColor(color) } + + fun textSecondaryOnThemeForColor(color: Int): Observable = Observable.just(color) .map { theme -> measureLuminance(theme) } .map { themeLuminance -> secondaryTextLuminance / themeLuminance } .map { contrastRatio -> contrastRatio < minimumContrastRatio } @@ -210,6 +216,9 @@ class Colors @Inject constructor(private val context: Context, private val prefs .distinctUntilChanged() fun textTertiaryOnThemeForConversation(threadId: Long = 0): Observable = themeForConversation(threadId) + .switchMap { color -> textTertiaryOnThemeForColor(color) } + + fun textTertiaryOnThemeForColor(color: Int): Observable = Observable.just(color) .map { theme -> measureLuminance(theme) } .map { themeLuminance -> tertiaryTextLuminance / themeLuminance } .map { contrastRatio -> contrastRatio < minimumContrastRatio } diff --git a/presentation/src/main/java/feature/themepicker/HSVPickerView.kt b/presentation/src/main/java/feature/themepicker/HSVPickerView.kt index 92e49e77..cff247c9 100644 --- a/presentation/src/main/java/feature/themepicker/HSVPickerView.kt +++ b/presentation/src/main/java/feature/themepicker/HSVPickerView.kt @@ -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 = 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() { diff --git a/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt b/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt index 2987996b..c3d2711f 100644 --- a/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt +++ b/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt @@ -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(), 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(), 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) } } \ No newline at end of file diff --git a/presentation/src/main/java/feature/themepicker/ThemePickerState.kt b/presentation/src/main/java/feature/themepicker/ThemePickerState.kt index c6333312..20ad0d31 100644 --- a/presentation/src/main/java/feature/themepicker/ThemePickerState.kt +++ b/presentation/src/main/java/feature/themepicker/ThemePickerState.kt @@ -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 ) \ No newline at end of file diff --git a/presentation/src/main/java/feature/themepicker/ThemePickerView.kt b/presentation/src/main/java/feature/themepicker/ThemePickerView.kt index 21931266..5d3e6bd9 100644 --- a/presentation/src/main/java/feature/themepicker/ThemePickerView.kt +++ b/presentation/src/main/java/feature/themepicker/ThemePickerView.kt @@ -24,5 +24,10 @@ import io.reactivex.Observable interface ThemePickerView : QkView { val themeSelectedIntent: Observable + val hsvThemeSelectedIntent: Observable + val hsvThemeClearedIntent: Observable<*> + val hsvThemeAppliedIntent: Observable<*> + + fun setCurrentTheme(color: Int) } \ No newline at end of file diff --git a/presentation/src/main/java/feature/themepicker/ThemePickerViewModel.kt b/presentation/src/main/java/feature/themepicker/ThemePickerViewModel.kt index f20158ef..156a26fe 100644 --- a/presentation/src/main/java/feature/themepicker/ThemePickerViewModel.kt +++ b/presentation/src/main/java/feature/themepicker/ThemePickerViewModel.kt @@ -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(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 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) } } } \ No newline at end of file diff --git a/presentation/src/main/res/layout/hsv_picker_view.xml b/presentation/src/main/res/layout/hsv_picker_view.xml index ed1d0b5d..b6a1b361 100644 --- a/presentation/src/main/res/layout/hsv_picker_view.xml +++ b/presentation/src/main/res/layout/hsv_picker_view.xml @@ -49,7 +49,9 @@ android:elevation="4dp" android:padding="2dp" android:src="@drawable/circle" - tools:tint="@color/tools_theme" /> + tools:tint="@color/tools_theme" + tools:translationX="240dp" + tools:translationY="120dp" /> + android:layout_height="match_parent" + android:animateLayoutChanges="true" + tools:background="@color/white"> + + + + + + + + \ No newline at end of file diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index 88a180d7..61c54669 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -142,6 +142,7 @@ Material RGB + Apply Reply Mark read