Skip to content

Commit

Permalink
Final tweaks & slides
Browse files Browse the repository at this point in the history
  • Loading branch information
fpillet committed Sep 21, 2017
1 parent 9dce5b8 commit 592b4f6
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ example(of: "shareReplayLatestWhileConnected()", run: false) {
// ------------------------------------------------------------------------------------
// 4. share(replay:scope:)
// ------------------------------------------------------------------------------------
example(of: "share(replay:scope:)", run: false) {
example(of: "share(replay:scope:)", run: true) {
let observable = Observable<Int>.timer(0, period: 1, scheduler: MainScheduler.instance)
.share(replay: 3, scope: .whileConnected)

Expand Down
Binary file not shown.
6 changes: 3 additions & 3 deletions RxFeedbackDemo/RxFeedbackDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@
3D8FB31F1F6FA88900A4D2F6 /* RxFeedbackDemo */ = {
isa = PBXGroup;
children = (
3D8FB3201F6FA88900A4D2F6 /* AppDelegate.swift */,
3D8FB3321F6FAA9A00A4D2F6 /* PlaybackState.swift */,
3D8FB3341F6FAAE000A4D2F6 /* PlaybackCommand.swift */,
3D8FB3221F6FA88900A4D2F6 /* ViewController.swift */,
3D8FB3201F6FA88900A4D2F6 /* AppDelegate.swift */,
3D8FB3241F6FA88900A4D2F6 /* Main.storyboard */,
3D8FB3271F6FA88900A4D2F6 /* Assets.xcassets */,
3D8FB3291F6FA88900A4D2F6 /* LaunchScreen.storyboard */,
3D8FB32C1F6FA88900A4D2F6 /* Info.plist */,
3D8FB3321F6FAA9A00A4D2F6 /* PlaybackState.swift */,
3D8FB3341F6FAAE000A4D2F6 /* PlaybackCommand.swift */,
);
path = RxFeedbackDemo;
sourceTree = "<group>";
Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions RxFeedbackDemo/RxFeedbackDemo/PlaybackState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import Foundation
//
// PlaybackState: the possible states of our player state machine
//
enum PlaybackState: Equatable {
enum PlaybackState {
case stopped
case playing(Int,Date) // media index, end date
case paused(Int,Double) // media index, remaining seconds
}

extension PlaybackState {
extension PlaybackState: Equatable {
var isPaused: Bool {
if case .paused(_,_) = self { return true }
return false
Expand Down
8 changes: 5 additions & 3 deletions RxFeedbackDemo/RxFeedbackDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ViewController: UIViewController {
bindUI,

// Playback command generator that runs playback and loops on playlists
generateCommandsFromPlaybackState
generateCommandsFromPlaybackStates
]
)
.distinctUntilChanged()
Expand Down Expand Up @@ -127,8 +127,10 @@ class ViewController: UIViewController {
}
}

func generateCommandsFromPlaybackState(states: ObservableSchedulerContext<PlaybackState>) -> Observable<PlaybackCommand> {
return states.source.flatMapLatest { [weak self] (state: PlaybackState) -> Observable<PlaybackCommand> in
func generateCommandsFromPlaybackStates(states: ObservableSchedulerContext<PlaybackState>) -> Observable<PlaybackCommand> {
return states.source.flatMapLatest {
[weak self] (state: PlaybackState) -> Observable<PlaybackCommand> in

guard let this = self else { return .empty() }

switch state {
Expand Down
158 changes: 154 additions & 4 deletions Slides/RxClassroom2017.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
## RxSwift Classroom
![fit](images/rxlogo.png)
## [fit] RxSwift Classroom

### FrenchKit 2017
### [fit]           FrenchKit 2017

Florent Pillet

**@fpillet**

Co-author, RxSwift book from Ray Wenderlich

---
## Agenda
Expand Down Expand Up @@ -77,7 +84,8 @@ When you only want to see the freshest results

* Replays the last N emitted elements
* Event if # subscribers fell to zero
* Experiment with the playground!

👉 Experiment with the playground!

---
### `shareReplayLatestWhileConnected`
Expand All @@ -92,6 +100,148 @@ When you only want to see the freshest results
* Control whether buffering 'sticks' when subscribers drop to zero

---
## Introducing RxFeedback
# Introducing RxFeedback


---
# Introducing RxFeedback

https://github.com/kzaher/RxFeedback

`pod 'RxFeedback'`

---
### State machines

* Describe states of a system
* Transition description semantics
* How do you describe feedback?

---
### Feedback?

* A new state triggers a set of inputs
* Inputs produce events, sooner of later
* Events can be turned into a new state

---
### RxFeedback: all-in-one solution

![inline](images/RxFeedback.png)

---
### RxFeedback: all-in-one solution

* Describes the entire system: reducer, inputs, events
* Flexible inputs adjusting for each state
* Model circular dependencies

---
### Example project

* Simple media player to cycle between images
* A few states, easy to understand

👉 open `RxFeedbackDemo` project
👉 run app

---
### States & events

* I named events 'commands', better suited to this project

👉 examine `PlaybackStates.swift`
👉 examine `PlaybackCommand.swift`

---
### RxFeedback: a single operator (mostly)

* `Observable.system`
* Describes a complete system
* Event reducer
* Scheduler
* Feedback loops

---
### Reducer

* Barbaric name for a simple concept
* Reduces a command to a new state
* Try resist performing side effects here

---
### Reducer

* `reducePlaybackStateFromCommand` function
* simple switch-case clearly describes what happens

---
### Scheduler

* Use an asynchronous scheduler
* Prevents issue when doing immediate transitions
* RxFeedback does it for you, mostly

---
### Feedback loops

* One or more closures, called only *once*
* Observe a sequence of states
* Produce a sequence of comments

---
### Feedback loops

* `generateCommandsFromPlaybackStates`
* Observe the sequence of states
* On change, produce either `empty()` (empty sequence)
* ... or a timer that triggers to play the next image

---
### Feedback loops

* `flatMapLatest` 💪

---
### UI binding

* `bindUI`
* 😱
* Convoluted but not that complicated


---
### UI binding

* "observers" update UI from state
* "emitters" produce commands from user input
* commands go feed the beast

---
### Putting it all together

* Initial state goes into feedback loop
* Our example does nothing
* "Play" button triggers initial `playing` state
* `playing` initiates timer
* `playing` enables `pause` button
* It all goes on from

---
# Final words


---
# [fit] Discussion / Q&A




I'm Florent Pillet

**@fpillet** on Twitter, Slack, gmail and everywhere else



Co-author, RxSwift book from Ray Wenderlich
[https://store.raywenderlich.com/products/rxswift](https://store.raywenderlich.com/products/rxswift)
Binary file modified Slides/RxClassroom2017.pdf
Binary file not shown.
Binary file added Slides/images/RxFeedback.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Slides/images/rxlogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 592b4f6

Please sign in to comment.