Skip to content

Commit

Permalink
feat: custom click able
Browse files Browse the repository at this point in the history
  • Loading branch information
lsakee committed Jul 10, 2024
1 parent b2226f9 commit a06978f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
55 changes: 55 additions & 0 deletions core/ui/src/main/java/com/record/ui/extension/Modifier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.record.ui.extension

import android.annotation.SuppressLint
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.Color

/**
* @param rippleEnabled
* @param rippleColor
* @param runIf clickable 조건
* @param singleClick 중복 클릭
* @param onClick
* @return clickable 이 적용된 [Modifier]
*/
@SuppressLint("ModifierFactoryUnreferencedReceiver")
@OptIn(ExperimentalFoundationApi::class)
fun Modifier.customClickable(
rippleEnabled: Boolean = true,
rippleColor: Color? = null,
runIf: Boolean = true,
singleClick: Boolean = true,
onLongClick: (() -> Unit)? = null,
onClick: (() -> Unit)?,
) = runIf(runIf) {
composed {
val multipleEventsCutter = remember { MultipleEventsCutter.get() }

combinedClickable(
onClick = {
onClick?.let {
if (singleClick) {
multipleEventsCutter.processEvent {
it()
}
} else {
it()
}
}
},
onLongClick = onLongClick,
indication = rememberRipple(
color = rippleColor ?: Color.Unspecified,
).takeIf {
rippleEnabled
},
interactionSource = remember { MutableInteractionSource() },
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.record.ui.extension

internal inline fun <T> T.runIf(condition: Boolean, run: T.() -> T) = if (condition) {
run()
} else {
this
}

private const val CLICK_EVENT_DELAY_TIME: Long = 300L

internal interface MultipleEventsCutter {
fun processEvent(event: () -> Unit)

companion object
}

internal fun MultipleEventsCutter.Companion.get(): MultipleEventsCutter =
MultipleEventsCutterImpl()

private class MultipleEventsCutterImpl : MultipleEventsCutter {
private val now: Long
get() = System.currentTimeMillis()

private var lastEventTimeMs: Long = 0

override fun processEvent(event: () -> Unit) {
if (now - lastEventTimeMs >= CLICK_EVENT_DELAY_TIME) {
event.invoke()
}
lastEventTimeMs = now
}
}

0 comments on commit a06978f

Please sign in to comment.