Skip to content

Commit

Permalink
feat: Add toggleable modifier extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
raine-lemon committed Sep 2, 2024
1 parent 00d0931 commit 5b896c2
Showing 1 changed file with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.lemon.core.compose.util.modifier

import androidx.compose.foundation.Indication
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.selection.toggleable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.semantics.Role
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* Toggle interaction 이 연속적으로 발생하는 것을 방지하는 확장자
*/
fun Modifier.throttleToggleable(
value: Boolean,
onValueChange: (Boolean) -> Unit,
enabled: Boolean = true,
role: Role? = null,
coroutineDispatcher: CoroutineDispatcher = Dispatchers.Main,
throttleTime: Long = 250L,
) = composed {
val coroutineScope = rememberCoroutineScope { coroutineDispatcher }
var lastEmissionTime: Long by remember { mutableLongStateOf(0L) }

toggleable(
value = value,
onValueChange = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastEmissionTime >= throttleTime) {
coroutineScope.launch {
lastEmissionTime = currentTime
onValueChange(it)
}
}
lastEmissionTime = currentTime
},
enabled = enabled,
role = role,
)
}

/**
* Toggle interaction 이 연속적으로 발생하는 것을 방지하는 확장자
*/
fun Modifier.throttleToggleable(
value: Boolean,
onValueChange: (Boolean) -> Unit,
interactionSource: MutableInteractionSource = MutableInteractionSource(),
indication: Indication? = null,
enabled: Boolean = true,
role: Role? = null,
coroutineDispatcher: CoroutineDispatcher = Dispatchers.Main,
throttleTime: Long = 250L,
) = composed {
val coroutineScope = rememberCoroutineScope { coroutineDispatcher }
var lastEmissionTime: Long by remember { mutableLongStateOf(0L) }

noRippleToggleable(
value = value,
onValueChange = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastEmissionTime >= throttleTime) {
coroutineScope.launch {
lastEmissionTime = currentTime
onValueChange(it)
}
}
lastEmissionTime = currentTime
},
interactionSource = remember { interactionSource },
indication = indication,
enabled = enabled,
role = role
)
}

/**
* Toggle interaction 중 발생하는 시각 이펙트를 제거하는 확장자
*/
fun Modifier.noRippleToggleable(
value: Boolean,
onValueChange: (Boolean) -> Unit,
interactionSource: MutableInteractionSource = MutableInteractionSource(),
indication: Indication? = null,
enabled: Boolean = true,
role: Role? = null,
) = composed {
toggleable(
value = value,
interactionSource = remember { interactionSource },
indication = indication,
enabled = enabled,
onValueChange = onValueChange,
role = role
)
}

0 comments on commit 5b896c2

Please sign in to comment.