From 01be2f00c58b73cf79a14110e41aa777726a3509 Mon Sep 17 00:00:00 2001 From: Andrii Chubko Date: Mon, 13 Sep 2021 03:40:02 +0300 Subject: [PATCH] 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. --- .../commons/dialogs/ColorPickerDialog.kt | 87 +++++++++++++++---- .../commons/extensions/ImageView.kt | 2 +- .../commons/helpers/BaseConfig.kt | 5 ++ .../commons/helpers/Constants.kt | 1 + .../main/res/layout/dialog_color_picker.xml | 25 ++++++ 5 files changed, 101 insertions(+), 19 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/ColorPickerDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/ColorPickerDialog.kt index c702cd900..1a4128a48 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/ColorPickerDialog.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/ColorPickerDialog.kt @@ -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,11 +140,11 @@ 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() } - .setOnCancelListener { dialogDismissed() } + .setPositiveButton(R.string.ok) { dialog, which -> confirmNewColor() } + .setNegativeButton(R.string.cancel) { dialog, which -> dialogDismissed() } + .setOnCancelListener { dialogDismissed() } if (showUseDefaultButton) { builder.setNeutralButton(R.string.use_default) { dialog, which -> useDefault() } @@ -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) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/ImageView.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/ImageView.kt index 12539a6e2..b607568dc 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/ImageView.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/ImageView.kt @@ -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 diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt index 620b12b8b..f8648727d 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt @@ -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 + 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() } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt index ea7817cb2..8938f1f0a 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt @@ -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 diff --git a/commons/src/main/res/layout/dialog_color_picker.xml b/commons/src/main/res/layout/dialog_color_picker.xml index ca278a941..dd91e2b87 100644 --- a/commons/src/main/res/layout/dialog_color_picker.xml +++ b/commons/src/main/res/layout/dialog_color_picker.xml @@ -1,5 +1,6 @@ + + + + + +