Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IDLE-501] API 요청을 하는 버튼 및 키보드 액션에 Throttle 적용 #150

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
namespace = "com.idle.care"

defaultConfig {
versionCode = 9
versionName = "1.1.2"
versionCode = 10
versionName = "1.1.3"
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.focus.FocusManager
import androidx.compose.ui.input.pointer.pointerInput

fun Modifier.addFocusCleaner(focusManager: FocusManager, doOnClear: () -> Unit = {}): Modifier {
fun Modifier.addFocusCleaner(
focusManager: FocusManager,
doOnClear: () -> Unit = {},
): Modifier {
return this.pointerInput(Unit) {
detectTapGestures(
onTap = {
Expand All @@ -23,12 +30,20 @@ fun Modifier.addFocusCleaner(focusManager: FocusManager, doOnClear: () -> Unit =
@Composable
fun Modifier.clickable(
enabled: Boolean = true,
throttleTime: Long = 0L,
onClick: () -> Unit,
): Modifier {
return this.clickable(
): Modifier = composed {
var lastClickTime by remember { mutableStateOf(0L) }

this.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null,
enabled = enabled,
onClick = onClick,
)
}
) {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Devices
Expand All @@ -26,9 +30,18 @@ fun CareButtonSmall(
onClick: () -> Unit,
enable: Boolean,
modifier: Modifier = Modifier,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(8.dp),
contentPadding = PaddingValues(0.dp),
Expand Down Expand Up @@ -57,9 +70,18 @@ fun CareButtonMedium(
containerColor: Color = CareTheme.colors.orange500,
textColor: Color = CareTheme.colors.white000,
border: BorderStroke? = null,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(8.dp),
border = border,
Expand Down Expand Up @@ -89,9 +111,18 @@ fun CareButtonLarge(
disabledContainerColor: Color = CareTheme.colors.gray200,
textColor: Color = CareTheme.colors.white000,
border: BorderStroke? = null,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(8.dp),
colors = ButtonColors(
Expand All @@ -117,9 +148,18 @@ fun CareButtonCardLarge(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enable: Boolean = true,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(6.dp),
colors = ButtonColors(
Expand Down Expand Up @@ -147,9 +187,18 @@ fun CareButtonCardMedium(
containerColor: Color = CareTheme.colors.orange500,
textColor: Color = CareTheme.colors.white000,
border: BorderStroke? = null,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
border = border,
shape = RoundedCornerShape(6.dp),
Expand All @@ -175,9 +224,18 @@ fun CareButtonRound(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enable: Boolean = true,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(19.dp),
border = BorderStroke(width = 1.dp, color = CareTheme.colors.gray100),
Expand Down Expand Up @@ -207,9 +265,18 @@ fun CareButtonLine(
borderColor: Color = CareTheme.colors.orange400,
containerColor: Color = CareTheme.colors.white000,
textColor: Color = CareTheme.colors.orange500,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(6.dp),
border = BorderStroke(
Expand Down Expand Up @@ -241,9 +308,18 @@ fun CareDialogButton(
modifier: Modifier = Modifier,
border: BorderStroke? = null,
enable: Boolean = true,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(6.dp),
border = border,
Expand Down Expand Up @@ -273,9 +349,18 @@ fun CareFloatingButton(
modifier: Modifier = Modifier,
border: BorderStroke? = null,
enable: Boolean = true,
throttleTime: Long = 0L,
) {
var lastClickTime by remember { mutableStateOf(0L) }

Button(
onClick = onClick,
onClick = {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime >= throttleTime) {
onClick()
lastClickTime = currentTime
}
},
enabled = enable,
shape = RoundedCornerShape(50.dp),
border = border,
Expand Down Expand Up @@ -440,7 +525,11 @@ private fun PreviewButtonPrimaryDefaultFloating() {
}

// Flip Group Previews
@Preview(name = "Button_Primary_Flip_Large", showBackground = true, device = FLIP, group = "Flip")
@Preview(
name = "Button_Primary_Flip_Large", showBackground = true,
device = FLIP,
group = "Flip"
)
@Composable
private fun PreviewButtonPrimaryFlipLarge() {
PreviewButtonLarge()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
Expand Down Expand Up @@ -51,6 +54,7 @@ fun CareTextField(
readOnly: Boolean = false,
enabled: Boolean = true,
isError: Boolean = false,
throttleTime: Long = 0L,
onDone: () -> Unit = {},
textStyle: TextStyle = CareTheme.typography.body3.copy(
color = if (readOnly) {
Expand All @@ -74,6 +78,7 @@ fun CareTextField(
CareTheme.colors.gray100
},
)
var lastDoneTime by remember { mutableStateOf(0L) }

Column(
verticalArrangement = Arrangement.spacedBy(4.dp),
Expand Down Expand Up @@ -110,8 +115,12 @@ fun CareTextField(
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
keyboardController?.hide()
onDone()
val currentTime = System.currentTimeMillis()
if (currentTime - lastDoneTime >= throttleTime) {
keyboardController?.hide()
onDone()
lastDoneTime = currentTime
}
}),
modifier = Modifier
.weight(1f)
Expand Down Expand Up @@ -156,6 +165,7 @@ fun CareTextField(
readOnly: Boolean = false,
enabled: Boolean = true,
isError: Boolean = false,
throttleTime: Long = 0L,
onDone: () -> Unit = {},
textStyle: TextStyle = CareTheme.typography.body3.copy(
color = if (readOnly) {
Expand All @@ -179,6 +189,7 @@ fun CareTextField(
CareTheme.colors.gray100
},
)
var lastDoneTime by remember { mutableStateOf(0L) }

Row(
verticalAlignment = Alignment.CenterVertically,
Expand Down Expand Up @@ -211,8 +222,12 @@ fun CareTextField(
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
keyboardController?.hide()
onDone()
val currentTime = System.currentTimeMillis()
if (currentTime - lastDoneTime >= throttleTime) {
keyboardController?.hide()
onDone()
lastDoneTime = currentTime
}
}),
modifier = Modifier
.weight(1f)
Expand Down Expand Up @@ -244,6 +259,7 @@ fun CareTextFieldLong(
isError: Boolean = false,
visualTransformation: VisualTransformation = VisualTransformation.None,
onValueChanged: (String) -> Unit,
throttleTime: Long = 0L,
onDone: () -> Unit = {},
modifier: Modifier = Modifier,
) {
Expand All @@ -260,6 +276,7 @@ fun CareTextFieldLong(
CareTheme.colors.gray100
},
)
var lastDoneTime by remember { mutableStateOf(0L) }

Box(
modifier = modifier
Expand Down Expand Up @@ -293,8 +310,12 @@ fun CareTextFieldLong(
visualTransformation = visualTransformation,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = {
onDone()
keyboardController?.hide()
val currentTime = System.currentTimeMillis()
if (currentTime - lastDoneTime >= throttleTime) {
keyboardController?.hide()
onDone()
lastDoneTime = currentTime
}
}),
modifier = Modifier.fillMaxSize(),
decorationBox = { innerTextField ->
Expand All @@ -318,6 +339,7 @@ fun CareClickableTextField(
onClick: () -> Unit,
modifier: Modifier = Modifier,
hint: String = "",
throttleTime: Long = 0L,
leftComponent: @Composable () -> Unit = {},
) {
Row(
Expand All @@ -330,7 +352,10 @@ fun CareClickableTextField(
border = BorderStroke(width = 1.dp, color = CareTheme.colors.gray100),
shape = RoundedCornerShape(6.dp)
)
.clickable(onClick = onClick)
.clickable(
onClick = onClick,
throttleTime = throttleTime,
)
.padding(horizontal = 16.dp),
) {
Text(
Expand Down
Loading
Loading