Slide 1

Slide 1 text

Hello, ReactorKit! Suyeol Jeon https://github.com/devxoul

Slide 2

Slide 2 text

Jeon Suyeol StyleShare Inc. Open Source Lover Then URLNavigator RxSwift ObjectMapper

Slide 3

Slide 3 text

Why?

Slide 4

Slide 4 text

Why? Massive View Controller

Slide 5

Slide 5 text

Why? Massive View Controller RxSwift State Managing

Slide 6

Slide 6 text

Massive View Controller https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

Slide 7

Slide 7 text

Massive View Controller https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

Slide 8

Slide 8 text

Massive View Controller https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

Slide 9

Slide 9 text

RxSwift State Managing Cyclic Data Dependencies

Slide 10

Slide 10 text

RxSwift State Managing currentValue increaseValue()

Slide 11

Slide 11 text

RxSwift State Managing currentValue increaseValue() last state

Slide 12

Slide 12 text

RxSwift State Managing currentValue increaseValue() last state result

Slide 13

Slide 13 text

RxSwift State Managing Cyclic Data Dependencies

Slide 14

Slide 14 text

RxSwift State Managing Cyclic Data Dependencies Pagination List operation Value update ...

Slide 15

Slide 15 text

RxSwift State Managing After a while...

Slide 16

Slide 16 text

RxSwift State Managing After a while... Variable PublishSubject PublishRelay<[Item]> Variable BehaviorSubject Variable

Slide 17

Slide 17 text

I wanted to... 1. Avoid Massive View Controller

Slide 18

Slide 18 text

I wanted to... 1. Avoid Massive View Controller 2. Take advantages of RxSwift

Slide 19

Slide 19 text

I wanted to... 1. Avoid Massive View Controller 2. Take advantages of RxSwift 3. Manage states gracefully

Slide 20

Slide 20 text

ReactorKit

Slide 21

Slide 21 text

ReactorKit can... 1. Avoid Massive View Controller ✅

Slide 22

Slide 22 text

ReactorKit can... 1. Avoid Massive View Controller ✅ Separates responsibilities of view and logic

Slide 23

Slide 23 text

ReactorKit can... 1. Avoid Massive View Controller ✅ Separates responsibilities of view and logic View Controller becomes simple

Slide 24

Slide 24 text

ReactorKit can... 2. Take advantages of RxSwift ✅

Slide 25

Slide 25 text

ReactorKit can... 2. Take advantages of RxSwift ✅ Based on RxSwift

Slide 26

Slide 26 text

ReactorKit can... 2. Take advantages of RxSwift ✅ Based on RxSwift All of RxSwift features are available

Slide 27

Slide 27 text

ReactorKit can... 3. Manage states gracefully ✅

Slide 28

Slide 28 text

ReactorKit can... 3. Manage states gracefully ✅ Unidirectional data flow

Slide 29

Slide 29 text

ReactorKit can... 3. Manage states gracefully ✅ Unidirectional data flow Modify states only in reduce()

Slide 30

Slide 30 text

ReactorKit can... 3. Manage states gracefully ✅ Unidirectional data flow Modify states only in reduce() State management became easy

Slide 31

Slide 31 text

ReactorKit can... Be the future ~1.1K ⭐ ~50K downloads ~900 apps https://starcharts.herokuapp.com/ReactorKit/ReactorKit

Slide 32

Slide 32 text

ReactorKit can... Be the future ~1.1K ⭐ ~50K downloads ~900 apps https://starcharts.herokuapp.com/ReactorKit/ReactorKit

Slide 33

Slide 33 text

Basic Concept

Slide 34

Slide 34 text

Basic Concept Abstraction of User Interaction Abstraction of View State

Slide 35

Slide 35 text

Basic Concept Renders view states Handles user interactions ViewController, Cell, ...

Slide 36

Slide 36 text

Basic Concept protocol View { associatedtype Reactor var disposeBag: DisposeBag // gets called when // self.reactor is changed func bind(reactor: Reactor) }

Slide 37

Slide 37 text

Basic Concept protocol StoryboardView { associatedtype Reactor var disposeBag: DisposeBag // gets called when // the view is loaded func bind(reactor: Reactor) } // for Storyboard support

Slide 38

Slide 38 text

Basic Concept Performs business logic Manages states Corresponds to view

Slide 39

Slide 39 text

Basic Concept protocol Reactor { associatedtype Action associatedtype Mutation associatedtype State var initialState: State }

Slide 40

Slide 40 text

Basic Concept Based on RxSwift

Slide 41

Slide 41 text

Data Flow

Slide 42

Slide 42 text

Data Flow

Slide 43

Slide 43 text

Data Flow Action → State ❌

Slide 44

Slide 44 text

Data Flow Action → Mutation → State

Slide 45

Slide 45 text

Data Flow Action → Mutation → State State manipulator

Slide 46

Slide 46 text

Data Flow State manipulator Async-able Action → Mutation → State

Slide 47

Slide 47 text

Data Flow State manipulator Async-able Not exposed to view Action → Mutation → State

Slide 48

Slide 48 text

Data Flow (Action) -> Observable (State, Mutation) -> State

Slide 49

Slide 49 text

Data Flow

Slide 50

Slide 50 text

Data Flow class ProfileViewReactor: Reactor { enum Action { } struct State { } }

Slide 51

Slide 51 text

Data Flow class ProfileViewReactor: Reactor { enum Action { case follow // user interaction } struct State { } }

Slide 52

Slide 52 text

Data Flow class ProfileViewReactor: Reactor { enum Action { case follow // user interaction } struct State { var isFollowing: Bool // view state } }

Slide 53

Slide 53 text

Data Flow class ProfileViewReactor: Reactor { enum Action { case follow // user interaction } struct State { var isFollowing: Bool // view state } } Execute user follow API → Change state

Slide 54

Slide 54 text

Data Flow class ProfileViewReactor: Reactor { enum Action { case follow // user interaction } struct State { var isFollowing: Bool // view state } } Execute user follow API → Change state Async

Slide 55

Slide 55 text

Data Flow class ProfileViewReactor: Reactor { enum Action { case follow // user interaction } enum Mutation { } struct State { var isFollowing: Bool // view state } }

Slide 56

Slide 56 text

Data Flow class ProfileViewReactor: Reactor { enum Action { case follow // user interaction } enum Mutation { case setFollowing(Bool) // change state } struct State { var isFollowing: Bool // view state } }

Slide 57

Slide 57 text

Data Flow

Slide 58

Slide 58 text

Data Flow Tap follow button

Slide 59

Slide 59 text

Data Flow Action.follow

Slide 60

Slide 60 text

Data Flow Action.follow

Slide 61

Slide 61 text

Data Flow UserService.follow()

Slide 62

Slide 62 text

Data Flow UserService

Slide 63

Slide 63 text

Data Flow Observable

Slide 64

Slide 64 text

Data Flow

Slide 65

Slide 65 text

Data Flow Mutation.setFollowing(true)

Slide 66

Slide 66 text

Data Flow Mutation.setFollowing(true)

Slide 67

Slide 67 text

Data Flow isFollowing = true

Slide 68

Slide 68 text

Data Flow Update follow button

Slide 69

Slide 69 text

Data Flow

Slide 70

Slide 70 text

More Examples

Slide 71

Slide 71 text

More Examples

Slide 72

Slide 72 text

Advanced View Communications Testing View and Reactor

Slide 73

Slide 73 text

View Communications ProfileViewController ProfileViewReactor

Slide 74

Slide 74 text

View Communications UICollectionView ProfileViewController ProfileViewReactor

Slide 75

Slide 75 text

View Communications UICollectionView ProfileViewController ProfileViewReactor UserCell

Slide 76

Slide 76 text

View Communications Passing user data Observing button tap

Slide 77

Slide 77 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor

Slide 78

Slide 78 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor 1. User

Slide 79

Slide 79 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor 1. User 2. User

Slide 80

Slide 80 text

View Communications - Passing user data // ProfileViewReactor struct State { }

Slide 81

Slide 81 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? }

Slide 82

Slide 82 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? } // ProfileViewController let cell = collectionView.dequeue... return cell

Slide 83

Slide 83 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? } // ProfileViewController let cell = collectionView.dequeue... if let reactor = self.reactor { } return cell

Slide 84

Slide 84 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? } // ProfileViewController let cell = collectionView.dequeue... if let reactor = self.reactor { cell.user = reactor.currentState.user } return cell

Slide 85

Slide 85 text

View Communications - Observing button tap ProfileView Controller UserCell ProfileView Reactor

Slide 86

Slide 86 text

View Communications - Observing button tap ProfileView Controller UserCell ProfileView Reactor 1. rx.tap

Slide 87

Slide 87 text

View Communications - Observing button tap ProfileView Controller UserCell ProfileView Reactor 2. Action.follow 1. rx.tap

Slide 88

Slide 88 text

View Communications - Observing button tap ProfileView Controller UserCell ProfileView Reactor 2. Action.follow 1. rx.tap Reactive Extension

Slide 89

Slide 89 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { }

Slide 90

Slide 90 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { var buttonTap: ControlEvent { } }

Slide 91

Slide 91 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { var buttonTap: ControlEvent { return self.base.followButton.rx.tap } }

Slide 92

Slide 92 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { var buttonTap: ControlEvent { return self.base.followButton.rx.tap } } // ProfileViewController cell.user = reactor.currentState.user

Slide 93

Slide 93 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { var buttonTap: ControlEvent { return self.base.followButton.rx.tap } } // ProfileViewController cell.user = reactor.currentState.user cell.rx.buttonTap

Slide 94

Slide 94 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { var tap: ControlEvent { return self.base.followButton.rx.tap } } // ProfileViewController cell.user = reactor.currentState.user cell.rx.buttonTap .map { Reactor.Action.follow }

Slide 95

Slide 95 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { var tap: ControlEvent { return self.base.followButton.rx.tap } } // ProfileViewController cell.user = reactor.currentState.user cell.rx.buttonTap .map { Reactor.Action.follow } .bind(to: reactor.action)

Slide 96

Slide 96 text

View Communications - Observing button tap // UserCell extension Reactive where Base: UserCell { var tap: ControlEvent { return self.base.followButton.rx.tap } } // ProfileViewController cell.user = reactor.currentState.user cell.rx.buttonTap .map { Reactor.Action.follow } .bind(to: reactor.action) .disposed(by: self.disposeBag)

Slide 97

Slide 97 text

View Communications UICollectionView ProfileViewController ProfileViewReactor UserCell

Slide 98

Slide 98 text

View Communications UICollectionView ProfileViewController ProfileViewReactor UserCell UserCellReactor

Slide 99

Slide 99 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor

Slide 100

Slide 100 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor UserCell Reactor

Slide 101

Slide 101 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor 1. CellReactor UserCell Reactor

Slide 102

Slide 102 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor 1. CellReactor 2. CellReactor UserCell Reactor

Slide 103

Slide 103 text

View Communications - Passing user data ProfileView Controller UserCell ProfileView Reactor 1. CellReactor 2. CellReactor UserCell Reactor

Slide 104

Slide 104 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? }

Slide 105

Slide 105 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? var userCellReactor: UserCellReactor? }

Slide 106

Slide 106 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? var userCellReactor: UserCellReactor? } // ProfileViewController cell.user = reactor.currentState.user

Slide 107

Slide 107 text

View Communications - Passing user data // ProfileViewReactor struct State { var user: User? var userCellReactor: UserCellReactor? } // ProfileViewController cell.user = reactor.currentState.user cell.reactor = reactor.currentState.userCellReactor

Slide 108

Slide 108 text

Testing View and Reactor What to test? View Reactor

Slide 109

Slide 109 text

Testing View and Reactor What to test? View Action: on user interaction → action sent? Reactor

Slide 110

Slide 110 text

Testing View and Reactor What to test? View Action: on user interaction → action sent? State: on state change → view updated? Reactor

Slide 111

Slide 111 text

Testing View and Reactor What to test? View Action: on user interaction → action sent? State: on state change → view updated? Reactor State: on action receive → state updated?

Slide 112

Slide 112 text

Testing View and Reactor How to test?

Slide 113

Slide 113 text

Testing View and Reactor How to test? Reactor.stub()

Slide 114

Slide 114 text

Testing View and Reactor How to test? Reactor.stub() state: set fake state

Slide 115

Slide 115 text

Testing View and Reactor How to test? Reactor.stub() state: set fake state action: send fake action

Slide 116

Slide 116 text

Testing View and Reactor How to test? Reactor.stub() state: set fake state action: send fake action actions: log received actions

Slide 117

Slide 117 text

Testing View and Reactor - View Action

Slide 118

Slide 118 text

Testing View and Reactor - View Action When: follow button taps Then: sends follow action

Slide 119

Slide 119 text

Testing View and Reactor - View Action // given let reactor = ProfileViewReactor() // when // then

Slide 120

Slide 120 text

Testing View and Reactor - View Action // given let reactor = ProfileViewReactor() reactor.stub.isEnabled = true // when // then

Slide 121

Slide 121 text

Testing View and Reactor - View Action // given let reactor = ProfileViewReactor() reactor.stub.isEnabled = true reactor.stub.state.value.user = User() // when // then

Slide 122

Slide 122 text

Testing View and Reactor - View Action // given let reactor = ProfileViewReactor() reactor.stub.isEnabled = true reactor.stub.state.value.user = User() let viewController = ProfileViewController() viewController.reactor = reactor // when // then

Slide 123

Slide 123 text

Testing View and Reactor - View Action // given let reactor = ProfileViewReactor() reactor.stub.isEnabled = true reactor.stub.state.value.user = User() let viewController = ProfileViewController() viewController.reactor = reactor // when let button = viewController.userCell.followButton // then

Slide 124

Slide 124 text

Testing View and Reactor - View Action // given let reactor = ProfileViewReactor() reactor.stub.isEnabled = true reactor.stub.state.value.user = User() let viewController = ProfileViewController() viewController.reactor = reactor // when let button = viewController.userCell.followButton button.sendActions(for: .touchUpInside) // then

Slide 125

Slide 125 text

Testing View and Reactor - View Action // given let reactor = ProfileViewReactor() reactor.stub.isEnabled = true reactor.stub.state.value.user = User() let viewController = ProfileViewController() viewController.reactor = reactor // when let button = viewController.userCell.followButton button.sendActions(for: .touchUpInside) // then let lastAction = reactor.stub.actions.last XCTAssertEqual(lastAction, .follow)

Slide 126

Slide 126 text

Testing View and Reactor - View State When: following the user Then: button is selected

Slide 127

Slide 127 text

Testing View and Reactor - View State // given let cellReactor = UserCellReactor() // when // then

Slide 128

Slide 128 text

Testing View and Reactor - View State // given let cellReactor = UserCellReactor() cellReactor.stub.isEnabled = true // when // then

Slide 129

Slide 129 text

Testing View and Reactor - View State // given let cellReactor = UserCellReactor() cellReactor.stub.isEnabled = true let cell = UserCell() cell.reactor = cellReactor // when // then

Slide 130

Slide 130 text

Testing View and Reactor - View State // given let cellReactor = UserCellReactor() cellReactor.stub.isEnabled = true let cell = UserCell() cell.reactor = cellReactor // when cellReactor.stub.state.value.isFollowing = true // then

Slide 131

Slide 131 text

Testing View and Reactor - View State // given let cellReactor = UserCellReactor() cellReactor.stub.isEnabled = true let cell = UserCell() cell.reactor = cellReactor // when cellReactor.stub.state.value.isFollowing = true // then XCTAssertTrue(cell.followButton.isSelected)

Slide 132

Slide 132 text

Testing View and Reactor - Reactor State When: receive follow action Then: update following state ProfileView Reactor

Slide 133

Slide 133 text

Testing View and Reactor - Reactor State // given let reactor = ProfileViewReactor() // when // then

Slide 134

Slide 134 text

Testing View and Reactor - Reactor State // given let reactor = ProfileViewReactor() // when reactor.action.onNext(.follow) // then

Slide 135

Slide 135 text

Testing View and Reactor - Reactor State // given let reactor = ProfileViewReactor() // when reactor.action.onNext(.follow) // then let user = reactor.currentState.user XCTAssertEqual(user?.isFollowing, true)

Slide 136

Slide 136 text

Future Ideas Development Documentation Community

Slide 137

Slide 137 text

Development Testing Support SectionReactor AlertReactor ModelReactor Plugins ... Nested stub Dummy reactor ... Building Extensions

Slide 138

Slide 138 text

Documentation Best Practices Translations Code-level Documentation

Slide 139

Slide 139 text

Community RxSwift Slack #reactorkit (English) https://rxswift-slack.herokuapp.com Swift Korea Slack #reactorkit (Korean) http://slack.swiftkorea.org

Slide 140

Slide 140 text

https://reactorkit.io