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.os.Bundle
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import android.view.MotionEvent
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.dpToPx
import common.util.extensions.setBackgroundTint
import common.util.extensions.within
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_hsl.* 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 themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter @Inject lateinit var themePagerAdapter: ThemePagerAdapter
private val swatchPadding by lazy { 18.dpToPx(this) }
init { init {
appComponent.inject(this) appComponent.inject(this)
} }
@@ -67,36 +61,12 @@ 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) }
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) { override fun render(state: ThemePickerState) {
themeAdapter.threadId = state.threadId themeAdapter.threadId = state.threadId
themeAdapter.selectedColor = state.selectedColor themeAdapter.selectedColor = state.selectedColor
saturation.setBackgroundTint(state.hue) picker.setColor(state.selectedColor)
swatchPreview.setBackgroundTint(state.selectedColor)
} }
} }
@@ -20,6 +20,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 hue: Int = -1
) )
@@ -19,7 +19,6 @@
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
@@ -44,16 +43,7 @@ class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemeP
theme = prefs.theme(threadId) theme = prefs.theme(threadId)
disposables += theme.asObservable() disposables += theme.asObservable()
.doOnNext { color -> newState { it.copy(selectedColor = color) } } .subscribe { 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) {
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<merge 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="wrap_content"
tools:parentTag="feature.themepicker.HSLPickerView">
<View
android:id="@+id/saturation"
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/saturation"
app:layout_constraintEnd_toEndOf="@id/saturation"
app:layout_constraintStart_toStartOf="@id/saturation"
app:layout_constraintTop_toTopOf="@id/saturation" />
<FrameLayout
android:id="@+id/swatch"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_margin="18dp"
android:background="@drawable/circle"
android:padding="1dp"
app:layout_constraintBottom_toBottomOf="@id/saturation"
app:layout_constraintEnd_toEndOf="@id/saturation"
app:layout_constraintStart_toStartOf="@id/saturation"
app:layout_constraintTop_toTopOf="@id/saturation">
<View
android:id="@+id/swatchPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/circle"
android:backgroundTint="@color/tools_theme" />
</FrameLayout>
</merge>
@@ -1,54 +1,15 @@
<?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">
<View <feature.themepicker.HSLPickerView
android:id="@+id/saturation" android:id="@+id/picker"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="wrap_content"
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_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="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/saturation"
app:layout_constraintEnd_toEndOf="@id/saturation"
app:layout_constraintStart_toStartOf="@id/saturation"
app:layout_constraintTop_toTopOf="@id/saturation" />
<FrameLayout
android:id="@+id/swatch"
android:layout_width="12dp"
android:layout_height="12dp"
android:background="@drawable/circle"
android:padding="1dp"
android:layout_margin="18dp"
app:layout_constraintBottom_toBottomOf="@id/saturation"
app:layout_constraintEnd_toEndOf="@id/saturation"
app:layout_constraintStart_toStartOf="@id/saturation"
app:layout_constraintTop_toTopOf="@id/saturation">
<View
android:id="@+id/swatchPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/circle"
android:backgroundTint="@color/tools_theme" />
</FrameLayout>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>