Android code piecesπŸ“±πŸ’»
23 subscribers
39 photos
3 videos
6 links
Sample and useful code pieces and tricks for Android and Kotlin
Download Telegram
If you want to iterate over all the entries of a known enum class, you can use the β€˜.values()’ function. If you are working with generic enums, you can use the β€˜enumValues()’ function instead.
JetPack Compose has a very strange
way of describing columns and rows.
The calls to StartScreenMiddleScreen, and EndScreen might happen in any order.

This means you can't, for example, have StartScreen() set some global variable (a side-effect) and have MiddleScreen() take advantage of that change.

Instead, each of those functions needs to be self-contained.
This media is not supported in your browser
VIEW IN TELEGRAM
We are finally getting the ability to animate Lazy list's item position changes in #JetpackCompose just like we did with DiffUtil πŸŽ‰

The Modifier API called `Modifier.animateItemPlacement()` was implemented and merged and will probably be released in an upcoming Compose version.
Ktor client now provides first-class support for retries.
🀩1
Optimizer for ConstraintLayout
The Dispatchers.Unconfined coroutine dispatcher starts a coroutine in the caller thread, but only until the first suspension point.

After suspension it resumes the coroutine in the thread that is fully determined by the suspending function that was invoked.
Explain the difference between Cold Flow and Hot Flow with an example.

Cold Flow πŸ₯Ά

➑️ A cold flow is a data flow that produces values only when there is an active collector.

➑️ Each collector receives the emitted values independently, and they start receiving emissions from the beginning when they subscribe.

➑️ If we subscribe to a cold flow, we will receive all of the emitted values, even if the flow has already completed emitting values.

βœ… When to use?

➑️ Cold flows are typically used for tasks that need to be performed only once, such as loading data from a database or fetching data from a remote server.

➑️ Example

Flow (itself)
Hot Flow πŸ”₯

➑️ A hot flow is a data flow that emits values regardless of whether there are active collectors. It can produce values even if there are no subscribers.

➑️ New collectors joining a hot flow might miss emissions that occurred before they started listening.

➑️ If we subscribe to a hot flow, we will start receiving emissions from the point where the flow is currently at. If the flow has already completed emitting values, we will not receive any emissions.

βœ… When to use?

➑️ Hot flows are typically used for tasks that need to be performed continuously, such as updating a Ul with real-time data.

βœ… Example

StateFlow and SharedFlow
How to observe lifecycle events (onCreate(), onResume(), onStart(),
etc)

Owner CompositionLocal can provide the current LifecycleOwner.
You can use SubComposeLayout to measure and resize Composables.
In version 2.7.0 of the lifecycle-runtime-compose library, you can now execute side effects based on Lifecycle.Event by utilizing LifecycleEventEffect.
Here we have a username field and a button that enables when the username is valid.

It starts off empty and so our state is false. Now when the user starts typing, our state correctly updates and our button becomes enabled.

This is where derivedStateOf comes in. Our state is changing more than we need our UI to update and so derivedStateOf can be used for this to reduce the number of recompositions.

It acts similarly to the Kotlin Flows distinctUntilChanged() operator.