This file contains hidden or 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 progress by animateLottieCompositionAsState( | |
| composition, | |
| iterations = LottieConstants.IterateForever, | |
| ) |
This file contains hidden or 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 composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart)) |
This file contains hidden or 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
| 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. |
This file contains hidden or 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
| 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 | |
| } |
This file contains hidden or 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 Label @JvmOverloads constructor( | |
| context: Context, | |
| attrs: AttributeSet? = null, | |
| defStyleAttr: Int = 0 | |
| ) : FrameLayout(context, attrs, defStyleAttr) { | |
| var inverted by uniqueObservable(false) { refreshDrawableState() } | |
| ... | |
| } |
This file contains hidden or 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
| /** | |
| * 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) | |
| } | |
| } |
This file contains hidden or 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 MyComposable() { | |
| val viewModel: MyViewModel = mavericksViewModel() | |
| val state by viewModel.collectAsState() | |
| AnotherComposable( | |
| state.value, | |
| onClick = viewModel.onClick() | |
| ) | |
| } |
This file contains hidden or 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
| // 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) |
This file contains hidden or 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
| recyclerView.updatePadding(top = 14.dp.toInt()) |
This file contains hidden or 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
| /** | |
| * Call this function on a dp value and it will return the equivalent | |
| * number of pixels for the current display. | |
| * e.g. 8.dp | |
| */ | |
| val Number.dp get() = toFloat() * (Resources.getSystem().displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT) |