Skip to content

Instantly share code, notes, and snippets.

View pauloaapereira's full-sized avatar

Paulo Pereira pauloaapereira

View GitHub Profile
@pauloaapereira
pauloaapereira / earthquake_9.kt
Created April 23, 2021 23:12
Jetpack Compose - Earthquake Effect_9
@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() }
@pauloaapereira
pauloaapereira / earthquake_8.kt
Created April 23, 2021 23:08
Jetpack Compose - Earthquake Effect_8
@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)
@pauloaapereira
pauloaapereira / earthquake_7.kt
Created April 23, 2021 23:01
Jetpack Compose - Earthquake Effect_7
@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()
@pauloaapereira
pauloaapereira / earthquake_6.kt
Last active April 23, 2021 22:51
Jetpack Compose - Earthquake Effect_6
fun CoroutineScope.flowTimer(
duration: Long,
period: Long,
onFinished: () -> Unit = {},
onTick: (Long) -> Unit
) =
this.launch {
(duration downTo 0 step period).asFlow()
.onEach {
onTick(it)
@pauloaapereira
pauloaapereira / earthquake_5.kt
Created April 23, 2021 22:34
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) {
@pauloaapereira
pauloaapereira / earthquake_4.kt
Last active April 23, 2021 22:30
Jetpack Compose - Earthquake Effect_4
@Immutable
interface IEarthquakeController {
fun startShaking(earthquakeDuration: Long, shakeDuration: Long, shakeForce: Int)
fun stopShaking()
}
@pauloaapereira
pauloaapereira / earthquake_3.kt
Last active April 23, 2021 22:49
Jetpack Compose - Earthquake Effect_3
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
}
@pauloaapereira
pauloaapereira / earthquake_2.kt
Created April 23, 2021 22:17
Jetpack Compose - Earthquake Effect_2
interface IEarthquakeScope {
fun startShaking()
fun stopShaking()
val isShaking: Boolean
var shakeDuration: Long
var shakesPerSecond: Int
var shakeForce: Int
}
@pauloaapereira
pauloaapereira / earthquake_1.kt
Created April 23, 2021 22:14
Jetpack Compose - Earthquake Effect_1
@Stable
class EarthquakeState {
var isShaking by mutableStateOf(false)
var earthquakeDuration by mutableStateOf(2000L)
var shakesPerSecond by mutableStateOf(10)
var shakeForce by mutableStateOf(10)
}
@pauloaapereira
pauloaapereira / autocomplete_9.kt
Created March 31, 2021 01:46
Jetpack Compose - Auto Complete Search Bar_9
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()))