Skip to content

Instantly share code, notes, and snippets.

@pauloaapereira
Created April 23, 2021 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauloaapereira/31b1d69bd87ab94667efb9cd68703228 to your computer and use it in GitHub Desktop.
Save pauloaapereira/31b1d69bd87ab94667efb9cd68703228 to your computer and use it in GitHub Desktop.
Jetpack Compose - Earthquake Effect_5
@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) {
timerJob = scope.flowTimer(
duration = earthquakeDuration,
period = shakeDuration,
onFinished = {
onEarthquakeFinished()
stopShaking()
}
) {
scope.launch {
mover.move(
shakeDuration = shakeDuration.toInt(),
shakeForce = shakeForce
)
}
}
}
override fun stopShaking() {
timerJob ?: return
timerJob?.cancel()
timerJob = null
scope.launch {
mover.stop()
}
onEarthquakeFinished()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment