Skip to content

Commit e57c101

Browse files
committed
chore: extract magic numbers
1 parent 4d20ac2 commit e57c101

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

app/src/main/java/to/bitkit/ui/components/ToastView.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ import to.bitkit.ui.theme.AppThemeSurface
5757
import to.bitkit.ui.theme.Colors
5858
import kotlin.math.roundToInt
5959

60+
private const val DISMISS_THRESHOLD_DP = 50
61+
private const val DISMISS_ANIMATION_TARGET_PX = -200f
62+
private const val DISMISS_ANIMATION_DURATION_MS = 300
63+
private const val SNAP_BACK_DAMPING_RATIO = 0.7f
64+
private const val DRAG_RESISTANCE_FACTOR = 0.08f
65+
private const val DRAG_START_THRESHOLD_PX = 5
66+
private const val TINT_ALPHA = 0.32f
67+
private const val SHADOW_ALPHA = 0.4f
68+
private const val ELEVATION_DP = 10
69+
6070
@OptIn(ExperimentalHazeMaterialsApi::class)
6171
@Composable
6272
fun ToastView(
@@ -70,7 +80,7 @@ fun ToastView(
7080
val coroutineScope = rememberCoroutineScope()
7181
val dragOffset = remember { Animatable(0f) }
7282
var hasPausedAutoHide by remember { mutableStateOf(false) }
73-
val dismissThreshold = 50.dp
83+
val dismissThreshold = DISMISS_THRESHOLD_DP.dp
7484

7585
Box(
7686
contentAlignment = Alignment.TopStart,
@@ -88,17 +98,17 @@ fun ToastView(
8898
.fillMaxWidth()
8999
.offset { IntOffset(0, dragOffset.value.roundToInt()) }
90100
.shadow(
91-
elevation = 10.dp,
101+
elevation = ELEVATION_DP.dp,
92102
shape = MaterialTheme.shapes.medium,
93-
ambientColor = Color.Black.copy(alpha = 0.4f),
94-
spotColor = Color.Black.copy(alpha = 0.4f)
103+
ambientColor = Color.Black.copy(alpha = SHADOW_ALPHA),
104+
spotColor = Color.Black.copy(alpha = SHADOW_ALPHA)
95105
)
96106
.hazeEffect(
97107
state = hazeState,
98108
style = toastMaterial
99109
)
100110
.background(
101-
color = tintColor.copy(alpha = 0.32f),
111+
color = tintColor.copy(alpha = TINT_ALPHA),
102112
shape = MaterialTheme.shapes.medium
103113
)
104114
.pointerInput(Unit) {
@@ -115,16 +125,16 @@ fun ToastView(
115125
if (dragOffset.value < -dismissThreshold.toPx()) {
116126
// Animate out
117127
dragOffset.animateTo(
118-
targetValue = -200f,
119-
animationSpec = tween(durationMillis = 300)
128+
targetValue = DISMISS_ANIMATION_TARGET_PX,
129+
animationSpec = tween(durationMillis = DISMISS_ANIMATION_DURATION_MS)
120130
)
121131
onDismiss()
122132
} else {
123133
// Snap back to original position
124134
dragOffset.animateTo(
125135
targetValue = 0f,
126136
animationSpec = spring(
127-
dampingRatio = 0.7f,
137+
dampingRatio = SNAP_BACK_DAMPING_RATIO,
128138
stiffness = Spring.StiffnessMedium
129139
)
130140
)
@@ -136,7 +146,7 @@ fun ToastView(
136146
dragOffset.animateTo(
137147
targetValue = 0f,
138148
animationSpec = spring(
139-
dampingRatio = 0.7f,
149+
dampingRatio = SNAP_BACK_DAMPING_RATIO,
140150
stiffness = Spring.StiffnessMedium
141151
)
142152
)
@@ -152,11 +162,11 @@ fun ToastView(
152162
dragOffset.snapTo(translation)
153163
} else {
154164
// Downward drag - apply resistance
155-
dragOffset.snapTo(translation * 0.08f)
165+
dragOffset.snapTo(translation * DRAG_RESISTANCE_FACTOR)
156166
}
157167

158168
// Pause auto-hide when drag starts (only once)
159-
if (kotlin.math.abs(dragOffset.value) > 5 && !hasPausedAutoHide) {
169+
if (kotlin.math.abs(dragOffset.value) > DRAG_START_THRESHOLD_PX && !hasPausedAutoHide) {
160170
hasPausedAutoHide = true
161171
onDragStart()
162172
}

0 commit comments

Comments
 (0)