android and flux: it’s a match! - hwsw · android and flux: it’s a match! attila polacsek...

43
Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge

Upload: others

Post on 18-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

Android and Flux: It’s a match!

Attila PolacsekSenior Android Developer | Supercharge

Page 2: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

INTRO

Page 3: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

INTRO

Page 4: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

WHAT IS FLUX?

Page 5: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

What is FLUX?● Application architecture● Pattern, not a framework● Created by Facebook as a

successor of client-side MVC● Based on unidirectional data flow

WHAT IS FLUX?

Page 6: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

Action Controller View

View

View

View

View

View

Model

Model

Model

Model

Model

Model

WHAT IS FLUX?

MVC Architecture

Page 7: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

Action Dispatcher Store View Action

Action

Action

Action

Action

Action

Action

Action

Action

View

View

View

View

View

View

Store

e.g. touch eventse.g. push notification

via FCM

WHAT IS FLUX?

FLUX Architecture

Page 8: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

WHAT IS FLUX?

MVC Architecture

FLUX Architecture

Page 9: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

WHAT IS FLUX?

Page 10: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

WHAT IS FLUX?

Page 11: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

COMPONENTS● Store● Action● Dispatcher● View

WHAT IS FLUX?

Action Dispatcher Store View

Action

Page 12: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

STORE● Holds the application data● Immutable from the outside● Produces a change event

WHAT IS FLUX?

Action Dispatcher Store View

Action

Page 13: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

● Actions data objects● They define the internal API● Action Creators are methods● Send action to dispatcher

WHAT IS FLUX?

Action Dispatcher Store View

Action

ACTION

Page 14: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

DISPATCHER● Receives all actions● Broadcast them to stores● Invoke callbacks in specific order

WHAT IS FLUX?

Action Dispatcher Store View

Action

Page 15: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

// Facebook's own example for waitFor: CityStore.dispatchToken = flightDispatcher.register(function(payload) { if (payload.actionType === 'country-update') { // `CountryStore.country` may not be updated. flightDispatcher.waitFor([CountryStore.dispatchToken]); // `CountryStore.country` is now guaranteed to be updated.

// Select the default city for the new country CityStore.city = getDefaultCityForCountry(CountryStore.country); } });

WHAT IS FLUX?

Page 16: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

CONS● All UI state is handled in stores● Stores are too dependent● Dispatcher is everywhere● Not cancelable

WHAT IS FLUX?

Page 17: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

WHAT IS FLUX?

Page 18: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

On top of RxJava2

Page 19: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

SCFlux● Implemented on top of RxJava2● Dependencies between stores

are looser● State persistence is extracted

from the stores● Reducers functions mutate the

state

HOW WE IMPLEMENTED IT

Reducer State Store View

Action

ActionReducer

Reducer

dispatch

trigger

dispatch

trigger

newstate subscribe

newstate

state+action

Page 20: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

STORE● Minor differences● Returns a Flowable instead of

callbacks● State persistence is extracted

class ConversationStoreImpl @Inject constructor(repository: MessageRepository) : ConversationStore { private val listFlowable: Flowable<List<Message>>

init { listFlowable = Flowable.fromCallable(repository::findAll) .repeatWhen { completed -> completed.zipWith( repository.dataUpdatedEvents(), BiFunction<Any, Any, Any> { item, _ -> item } ) } .share() }

override fun getList(): Flowable<List<Message>> { return listFlowable } }

HOW WE IMPLEMENTED IT

Page 21: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

ACTION● Type comes from the class● Payload is optional

data class CreateMessage( val msg: String, val status: MessageStatus) : Action()

data class UpdateMessage( val id: String, val status: MessageStatus) : Action()

interface MessageActions { fun sendMessage(message: String): Completable fun updateMessage(id: String, status: MessageStatus): Completable }

ACTION CREATORS● Describes the action pipeline● Composes API calls and actions● Returns an Rx Comletable

HOW WE IMPLEMENTED IT

Page 22: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

DISPATCHER● Without waitFor it was just a

dumb pipe● Completely removed● Event pipes are declared with

Rx operators● State is modified directly with

actions and a reducers

fun changeMessageStatus(messages: List<Message>, action: UpdateMessage): List<Message> { return messages.map { message -> if (message.id == action.id) { message.copy(status = action.status) } else { message } } }

HOW WE IMPLEMENTED IT

Page 23: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 24: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 25: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 26: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 27: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 28: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 29: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 30: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 31: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 32: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 33: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 34: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 35: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 36: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 37: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 38: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 39: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 40: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

PROS● Unidirectional data flow● State mutation only via actions● Single source of truth: store● Automatic re-render after state

update

Page 41: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

HOW WE IMPLEMENTED IT

Page 42: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application

Thanks for your attention

Attila PolacsekSenior Android Developer | Supercharge

Contact us!

Page 43: Android and Flux: It’s a match! - HWSW · Android and Flux: It’s a match! Attila Polacsek Senior Android Developer | Supercharge.INTRO. INTRO. WHAT IS FLUX? What is FLUX? Application