Move hsl picker logic to custom view

This commit is contained in:
Moez Bhatti
2018-03-24 02:34:40 -04:00
parent d523d45a90
commit 65a8a51c8a
6 changed files with 135 additions and 87 deletions
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package feature.themepicker
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.v4.graphics.ColorUtils
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.moez.QKSMS.R
import common.util.extensions.setBackgroundTint
import common.util.extensions.within
import kotlinx.android.synthetic.main.hsl_picker_view.view.*
class HSLPickerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) {
private var swatchX = 0f
private var swatchY = 0f
init {
View.inflate(context, R.layout.hsl_picker_view, this)
saturation.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
swatchX = event.x - event.rawX
swatchY = event.y - event.rawY
}
MotionEvent.ACTION_MOVE -> {
val min = saturation.x - swatch.width / 2
val max = min + saturation.width
swatch.x = (event.rawX + swatchX + min).within(min, max)
swatch.y = (event.rawY + swatchY + min).within(min, max)
}
else -> return@setOnTouchListener false
}
true
}
}
fun setColor(color: Int) {
swatchPreview.setBackgroundTint(color)
val hsl = FloatArray(3).apply {
ColorUtils.colorToHSL(color, this)
this[1] = 1f
this[2] = 0.5f
}
saturation.setBackgroundTint(ColorUtils.HSLToColor(hsl))
}
}
@@ -20,14 +20,10 @@ package feature.themepicker
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.MotionEvent
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.dpToPx
import common.util.extensions.setBackgroundTint
import common.util.extensions.within
import injection.appComponent
import kotlinx.android.synthetic.main.theme_picker_activity.*
import kotlinx.android.synthetic.main.theme_picker_hsl.*
@@ -42,8 +38,6 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
@Inject lateinit var themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter
private val swatchPadding by lazy { 18.dpToPx(this) }
init {
appComponent.inject(this)
}
@@ -67,36 +61,12 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
colors.background
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
var dX = 0f
var dY = 0f
saturation.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
dX = event.x - event.rawX
dY = event.y - event.rawY
}
MotionEvent.ACTION_MOVE -> {
val min = saturation.x - swatch.width / 2
val max = min + saturation.width
swatch.x = (event.rawX + dX + swatchPadding).within(min, max)
swatch.y = (event.rawY + dY + swatchPadding).within(min, max)
}
else -> return@setOnTouchListener false
}
true
}
}
override fun render(state: ThemePickerState) {
themeAdapter.threadId = state.threadId
themeAdapter.selectedColor = state.selectedColor
saturation.setBackgroundTint(state.hue)
swatchPreview.setBackgroundTint(state.selectedColor)
picker.setColor(state.selectedColor)
}
}
@@ -20,6 +20,5 @@ package feature.themepicker
data class ThemePickerState(
val threadId: Long = 0,
val selectedColor: Int = -1,
val hue: Int = -1
val selectedColor: Int = -1
)
@@ -19,7 +19,6 @@
package feature.themepicker
import android.content.Intent
import android.support.v4.graphics.ColorUtils
import com.f2prateek.rx.preferences2.Preference
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
@@ -44,16 +43,7 @@ class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemeP
theme = prefs.theme(threadId)
disposables += theme.asObservable()
.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) } }
.subscribe { color -> newState { it.copy(selectedColor = color) } }
}
override fun bindView(view: ThemePickerView) {