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

Use Redux To Rock Your Mobile Development

Elvis Lin
October 29, 2017

Use Redux To Rock Your Mobile Development

Use Redux architecture in mobile development

Elvis Lin

October 29, 2017
Tweet

More Decks by Elvis Lin

Other Decks in Programming

Transcript

  1. MVP • 將 UI 更更新的邏輯抽到 Presenter • Activity / ViewController

    只負責 event 轉發跟畫⾯面更更新 • Presenter 不相依於 Framework 的程式 • Reference: https://speakerdeck.com/elvismetaphor/ introduction-to-mv-whatever-in-android
  2. MVVM • 將 UI 的狀狀態保存在 ViewModel 中 • Activity /

    ViewController 透過 Data binding 或 Rx (observable) 跟 ViewModel 溝通 • ViewModel 不相依於 Framework 的程式 • Reference: https://speakerdeck.com/elvismetaphor/ implements-mvvm-in-android-with-data-binding
  3. THREE PRINCIPLES (REDUX) • Single source of truth
 The state

    of your whole application is stored in an object tree within a single store. • State is read-only
 The only way to change the state is to emit an action. • Changes are made with pure functions
 Pure reducers specify how the state tree is transformed.
  4. SINGLE SOURCE OF TRUTH • Traditional Style • 多個 Manager

    / DateStore 保存、維護所有資 料 • Redux Style • ⼀一個 Store/State 保存所有資料
  5. STATE IS READ-ONLY • Traditional Style • Data/State是可變的(mutable) • Redux

    Style • State 是不可變的(immutable),⽤用新的 State 取代舊的 state
  6. CHANGES ARE MADE WITH PURE FUNCTIONS • Functional: • Only

    generate output from input, doesn’t modify other states. • Y = f(x), • ex: squareRoot(4) = 2
  7. CHANGES ARE MADE WITH PURE FUNCTIONS • Traditional Style •

    改變 state 的 method 是有 side-effect 的 • Redux Style • Reducer 接受 current state,輸出 new state
  8. REDUX 的元件 • Store: dispatch action, and notify the subscriber

    • State: key the data of the app • Action: Define which should be modify, and provide necessary parameter • Reducer: Modify the state by the action • View/Controller/Presenter: Get the event and dispatch it to store
  9. REDUX 更更新的流程(FLOW) • Store object holds and maintains the state

    • To change the state application can dispatch actions to Store • For each action Store compute new state using pure function f(s, a) (called reducer) • Store replace current state with new state
  10. REDUCTOR • Use Annotation to create boilerplate code • All

    the boilerplate is written by a machine
  11. STATE @CombinedState
 @AutoValue
 public abstract class AppState {
 public abstract

    List<Note> notes();
 
 public abstract NotesFilter filter();
 
 public static TypeAdapter<AppState> typeAdapter(Gson gson) {
 return new AutoValue_AppState.GsonTypeAdapter(gson);
 }
 
 public List<Note> getFilteredNotes() {
 List<Note> notes = this.notes();
 NotesFilter filter = this.filter();
 return Utils.filter(ConsPStack.from(notes), note ->
 filter == NotesFilter.ALL
 || filter == NotesFilter.CHECKED && note.checked
 || filter == NotesFilter.UNCHECKED && !note.checked);
 }
 }

  12. ACTION @ActionCreator
 public interface NotesActions {
 String ADD_ACTION = "ADD_ITEM";


    String TOGGLE = "TOGGLE";
 String REMOVE_ITEM = "REMOVE_ITEM";
 
 @ActionCreator.Action(NotesActions.ADD_ACTION)
 Action add(int id, String content);
 
 @ActionCreator.Action(NotesActions.REMOVE_ITEM)
 Action remove(int id);
 
 @ActionCreator.Action(NotesActions.TOGGLE)
 Action toggle(int id);
 }

  13. REDUCER @AutoReducer
 public abstract class NotesListReducer implements Reducer<List<Note>> {
 


    @AutoReducer.InitialState
 List<Note> initialState() {
 return TreePVector.empty();
 }
 
 @Action(value = NotesActions.ADD_ACTION,
 from = NotesActions.class)
 public List<Note> add(List<Note> state, int id, String content) {
 return TreePVector.from(state).plus(new Note(id, content, false));
 }
 
 
 
 public static NotesListReducer create() {
 return new NotesListReducerImpl();
 }
 }
  14. REDUCER • Each method of a reducer is a pure

    functional method • So, It’s a piece of cake!
  15. REFERENCE • Redux for Android. Predictable state container library for

    Java/Android
 https://github.com/Yarikx/reductor • Unidirectional data flow on Android using Kotlin
 https://speakerdeck.com/cesarvaliente/unidirectional-data- flow-on-android-using-kotlin