Show HSL palette in QKSMS+ theme view

This commit is contained in:
Moez Bhatti
2018-03-23 14:42:42 -04:00
parent 436e514c33
commit 875dfbca0f
7 changed files with 69 additions and 30 deletions
@@ -23,17 +23,17 @@ import android.support.v7.widget.LinearLayoutManager
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 injection.appComponent
import common.util.extensions.pageScrolled
import kotlinx.android.synthetic.main.theme_picker_activity.*
import common.base.QkThemedActivity import common.base.QkThemedActivity
import common.util.extensions.setBackgroundTint
import injection.appComponent
import kotlinx.android.synthetic.main.theme_picker_activity.*
import kotlinx.android.synthetic.main.theme_picker_hsl.*
import javax.inject.Inject import javax.inject.Inject
class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePickerView { class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePickerView {
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 pageScrolledIntent by lazy { pager.pageScrolled() }
@Inject lateinit var themeAdapter: ThemeAdapter @Inject lateinit var themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter @Inject lateinit var themePagerAdapter: ThemePagerAdapter
@@ -64,8 +64,7 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
} }
override fun render(state: ThemePickerState) { override fun render(state: ThemePickerState) {
rgbPicker.alpha = state.rgbAlpha hue.setBackgroundTint(state.hue)
rgbPicker.setBackgroundColor(state.selectedColor)
themeAdapter.threadId = state.threadId themeAdapter.threadId = state.threadId
themeAdapter.selectedColor = state.selectedColor themeAdapter.selectedColor = state.selectedColor
@@ -21,5 +21,5 @@ package feature.themepicker
data class ThemePickerState( data class ThemePickerState(
val threadId: Long = 0, val threadId: Long = 0,
val selectedColor: Int = -1, val selectedColor: Int = -1,
val rgbAlpha: Float = 0f val hue: Int = -1
) )
@@ -18,14 +18,11 @@
*/ */
package feature.themepicker package feature.themepicker
import common.util.extensions.PageScroll
import io.reactivex.Observable
import common.base.QkView import common.base.QkView
import feature.themepicker.ThemePickerState import io.reactivex.Observable
interface ThemePickerView : QkView<ThemePickerState> { interface ThemePickerView : QkView<ThemePickerState> {
val pageScrolledIntent: Observable<PageScroll>
val themeSelectedIntent: Observable<Int> val themeSelectedIntent: Observable<Int>
} }
@@ -19,16 +19,14 @@
package feature.themepicker package feature.themepicker
import android.content.Intent import android.content.Intent
import android.support.v4.graphics.ColorUtils
import com.f2prateek.rx.preferences2.Preference 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 injection.appComponent
import util.Preferences
import io.reactivex.rxkotlin.plusAssign
import common.base.QkViewModel import common.base.QkViewModel
import feature.themepicker.ThemePickerState import injection.appComponent
import feature.themepicker.ThemePickerView import io.reactivex.rxkotlin.plusAssign
import java.util.concurrent.TimeUnit 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()) {
@@ -46,18 +44,21 @@ class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemeP
theme = prefs.theme(threadId) theme = prefs.theme(threadId)
disposables += theme.asObservable() disposables += theme.asObservable()
.subscribe { color -> newState { it.copy(selectedColor = color) } } .doOnNext { color -> newState { it.copy(selectedColor = color) } }
.map { color -> FloatArray(3).apply { ColorUtils.colorToHSL(color, this) } }
.map { hsl ->
hsl.apply {
this[1] = 1f
this[2] = 0.5f
}
}
.map { hsl -> ColorUtils.HSLToColor(hsl) }
.subscribe { hue -> newState { it.copy(hue = hue) } }
} }
override fun bindView(view: ThemePickerView) { override fun bindView(view: ThemePickerView) {
super.bindView(view) super.bindView(view)
view.pageScrolledIntent
.throttleFirst(16, TimeUnit.MILLISECONDS)
.map { (position, positionOffset) -> if (position == 0) positionOffset * positionOffset else 1f }
.autoDisposable(view.scope())
.subscribe { alpha -> newState { it.copy(rgbAlpha = alpha) } }
view.themeSelectedIntent view.themeSelectedIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { color -> theme.set(color) } .subscribe { color -> theme.set(color) }
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="4dp" />
<gradient
android:endColor="#ff000000"
android:startColor="#00000000" />
</shape>
@@ -29,9 +29,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:orientation="horizontal" android:orientation="horizontal"
android:visibility="gone"
android:paddingEnd="12dp" android:paddingEnd="12dp"
android:paddingStart="12dp" /> android:paddingStart="12dp"
android:visibility="gone" />
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -48,12 +48,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
<FrameLayout <include
android:id="@+id/rgbPicker" android:id="@+id/rgbPicker"
layout="@layout/theme_picker_hsl"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent" />
</FrameLayout>
</android.support.v4.view.ViewPager> </android.support.v4.view.ViewPager>
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/hue"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="@drawable/color_picker_gradient"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:backgroundTint="@color/tools_theme" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/color_picker_gradient"
android:rotation="90"
app:layout_constraintBottom_toBottomOf="@id/hue"
app:layout_constraintEnd_toEndOf="@id/hue"
app:layout_constraintStart_toStartOf="@id/hue"
app:layout_constraintTop_toTopOf="@id/hue" />
</android.support.constraint.ConstraintLayout>