Add recent colors for color picker

Store 5 recently used colors for ColorPickerDialog, number is configured by `RECENT_COLORS_NUMBER` in `ColorPickerDialog`.
Client apps need to pass `showRecentColors = true` if they want these colors to be displayed.
This commit is contained in:
Andrii Chubko 2021-09-13 03:40:02 +03:00
parent 554dda71a4
commit 01be2f00c5
5 changed files with 101 additions and 19 deletions

View file

@ -14,19 +14,30 @@ import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.views.ColorPickerSquare
import kotlinx.android.synthetic.main.dialog_color_picker.view.*
import java.util.*
private const val RECENT_COLORS_NUMBER = 5
// forked from https://github.com/yukuku/ambilwarna
class ColorPickerDialog(val activity: Activity, color: Int, val removeDimmedBackground: Boolean = false, showUseDefaultButton: Boolean = false,
val currentColorCallback: ((color: Int) -> Unit)? = null, val callback: (wasPositivePressed: Boolean, color: Int) -> Unit) {
lateinit var viewHue: View
lateinit var viewSatVal: ColorPickerSquare
lateinit var viewCursor: ImageView
lateinit var viewNewColor: ImageView
lateinit var viewTarget: ImageView
lateinit var newHexField: EditText
lateinit var viewContainer: ViewGroup
class ColorPickerDialog(
val activity: Activity,
color: Int,
showRecentColors: Boolean = false,
val removeDimmedBackground: Boolean = false,
showUseDefaultButton: Boolean = false,
val currentColorCallback: ((color: Int) -> Unit)? = null,
val callback: (wasPositivePressed: Boolean, color: Int) -> Unit
) {
var viewHue: View
var viewSatVal: ColorPickerSquare
var viewCursor: ImageView
var viewNewColor: ImageView
var viewTarget: ImageView
var newHexField: EditText
var viewContainer: ViewGroup
private val baseConfig = activity.baseConfig
private val currentColorHsv = FloatArray(3)
private val backgroundColor = activity.baseConfig.backgroundColor
private val backgroundColor = baseConfig.backgroundColor
private val cornerRadius = activity.getCornerRadius()
private var isHueBeingDragged = false
private var wasDimmedBackgroundRemoved = false
@ -57,6 +68,10 @@ class ColorPickerDialog(val activity: Activity, color: Int, val removeDimmedBack
true
}
newHexField.setText(hexCode)
if (showRecentColors) {
setupRecentColors()
}
}
viewHue.setOnTouchListener(OnTouchListener { v, event ->
@ -125,7 +140,7 @@ class ColorPickerDialog(val activity: Activity, color: Int, val removeDimmedBack
}
}
val textColor = activity.baseConfig.textColor
val textColor = baseConfig.textColor
val builder = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { dialog, which -> confirmNewColor() }
.setNegativeButton(R.string.cancel) { dialog, which -> dialogDismissed() }
@ -149,21 +164,57 @@ class ColorPickerDialog(val activity: Activity, color: Int, val removeDimmedBack
}
}
private fun View.setupRecentColors() {
val recentColors = baseConfig.colorPickerRecentColors
if (recentColors.isNotEmpty()) {
recent_colors.beVisible()
val squareSize = context.resources.getDimensionPixelSize(R.dimen.normal_icon_size)
recentColors.take(RECENT_COLORS_NUMBER).forEach { recentColor ->
val recentColorView = ImageView(context)
recentColorView.id = View.generateViewId()
recentColorView.layoutParams = ViewGroup.LayoutParams(squareSize, squareSize)
recentColorView.setFillWithStroke(recentColor, backgroundColor)
recentColorView.setOnClickListener { newHexField.setText(getHexCode(recentColor)) }
recent_colors.addView(recentColorView)
recent_colors_flow.addView(recentColorView)
}
}
}
private fun dialogDismissed() {
callback(false, 0)
}
private fun confirmNewColor() {
val hexValue = newHexField.value
if (hexValue.length == 6) {
callback(true, Color.parseColor("#$hexValue"))
val newColor = if (hexValue.length == 6) {
Color.parseColor("#$hexValue")
} else {
callback(true, getColor())
getColor()
}
addRecentColor(newColor)
callback(true, newColor)
}
private fun useDefault() {
callback(true, activity.baseConfig.defaultNavigationBarColor)
val defaultColor = baseConfig.defaultNavigationBarColor
addRecentColor(defaultColor)
callback(true, defaultColor)
}
private fun addRecentColor(color: Int) {
var recentColors = baseConfig.colorPickerRecentColors
recentColors.remove(color)
if (recentColors.size >= RECENT_COLORS_NUMBER) {
val numberOfColorsToDrop = recentColors.size - RECENT_COLORS_NUMBER + 1
recentColors = LinkedList(recentColors.dropLast(numberOfColorsToDrop))
}
recentColors.addFirst(color)
baseConfig.colorPickerRecentColors = recentColors
}
private fun getHexCode(color: Int) = color.toHex().substring(1)

View file

@ -10,7 +10,7 @@ fun ImageView.setFillWithStroke(fillColor: Int, backgroundColor: Int, cornerRadi
shape = GradientDrawable.RECTANGLE
setColor(fillColor)
setStroke(2, strokeColor)
setBackgroundDrawable(this)
background = this
if (cornerRadiusSize != 0f) {
cornerRadius = cornerRadiusSize

View file

@ -442,4 +442,9 @@ open class BaseConfig(val context: Context) {
var showCallConfirmation: Boolean
get() = prefs.getBoolean(SHOW_CALL_CONFIRMATION, false)
set(showCallConfirmation) = prefs.edit().putBoolean(SHOW_CALL_CONFIRMATION, showCallConfirmation).apply()
// color picker last used colors
internal var colorPickerRecentColors: LinkedList<Int>
get() = LinkedList(prefs.getString(COLOR_PICKER_RECENT_COLORS, null)?.lines()?.map { it.toInt() } ?: emptyList())
set(recentColors) = prefs.edit().putString(COLOR_PICKER_RECENT_COLORS, recentColors.joinToString(separator = "\n")).apply()
}

View file

@ -150,6 +150,7 @@ const val DEFAULT_TAB = "default_tab"
const val START_NAME_WITH_SURNAME = "start_name_with_surname"
const val FAVORITES = "favorites"
const val SHOW_CALL_CONFIRMATION = "show_call_confirmation"
internal const val COLOR_PICKER_RECENT_COLORS = "color_picker_recent_colors"
// licenses
internal const val LICENSE_KOTLIN = 1

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/color_picker_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -11,10 +12,34 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/recent_colors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/activity_margin"
android:layout_marginTop="@dimen/activity_margin">
<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/recent_colors_flow"
android:layout_width="match_parent"
android:layout_height="0dp"
app:flow_horizontalAlign="start"
app:flow_horizontalGap="@dimen/activity_margin"
app:flow_verticalGap="@dimen/medium_margin"
app:flow_maxElementsWrap="5"
app:flow_wrapMode="aligned"
app:flow_horizontalStyle="packed"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<RelativeLayout
android:id="@+id/color_picker_top_holder"
android:layout_width="match_parent"
android:layout_height="@dimen/colorpicker_size_with_padding"
android:layout_below="@+id/recent_colors"
android:gravity="center_horizontal">
<com.simplemobiletools.commons.views.ColorPickerSquare