1 of 30

Composing Magic

MVI with Kotlin Flow & Jetpack Compose

2 of 30

Next -> Next -> Modern Android Architecture

Kotlin StateFlow

Kotlin StateFlow

MVI

Compose

Kotlin Flow

Suspend

ViewState

Coroutines

Suspend

Coil

3 of 30

RxJava

“Use RxJava scan() to apply reduce() to the previous state of app and return the new state.”

4 of 30

5 of 30

MVVM

ViewModel

View

UI Event

UI Event

UI Event

State

Repo

Logic

Room DB

Data Binding to update UI

6 of 30

7 of 30

MVI

View/Activity

UI Event

UI Event

UI Event

New State

Reducer

Accumulator / Actor

Room DB

New ViewState to Update UI

ViewModel

Intents

Old

Change

Render

Listening (V)

Rendering (V)

Business Logic (M)

8 of 30

9 of 30

10 of 30

You can think of a channel as a pipe between two coroutines. That pipe facilitates the transfer of information between those two coroutines. One coroutine can send information on that pipe. The other coroutine will wait to receive the information. This concept of communication between coroutines is different than threads.

Do not communicate by sharing memory; instead, share state by communicating.

11 of 30

Hot Steam vs Cold Stream

Data

Data

Data

Channel - HOT Streams

Flow - COLD Streams

Data Steam

Data Steam

Data Steam

Data Steam

Data Steam

  1. Where is Data
  2. How many receivers
  3. Laziness

12 of 30

Hot Stream (Kotlin Channel) vs Cold Stream (Kotlin Flow) https://www.stefandouma.nl/reactor-hot-stream-vs-cold-stream/

A radio channel is a hot stream.

  • One running channel for all listeners
  • The stream continues to run if there are no listeners
  • The data the subscriber receives depends on the moment the subscriber subscribes

A CD is a cold stream

  • One new instance of a channel per listener
  • Stream instance is stopped when user disconnects
  • Data is initiated from the beginning for every subscribe

13 of 30

Flow - Cold Stream - No Synchronization

  • The data source is inactive before and after being used.
  • Don’t need concurrency or synchronization

Just like a sequence, a flow represents a cold stream of sequential values - ‘asynchronous data streams’.

Channels - Hot Stream - Needs Synchronization

  • The data source always exists.
  • Hot streams great synchronization

Channels are a great fit to model data sources that are intrinsically hot, data sources that exist without application’s requests for them: incoming network connections, event streams, etc.

Channels are not scoped to a specific lifecycle like Flow. A Channel survives across screens and lifecycles!

14 of 30

The (MV) Intent Channel

15 of 30

Kotlin StateFlow to ShardFlow

  • Synchronous (rendezvous) flow-based event listeners (emit event, suspend until all listeners process it, see #1901).
  • Replay & buffering of last events (important for non-suspend context, buffer cushions fast producers, see #1761).
  • Advanced state sharing (custom conflation, no initial value, etc. see comments in #1973).
  • Get access to “the current cache snapshot” which for non-live display purposes (confirmation dialog window), etc.
  • Custom “upstream” connection logic by tracking the number of active collectors (see discussion in #1086).

16 of 30

  • There is a need to have a Flow implementation that is hot and shares emitted values among all collectors that subscribe to it.
  • Its design generalizes StateFlow beyond the narrow set of use-case it supports (see #1973).
  • It will replace all sorts of BroadcastChannel implementations and serves as a basis for the design of shareIn operator.
  • The simplest version of sharing operator builds a combination of a SharedFlow that downstream collects from and an upstream flow that emits into this SharedFlow. See #2047 for details on sharing operators.

17 of 30

Transforming Android Arch Components

Compose

Kotlin Flow

Kotlin Coroutines

Suspend

18 of 30

Review of App.

19 of 30

Activity/XML to Compose (MainComposeUI.kt)

20 of 30

UI Modifiers

Jetpack Compose

SwiftUI

21 of 30

UI Constraints (Motion Editor) (AddWordComposeUI.kt)

22 of 30

Nav is just Flipp’n Screens (MainActivity.kt)

23 of 30

Set Fun{} on Interactive UI

doSomething = f{}

24 of 30

LiveData to Kotlin Flow (WordDao.kt)

25 of 30

Dagger to Dagger Hilt (StateModule.kt)

26 of 30

Coil (MainComposeUI)

27 of 30

StateFlow/SharedFlow/?Flow (RxJava)

28 of 30

29 of 30

iOS & Combine Framework https://www.youtube.com/watch?v=4GjXq2Sr55Q&feature=youtu.be iOS CS 193P @ Stanford

30 of 30

Questions

-- @Biocodes --