Upgrade to Pro — share decks privately, control downloads, hide ads and more …

State Machines and Hopeful Dreams

State Machines and Hopeful Dreams

Talking about Mavericks + Workflow

Rikin Marfatia

June 04, 2022
Tweet

More Decks by Rikin Marfatia

Other Decks in Programming

Transcript

  1. Who am I? DROIDCON SF 2022 @HEYRIKIN • • •

    • Snowboarder 🏂 Amateur Barista ☕️ Android Engineer @ Pinterest 📌 Creators 💅🏽
  2. Why this talk? DROIDCON SF 2022 @HEYRIKIN • • •

    • • Compose is the new hotness Architecture is interesting I'm selfish Mavericks + Workflow are cool Fun to play with Libraries and learn things
  3. Agenda DROIDCON SF 2022 @HEYRIKIN • • • • •

    • • Setting the Stage Problem + Solutions The Journey I built an app in Mavericks I built an app in Workflow The Destination Parting Thoughts
  4. MVI'm Confused DROIDCON SF 2022 @HEYRIKIN • • • •

    Architecture is Hard MVI has won Android Multiple Interpretations of "Correct Architecture" Android Architecture
  5. Libraries DROIDCON SF 2022 @HEYRIKIN • • • • •

    • Mavericks by Airbnb Workflow by Square Future Explorations Decompose + MVIKotlin? Molecule? RIBS?
  6. Comparison Criteria DROIDCON SF 2022 @HEYRIKIN • • • •

    Complexity Opinionated Documentation Testing??
  7. What I've Built DROIDCON SF 2022 @HEYRIKIN • • •

    • • • • HydroHomie Compose App Firebase Backend Three Core Features No Dagger No Modularization Share code if possible
  8. Disclaimers DROIDCON SF 2022 @HEYRIKIN • • • Not a

    Compose Overview My personal experience and preferences Small application
  9. My Personal Preferences DROIDCON SF 2022 @HEYRIKIN • • •

    • • Global App State Embedded Feature States ViewState = Data Class Action Types Enum or Sealed Class
  10. DROIDCON SF 2022 @HEYRIKIN data class AppState( val weekday: Weekday,

    val drinkAmount: Double, val hydrations: List<HydrationState> ) { val hydrationState: HydrationState val streakState: StreakState val settingsState: SettingsState } sealed class AppAction { object Drink : AppAction() object Reset : AppAction() data class UpdateGoal(val goal: Double) : AppAction() data class UpdateDrinkSize(val drinkSize: Double) : AppAction() } AppDomain.kt
  11. Mavericks DROIDCON SF 2022 @HEYRIKIN • • • • •

    Airbnb Library by Gabriel Peal Core Concepts (IMO) MavericksState MavericksViewModel Subscribing to State
  12. MavericksState DROIDCON SF 2022 @HEYRIKIN • • • • •

    Kotlin Data Class Immutable Implement `MavericksState` Derived Properties variables defined in the class body
  13. Derived Properites DROIDCON SF 2022 @HEYRIKIN • • • •

    "Calculated Properties" Never out of sync Subscribe to derived props in your State Won't be part of equals, hashcode, etc.
  14. DROIDCON SF 2022 @HEYRIKIN data class AppState( val count: Float,

    val goal: Float, val percent = count / goal, ) data class AppState( val count: Float, val goal: Float ) { val percent = count / goal } Derived Props
  15. MavericksViewModel DROIDCON SF 2022 @HEYRIKIN • • • • •

    • Generic on MavericksState Exposes a Flow<S> Actions can just be function[s] Two core state mechanics setState {} withState {}
  16. setState {} DROIDCON SF 2022 @HEYRIKIN • • • •

    Reducer function S.() → S Makes it nice to update state, just use copy() Reducer functions are queued on a designated dispatcher withState {} • • • • Access the current state (S) → Unit Guaranteed to run after setState queue Functions are queued up on a designated dispatcher as well
  17. DROIDCON SF 2022 @HEYRIKIN AppViewModel Hydration send(AppAction.Drink) Tap AppState hydrations

    HydrationState HydrationState HydrationState drank - 0.0 hydrationState = hydrations[2] Hydration State drank - 0.0 goal - 64.0 drinkAmount - 8.0 percentage = drank / goal
  18. DROIDCON SF 2022 @HEYRIKIN AppViewModel Hydration collectAsState { it.hydrationState }

    AppState hydrations HydrationState HydrationState HydrationState drank - 8.0 hydrationState = hydrations[2] Hydration State drank - 8.0 goal - 64.0 drinkAmount - 8.0 percentage = drank / goal
  19. DROIDCON SF 2022 @HEYRIKIN AppViewModel Streaks collectAsState { it.streakState }

    AppState hydrations HydrationState HydrationState HydrationState drank - 8.0 streakState = StreakState(hydrations) Hydration State drank - 8.0 goal - 64.0 drinkAmount - 8.0 percentage = drank / goal
  20. Workflow DROIDCON SF 2022 @HEYRIKIN • • • • •

    • • Square Library by Ray Ryan + Zach Klippenstein Tree of Composable State Machines Core Concepts (IMO) Workflow Workflow Actions View "Stuff" Workers
  21. Workflows MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON

    SF 2022 @HEYRIKIN • • • • • • • • State Machine Generic on Four Types Props State Output Rendering render() - Rendering to emit Navigation Graph?
  22. State + Rendering MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT

    NEXT? DROIDCON SF 2022 @HEYRIKIN • • • • Renderings are View State State is Internal State Renderings typically wrap out Internal State Renderings forward actions to the Workflows via lambdas
  23. Props + Output MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT

    NEXT? DROIDCON SF 2022 @HEYRIKIN • • • • Props = Data from Parent Global State → Local State Output = Data for Parent Local State → Global State
  24. Workflow Actions MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT?

    DROIDCON SF 2022 @HEYRIKIN • • • • State Reducers Has access to the current state Respond to events, update state Causes Workflow to render() with updated State
  25. MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON SF

    2022 @HEYRIKIN State Output Props events Rendering
  26. View Layer MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT?

    DROIDCON SF 2022 @HEYRIKIN • • • • • • ViewFactory Rendering → @Composable ComposeViewFactory ComposeRendering WorkflowRendering ViewEnvironment
  27. Workers MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON

    SF 2022 @HEYRIKIN • • • How to communicate to APIs Wrapper around a Flow Emits Output that should feed back into the system
  28. DROIDCON SF 2022 @HEYRIKIN class InitialLoadWorker( private val dates: Dates,

    private val drinkRepository: DrinkRepository ) : Worker<AppState> { override fun run(): Flow<AppState> = flow { val drink = drinkRepository.getDrink(day = dates.today) val appState = AppState( weekday = dates.dayOfWeek.toWeekday(), hydrations = buildList { repeat(HYDRATION_LIMIT) { index -> if (index == dates.dayOfWeek) { add(HydrationState(drank = drink.count, goal = drink.goal)) } else if (index < dates.dayOfWeek) { add(HydrationState(drank = 64.0)) } else { add(HydrationState()) } } } ) emit(appState) } }
  29. MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON SF

    2022 @HEYRIKIN DrinkRepository InitialLoadWorker AppWorkflow StreaksWorkflow HydrationWorkflow SettingsWorkflow Hydration Settings Streaks
  30. MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON SF

    2022 @HEYRIKIN HydrationWorkflow Hydration rendering.action(Drink) Tap HydrationRendering Hydration State drank - 0.0 goal - 64.0 drinkAmount - 8.0 percentage = drank / goal action()
  31. MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON SF

    2022 @HEYRIKIN HydrationWorkflow Tap AppWorklow setOutput(…)
  32. MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON SF

    2022 @HEYRIKIN HydrationWorkflow Hydration WorkflowRendering(HydrationRendering) HydrationRendering Hydration State drank - 8.0 goal - 64.0 drinkAmount - 8.0 percentage = drank / goal action()
  33. Comparison Criteria MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT?

    DROIDCON SF 2022 @HEYRIKIN • • • • Complexity Opinionated Testing?? Documentation
  34. Mavericks MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON

    SF 2022 @HEYRIKIN • • • • Easy to use Somewhat Opinionated Well Documented Some Junit Test Rules
  35. Workflow MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON

    SF 2022 @HEYRIKIN • • • • • Difficult to use Very Opinionated ~Well Documented Have to source / sample dive Extensive Testing Support
  36. Should you use them? MAVERICKS ° WORKFLOW ° CONCLUSION °

    WHAT NEXT? DROIDCON SF 2022 @HEYRIKIN • • • • • Maybe Mavericks might be easier to use Workflow is more comprehensive Building a personal project? Learn from them and build your own
  37. Cool Things MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT?

    DROIDCON SF 2022 @HEYRIKIN • • • • Mavericks —> Workflow Migration Lot's of code Reuse Can learn a lot source diving these libraries Lot's of other libraries to explore, wanna keep using + updating them
  38. Resources MAVERICKS ° WORKFLOW ° CONCLUSION ° WHAT NEXT? DROIDCON

    SF 2022 @HEYRIKIN • • • • • Mavericks Github + Docs Workflow Github + Docs HydroHomie Github Thanks to the Library Maintainers! Thanks to my wife Eesha!
  39. Thanks! DROIDCON SF 2022 @HEYRIKIN @heyrikin on Twitter 🐦 Pinterest

    is hiring 📌 HydroHomie will be release soon 💧