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

ReSwift 101

ReSwift 101

Talk I gave at May 29th, 2019 at the UIKonf Unconference

Avatar for xavierLowmiller

xavierLowmiller

May 29, 2019

More Decks by xavierLowmiller

Other Decks in Programming

Transcript

  1. About me — ! iOS developer since ~2013 — In

    my free time I — " enjoy bouldering — # host a pub quiz — $ travel a lot — % Programming languages & developer tools — ❤ Swift is my favorite — ✅ Loooove green checkmarks in tests — ( Twitter: @xavierLowmiller
  2. Agenda 1. Basics 1. Entities 2. Observing 3. Actions 2.

    Testing 1. Observers 2. Reducers 3. About the library 4. Closing Thoughts
  3. State struct AppState: StateType, Equatable { var todos: [Todo] =

    [] } let store = Store(reducer: appReducer, state: AppState())
  4. Observing override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) store.subscribe(self) }

    override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) store.unsubscribe(self) }
  5. Observing override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) store.subscribe(self) {

    subscription in subscription .select { $0.todos } .skipRepeats() } } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) store.unsubscribe(self) }
  6. Reducers func appReducer(action: Action, state: AppState?) -> AppState { var

    state = // switch action { case let TodoAction.add(todo): if !state.todos.contains(todo) { state.todos.append(todo) } case ...:// } return state }
  7. Observers var actions: [Action] = [] override func setUp() {

    super.setUp() store.dispatchFunction = { self.actions.append($0) } }
  8. Observers func testTappingTheDeleteButtonShouldDispatchARemoveAction() { // Given XCTAssert(actions.isEmpty) viewController.todo = Todo()

    // When viewController.deleteButton.sendActions(for: .touchUpInside) // Then XCTAssertEqual(actions.count, 1) guard let action = actions[0] as? TodoAction, case let TodoAction.remove(todo) = action else { XCTFail("Wrong Action fired"); return } XCTAssertEqual(todo, viewController.todo) }
  9. Reducers func testReducerRemoveShouldRemoveAnExistingTodo() { // Given let state = AppState()

    let todo = Todo() var newState = appReducer(action: TodoAction.add(todo), state: state) XCTAssertEqual(newState.todos.count, 1) // When let action = TodoAction.remove(todo) newState = appReducer(action: action, state: newState) // Then XCTAssert(newState.todos.isEmpty) }
  10. cloc ReSwift 14 text files. 14 unique files. 1 file

    ignored. ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Swift 12 136 320 343 C/C++ Header 1 4 10 3 ------------------------------------------------------------------------------- SUM: 13 140 330 346 -------------------------------------------------------------------------------
  11. cloc ReSwiftTests 12 text files. 12 unique files. 1 file

    ignored. ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Swift 11 316 225 955 ------------------------------------------------------------------------------- SUM: 11 316 225 955 -------------------------------------------------------------------------------
  12. Closing Thoughts — Really small lib (~350 LoC Sources, ~950

    LoC Tests) — Try building something small with ReSwift (it's fun!)