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
@Composable | |
fun EarthquakeBox( | |
onEarthquakeFinished: () -> Unit = {}, | |
content: @Composable IEarthquakeScope.() -> Unit | |
) { | |
val coroutineScope = rememberCoroutineScope() | |
val state = remember { EarthquakeState() } | |
val scope = remember { EarthquakeScope(state = state) } | |
val mover = remember { EarthquakeMover() } |
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
@Immutable | |
class EarthquakeMover : IEarthquakeMover { | |
override val x = Animatable(0.dp.value) | |
override val y = Animatable(0.dp.value) | |
override val rotation = Animatable(0.dp.value) | |
override val alpha = Animatable(1f) | |
override suspend fun move(shakeDuration: Int, shakeForce: Int) { | |
val shakeOffset = generateOffset(shakeForce) | |
val shakeRotation = generateRotation(shakeForce) |
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
@Immutable | |
interface IEarthquakeMover { | |
val x: Animatable<Float, AnimationVector1D> | |
val y: Animatable<Float, AnimationVector1D> | |
val rotation: Animatable<Float, AnimationVector1D> | |
val alpha: Animatable<Float, AnimationVector1D> | |
suspend fun move(shakeDuration: Int, shakeForce: Int) | |
suspend fun stop() |
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
fun CoroutineScope.flowTimer( | |
duration: Long, | |
period: Long, | |
onFinished: () -> Unit = {}, | |
onTick: (Long) -> Unit | |
) = | |
this.launch { | |
(duration downTo 0 step period).asFlow() | |
.onEach { | |
onTick(it) |
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
@Immutable | |
class EarthquakeController( | |
private val scope: CoroutineScope, | |
private val mover: IEarthquakeMover, | |
private val onEarthquakeFinished: () -> Unit | |
) : IEarthquakeController { | |
private var timerJob: Job? = null | |
override fun startShaking(earthquakeDuration: Long, shakeDuration: Long, shakeForce: Int) { |
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
@Immutable | |
interface IEarthquakeController { | |
fun startShaking(earthquakeDuration: Long, shakeDuration: Long, shakeForce: Int) | |
fun stopShaking() | |
} |
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
class EarthquakeScope(private val state: EarthquakeState) : IEarthquakeScope { | |
override fun startShaking() { | |
if (!state.isShaking && state.earthquakeDuration > 0) | |
state.isShaking = true | |
} | |
override fun stopShaking() { | |
if (state.isShaking) | |
state.isShaking = false | |
} |
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
interface IEarthquakeScope { | |
fun startShaking() | |
fun stopShaking() | |
val isShaking: Boolean | |
var shakeDuration: Long | |
var shakesPerSecond: Int | |
var shakeForce: Int | |
} |
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
@Stable | |
class EarthquakeState { | |
var isShaking by mutableStateOf(false) | |
var earthquakeDuration by mutableStateOf(2000L) | |
var shakesPerSecond by mutableStateOf(10) | |
var shakeForce by mutableStateOf(10) | |
} |
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
val items = listOf( | |
"Paulo Pereira", | |
"Daenerys Targaryen", | |
"Jon Snow", | |
"Sansa Stark", | |
) | |
val autoCompleteEntities = items.asAutoCompleteEntities( | |
filter = { item, query -> | |
item.toLowerCase(Locale.getDefault()) | |
.startsWith(query.toLowerCase(Locale.getDefault())) |