Skip to content

Instantly share code, notes, and snippets.

Avatar

Gabriel Peal gpeal

View GitHub Profile
View lottie.kt
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
val progress by animateLottieCompositionAsState(
composition,
iterations = LottieConstants.IterateForever,
)
LottieAnimation(
composition,
progress,
)
View LottieAnimation.kt
LottieAnimation(
composition,
progress,
)
View animateLottieCompositionAsState.kt
val progress by animateLottieCompositionAsState(
composition,
iterations = LottieConstants.IterateForever,
)
View composition.kt
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
View example.kt
data class Person(val age: Int? = null)
val people = listOf(
Person(5),
Person(null),
)
// Before/Your Dart example equivalent:
val firstAge = people.first { it.age != null }.age
// firstAge is of type Int? even though logically you know that it's Int.
View wifiBroadcastReceiver.kt
context.registerReceiverInScope(scope, WifiManager.WIFI_STATE_CHANGED_ACTION) { intent ->
val state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED)
// Use wifi state here
}
@gpeal
gpeal / Label.kt
Created Mar 14, 2021
Unique Observable Usage
View Label.kt
class Label @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
var inverted by uniqueObservable(false) { refreshDrawableState() }
...
}
View uniqueObservable.kt
/**
* Like Delegates.observable except it only calls the callback when the value actually changes.
*/
public inline fun <T> uniqueObservable(initialValue: T, emitInitial: Boolean = false, crossinline onChange: (value: T) -> Unit): ReadWriteProperty<Any?, T> {
if (emitInitial) onChange(initialValue)
return object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) {
if (oldValue != newValue) onChange(newValue)
}
}
View MyComposable.kt
@Composable
fun MyComposable() {
val viewModel: MyViewModel = mavericksViewModel()
val state by viewModel.collectAsState()
AnotherComposable(
state.value,
onClick = viewModel.onClick()
)
}
View fadeTo.kt
// Will fade myView in or out.
// You can call this repeatedly with the same value and it won't interrupt the ongoing animation.
myView.fadeTo(true)
myView.fadeTo(false)
myView.fadeTo(true, toAlpha = 0.8f)
myView.fadeTo(true, startDelay = 300)
myView.fadeTo(true, duration = 500)