-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-click #277 [Feat] 컴포즈 SingleEventHandler 구현 및 적용
- Loading branch information
Showing
12 changed files
with
186 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...tem/src/main/java/hous/release/designsystem/component/user_interaction/SingleEventArea.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package hous.release.designsystem.component.user_interaction | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import hous.release.designsystem.util.single_event.SingleEventHandler | ||
|
||
/** | ||
* 여러 번 클릭 이벤트를 막아주는 Wrapper Composable | ||
* */ | ||
@Composable | ||
fun <T> SingleEventArea( | ||
content: @Composable (SingleEventHandler) -> T | ||
) { | ||
val singleEventHandler = remember { SingleEventHandler() } | ||
|
||
content(singleEventHandler) | ||
} |
39 changes: 39 additions & 0 deletions
39
designsystem/src/main/java/hous/release/designsystem/modifier/ClickableSingle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package hous.release.designsystem.modifier | ||
|
||
import androidx.compose.foundation.LocalIndication | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.platform.debugInspectorInfo | ||
import androidx.compose.ui.semantics.Role | ||
import hous.release.designsystem.util.single_event.SingleEventHandler | ||
|
||
/** | ||
* 여러 번 클릭 이벤트를 막아주는 Modifier | ||
* */ | ||
fun Modifier.clickableSingle( | ||
enabled: Boolean = true, | ||
onClickLabel: String? = null, | ||
role: Role? = null, | ||
onClick: () -> Unit | ||
): Modifier = composed( | ||
inspectorInfo = debugInspectorInfo { | ||
name = "clickable" | ||
properties["enabled"] = enabled | ||
properties["onClickLabel"] = onClickLabel | ||
properties["role"] = role | ||
properties["onClick"] = onClick | ||
} | ||
) { | ||
val manager = remember { SingleEventHandler() } | ||
Modifier.clickable( | ||
enabled = enabled, | ||
onClickLabel = onClickLabel, | ||
onClick = { manager.handle { onClick() } }, | ||
role = role, | ||
indication = LocalIndication.current, | ||
interactionSource = remember { MutableInteractionSource() } | ||
) | ||
} |
2 changes: 1 addition & 1 deletion
2
...lease/designsystem/util/DimensionUtils.kt → ...signsystem/util/extension/DimensionExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...em/src/main/java/hous/release/designsystem/util/single_event/SingleEventHandleStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package hous.release.designsystem.util.single_event | ||
|
||
fun interface SingleEventHandleStrategy { | ||
fun handle(event: () -> Unit) | ||
} |
9 changes: 9 additions & 0 deletions
9
designsystem/src/main/java/hous/release/designsystem/util/single_event/SingleEventHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hous.release.designsystem.util.single_event | ||
|
||
class SingleEventHandler( | ||
private val singleEventHandleStrategy: SingleEventHandleStrategy = ThrottledDurationStrategy() | ||
) { | ||
fun handle(event: () -> Unit) { | ||
singleEventHandleStrategy.handle(event) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...em/src/main/java/hous/release/designsystem/util/single_event/ThrottledDurationStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package hous.release.designsystem.util.single_event | ||
|
||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.ExperimentalTime | ||
import kotlin.time.TimeMark | ||
import kotlin.time.TimeSource | ||
|
||
@OptIn(ExperimentalTime::class) | ||
internal class ThrottledDurationStrategy : SingleEventHandleStrategy { | ||
private val currentTime: TimeMark get() = TimeSource.Monotonic.markNow() | ||
private val throttleDuration: Duration = 300.milliseconds | ||
private lateinit var lastEventTime: TimeMark | ||
|
||
override fun handle(event: () -> Unit) { | ||
if (::lastEventTime.isInitialized.not() || (lastEventTime + throttleDuration).hasPassedNow()) { | ||
event() | ||
} | ||
lastEventTime = currentTime | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ystem/src/test/java/hous/release/designsystem/util/single_event/SingleEventHandlerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package hous.release.designsystem.util.single_event | ||
|
||
import hous.release.testing.CoroutinesTestExtension | ||
import io.mockk.every | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@ExtendWith(CoroutinesTestExtension::class) | ||
@ExtendWith(MockKExtension::class) | ||
internal class SingleEventHandlerTest { | ||
|
||
@Test | ||
fun `이벤트를 처리할 때 내부적으로 SingleEventHandleStrategy의 handle을 호출 한다`() { | ||
// given | ||
val mockStrategy = mockk<SingleEventHandleStrategy>(relaxed = true) | ||
val handler = SingleEventHandler(mockStrategy) | ||
|
||
// when | ||
every { mockStrategy.handle(any()) } returns Unit | ||
handler.handle { } | ||
// then | ||
verify(exactly = 1) { handler.handle(any()) } | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rc/test/java/hous/release/designsystem/util/single_event/ThrottledDurationStrategyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hous.release.designsystem.util.single_event | ||
|
||
import com.google.common.truth.Truth | ||
import hous.release.testing.CoroutinesTestExtension | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@ExtendWith(CoroutinesTestExtension::class) | ||
internal class ThrottledDurationStrategyTest { | ||
|
||
@Test | ||
fun `마지막 이벤트가 발생한 지 300ms가 지나지 않고 추가 이벤트가 발생시 무시한다`() = runTest { | ||
// given | ||
val strategy = ThrottledDurationStrategy() | ||
var count = 0 | ||
// when | ||
strategy.handle { | ||
count++ | ||
} | ||
delay(301) | ||
strategy.handle { | ||
count++ | ||
} | ||
// then | ||
Truth.assertThat(count).isEqualTo(1) | ||
} | ||
} |