diff --git a/presentation/src/main/java/feature/themepicker/HSLPickerView.kt b/presentation/src/main/java/feature/themepicker/HSLPickerView.kt new file mode 100644 index 00000000..0bb70c83 --- /dev/null +++ b/presentation/src/main/java/feature/themepicker/HSLPickerView.kt @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +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)) + } + +} \ No newline at end of file diff --git a/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt b/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt index 075ce03f..fb912c3d 100644 --- a/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt +++ b/presentation/src/main/java/feature/themepicker/ThemePickerActivity.kt @@ -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(), 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(), 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) } } \ 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 e946f81b..c6333312 100644 --- a/presentation/src/main/java/feature/themepicker/ThemePickerState.kt +++ b/presentation/src/main/java/feature/themepicker/ThemePickerState.kt @@ -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 ) \ 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 986f9b5d..f20158ef 100644 --- a/presentation/src/main/java/feature/themepicker/ThemePickerViewModel.kt +++ b/presentation/src/main/java/feature/themepicker/ThemePickerViewModel.kt @@ -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 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) { diff --git a/presentation/src/main/res/layout/hsl_picker_view.xml b/presentation/src/main/res/layout/hsl_picker_view.xml new file mode 100644 index 00000000..9336e376 --- /dev/null +++ b/presentation/src/main/res/layout/hsl_picker_view.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/presentation/src/main/res/layout/theme_picker_hsl.xml b/presentation/src/main/res/layout/theme_picker_hsl.xml index 19243d22..abf18c11 100644 --- a/presentation/src/main/res/layout/theme_picker_hsl.xml +++ b/presentation/src/main/res/layout/theme_picker_hsl.xml @@ -1,54 +1,15 @@ - - - - - - - - - + app:layout_constraintTop_toTopOf="parent" /> \ No newline at end of file