Skip to content

Commit

Permalink
Apply debounceDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Jun 29, 2024
1 parent 1b69d65 commit 8f9c97f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</p><br>

<p align="center">
🎨 Jetpack Compose color picker library that allows you to get colors from any images like gallery pictures by tapping on the desired color.
🎨 Kotlin Multiplatform color picker library that allows you to get colors from any images like gallery pictures by tapping on the desired color.
Also, it supports brightness and alpha slider, which can adjust your ARGB factors.
</p>

Expand Down Expand Up @@ -185,15 +185,15 @@ You can select specific points with the functions below:
You can set the debounce duration, which decides to invoke the color listener from the last tapping. Debounce can be useful to reduce overhead. For example, communicating with IoT devices or relevant works that require heavy operations.

```kotlin
.setDebounceDuration(300L)
controller.debounceDuration = 200L
```

#### Enable and Disable

You can enable or disable your color picker with the below function:

```kotlin
.setEnabled(false)
controller.enabled = false
```

<img src="preview/preview3.gif" width="270" align="right">
Expand Down
4 changes: 2 additions & 2 deletions colorpicker-compose/api/android/colorpicker-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class com/github/skydoves/colorpicker/compose/ColorPickerController
public synthetic fun <init> (Lkotlinx/coroutines/CoroutineScope;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getColorFlow (J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun getColorFlow$default (Lcom/github/skydoves/colorpicker/compose/ColorPickerController;JILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun getDebounceDuration ()Ljava/lang/Long;
public final fun getEnabled ()Z
public final fun getPaletteBitmap ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getSelectedColor ()Landroidx/compose/runtime/State;
Expand All @@ -50,8 +51,7 @@ public final class com/github/skydoves/colorpicker/compose/ColorPickerController
public final fun selectCenter (Z)V
public final fun setAlpha (FZ)V
public final fun setBrightness (FZ)V
public final fun setDebounceDuration (J)V
public static synthetic fun setDebounceDuration$default (Lcom/github/skydoves/colorpicker/compose/ColorPickerController;JILjava/lang/Object;)V
public final fun setDebounceDuration (Ljava/lang/Long;)V
public final fun setEnabled (Z)V
public final fun setPaletteImageBitmap (Landroidx/compose/ui/graphics/ImageBitmap;)V
public final fun setWheelAlpha (F)V
Expand Down
4 changes: 2 additions & 2 deletions colorpicker-compose/api/desktop/colorpicker-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class com/github/skydoves/colorpicker/compose/ColorPickerController
public synthetic fun <init> (Lkotlinx/coroutines/CoroutineScope;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getColorFlow (J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun getColorFlow$default (Lcom/github/skydoves/colorpicker/compose/ColorPickerController;JILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun getDebounceDuration ()Ljava/lang/Long;
public final fun getEnabled ()Z
public final fun getPaletteBitmap ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getSelectedColor ()Landroidx/compose/runtime/State;
Expand All @@ -50,8 +51,7 @@ public final class com/github/skydoves/colorpicker/compose/ColorPickerController
public final fun selectCenter (Z)V
public final fun setAlpha (FZ)V
public final fun setBrightness (FZ)V
public final fun setDebounceDuration (J)V
public static synthetic fun setDebounceDuration$default (Lcom/github/skydoves/colorpicker/compose/ColorPickerController;JILjava/lang/Object;)V
public final fun setDebounceDuration (Ljava/lang/Long;)V
public final fun setEnabled (Z)V
public final fun setPaletteImageBitmap (Landroidx/compose/ui/graphics/ImageBitmap;)V
public final fun setWheelAlpha (F)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public fun AlphaTile(
color = controller?.selectedColor?.value ?: selectedColor
}
val paint = alphaTilePaint(
with(density) { tileSize.toPx() },
tileOddColor,
tileEvenColor,
tileSize = with(density) { tileSize.toPx() },
tileOddColor = tileOddColor,
tileEvenColor = tileEvenColor,
)

Canvas(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ internal fun ColorPicker(
) {
var initialized by remember { mutableStateOf(false) }

DisposableEffect(key1 = controller) {
val debounceDuration = controller.debounceDuration
DisposableEffect(key1 = controller, key2 = debounceDuration) {
controller.coroutineScope.launch(Dispatchers.Main) {
controller.getColorFlow().collect { onColorChanged(it) }
controller.getColorFlow(debounceDuration ?: 0).collect {
onColorChanged(it)
}
}
onDispose { controller.releaseBitmap() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ constructor(
/** An [ImageBitmap] to be drawn on the canvas as a wheel. */
public var wheelBitmap: ImageBitmap? = null

internal val _debounceDuration: MutableState<Long?> = mutableStateOf(null)

/** A debounce duration for observing color changes. */
private var debounceDuration: Long? = null
public var debounceDuration: Long?
get() = _debounceDuration.value
set(value) {
_debounceDuration.value = value
}

/** Radius to draw default wheel. */
public var wheelRadius: Dp = 12.dp
Expand Down Expand Up @@ -265,11 +271,6 @@ constructor(
}
}

/** Sets the debounce duration that allows you to observe color changes with a given duration. */
public fun setDebounceDuration(duration: Long = 0) {
this.debounceDuration = duration
}

/** Notify color changes to the color picker and other subcomponents. */
private fun notifyColorChanged(fromUser: Boolean) {
val color = _selectedColor.value
Expand Down

0 comments on commit 8f9c97f

Please sign in to comment.