feat(ui): тактильный тик при вращении листа
This commit is contained in:
1 parent
1d73a97672
commit
bba32ea051
2 files changed
+29
No files matched your search
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
package com.freeturn.app.ui.screens.home
|
package com.freeturn.app.ui.screens.home
|
||||||
|
|
||||||
|
import android.os.SystemClock
|
||||||
import androidx.compose.animation.core.Animatable
|
import androidx.compose.animation.core.Animatable
|
||||||
import androidx.compose.animation.core.AnimationVector1D
|
import androidx.compose.animation.core.AnimationVector1D
|
||||||
import androidx.compose.animation.core.LinearEasing
|
import androidx.compose.animation.core.LinearEasing
|
||||||
@@ -20,6 +21,7 @@ import androidx.compose.runtime.mutableStateOf
|
|||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.runtime.snapshotFlow
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.geometry.Offset
|
import androidx.compose.ui.geometry.Offset
|
||||||
import androidx.compose.ui.geometry.Size
|
import androidx.compose.ui.geometry.Size
|
||||||
@@ -58,6 +60,7 @@ private const val BURST_MS = 950
|
|||||||
|
|
||||||
/** Грань Cookie12Sided: на отпускании фигура доводится до кратного угла. */
|
/** Грань Cookie12Sided: на отпускании фигура доводится до кратного угла. */
|
||||||
private const val SPIN_NOTCH_DEG = 30f
|
private const val SPIN_NOTCH_DEG = 30f
|
||||||
|
private const val SPIN_TICK_MIN_MS = 30L
|
||||||
|
|
||||||
/** Сектор разлёта: вверх и в стороны. Вниз лист не выстреливает, он туда падает сам. */
|
/** Сектор разлёта: вверх и в стороны. Вниз лист не выстреливает, он туда падает сам. */
|
||||||
private const val SPREAD_FROM_DEG = -170f
|
private const val SPREAD_FROM_DEG = -170f
|
||||||
@@ -217,6 +220,24 @@ internal fun Modifier.leafSpin(
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
val settleSpec = MaterialTheme.motionScheme.slowSpatialSpec<Float>()
|
val settleSpec = MaterialTheme.motionScheme.slowSpatialSpec<Float>()
|
||||||
|
|
||||||
|
LaunchedEffect(spin, enabled) {
|
||||||
|
if (!enabled) return@LaunchedEffect
|
||||||
|
var lastAngle = spin.value
|
||||||
|
var lastNotch = (lastAngle / SPIN_NOTCH_DEG).roundToInt()
|
||||||
|
var lastTickAt = 0L
|
||||||
|
snapshotFlow { spin.value }.collect { angle ->
|
||||||
|
val notch = (angle / SPIN_NOTCH_DEG).roundToInt()
|
||||||
|
val real = abs(angle - lastAngle) < 180f
|
||||||
|
val now = SystemClock.uptimeMillis()
|
||||||
|
if (real && notch != lastNotch && now - lastTickAt >= SPIN_TICK_MIN_MS) {
|
||||||
|
HapticUtil.perform(context, HapticUtil.Pattern.TICK)
|
||||||
|
lastTickAt = now
|
||||||
|
}
|
||||||
|
lastAngle = angle
|
||||||
|
lastNotch = notch
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!enabled) return this
|
if (!enabled) return this
|
||||||
|
|
||||||
return pointerInput(Unit) {
|
return pointerInput(Unit) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import com.freeturn.app.viewmodel.Haptics
|
|||||||
object HapticUtil {
|
object HapticUtil {
|
||||||
|
|
||||||
enum class Pattern {
|
enum class Pattern {
|
||||||
|
TICK,
|
||||||
SELECTION,
|
SELECTION,
|
||||||
CLICK,
|
CLICK,
|
||||||
TOGGLE_ON,
|
TOGGLE_ON,
|
||||||
@@ -31,6 +32,12 @@ object HapticUtil {
|
|||||||
val vibrator = context.getSystemService(Vibrator::class.java) ?: return
|
val vibrator = context.getSystemService(Vibrator::class.java) ?: return
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
val effect = when (pattern) {
|
val effect = when (pattern) {
|
||||||
|
Pattern.TICK -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)
|
||||||
|
} else {
|
||||||
|
VibrationEffect.createOneShot(8, 70)
|
||||||
|
}
|
||||||
|
|
||||||
Pattern.SELECTION -> VibrationEffect.createOneShot(20, 120)
|
Pattern.SELECTION -> VibrationEffect.createOneShot(20, 120)
|
||||||
|
|
||||||
Pattern.CLICK -> VibrationEffect.createOneShot(25, 160)
|
Pattern.CLICK -> VibrationEffect.createOneShot(25, 160)
|
||||||
@@ -90,6 +97,7 @@ object HapticUtil {
|
|||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
} else {
|
} else {
|
||||||
val duration = when (pattern) {
|
val duration = when (pattern) {
|
||||||
|
Pattern.TICK -> 8L
|
||||||
Pattern.SELECTION -> 20L
|
Pattern.SELECTION -> 20L
|
||||||
Pattern.CLICK -> 25L
|
Pattern.CLICK -> 25L
|
||||||
Pattern.TOGGLE_ON, Pattern.TOGGLE_OFF -> 50L
|
Pattern.TOGGLE_ON, Pattern.TOGGLE_OFF -> 50L
|
||||||
|
|||||||
Reference in new issue
Block a user