Created
April 23, 2021 22:34
-
-
Save pauloaapereira/31b1d69bd87ab94667efb9cd68703228 to your computer and use it in GitHub Desktop.
Jetpack Compose - Earthquake Effect_5
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) { | |
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