pavi2410 Profile picture
(づ ̄ ³ ̄)づ Computerphile. 👨‍💻 Co-founder, @KodularIO. Building @inkspace_app https://t.co/aEuy0C5lsz

Feb 20, 2022, 10 tweets

#AndroidDev Recipe: How to cook "complex" UI logic in a #JetpackCompose project? 🧑‍🍳

Solution: Type-safe reducer using Kotlin magic 🪄

🧵 Thread

Ingredients:

1️⃣ useCompose's react module which has useReducer hook

2️⃣ Create a data class to hold the state

3️⃣ Create a sealed interface to describe Actions in a type-safe way

4️⃣ Define the initial state

5️⃣ Use the useReducer hook

💡 What's a reducer? It's a function that takes a state and an action to produce a new state.

In Kotlin terms, it is a function of the type `reducer: (S, A) -> S`

6️⃣ Bake the UI 🧁

useReducer returns a pair of current state and a dispatch function

To read state, use the state variable
To dispatch an action, use dispatch function with the Action type

7️⃣ Decorating it all together 😋

🙏 Thank you

and

"Use Compose" 😆

github.com/pavi2410/useCo…

Thanks @carbon_app for the beautiful inages 🤩

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling