Skip to content

Instantly share code, notes, and snippets.

View twittemb's full-sized avatar

Thibault Wittemberg twittemb

View GitHub Profile
import Foundation
var shouldApplyPrivacy: Bool = {
#if targetEnvironment(simulator) || DEBUG
return false
#else
return true
#endif
}()
extension DispatchTimeInterval {
var nanoseconds: UInt64 {
switch self {
case .nanoseconds(let value) where value >= 0: return UInt64(value)
case .microseconds(let value) where value >= 0: return UInt64(value) * 1000
case .milliseconds(let value) where value >= 0: return UInt64(value) * 1_000_000
case .seconds(let value) where value >= 0: return UInt64(value) * 1_000_000_000
case .never: return .zero
default: return .zero
}
import SwiftUI
struct DetailView : View {
var body : some View {
// In order for the dismiss to happen, either NavigationView or VStack has to be commented
NavigationView {
VStack {
InnerView()
}
.navigationTitle("Detail")
Button(action: {
self.uiSpin.emit(Event.startCounter)
}) {
Text("\(self.uiSpin.state.isCounterPaused ? "Start": "Stop")")
}
// A SwiftUISpin can also be used to produce SwiftUI bindings:
Toggle(isOn: self.uiSpin.binding(for: \.isPaused, event: .toggle) {
Text("toggle")
}
@ObservedObject
private var uiSpin: SwiftUISpin<State, Event> = {
// previously defined or injected: counterSpin is the Spin that handles our counter business
let spin = SwiftUISpin(spin: counterSpin)
spin.start()
return spin
}()
self.uiSpin.emit(Event.startCounter)
// previously defined or injected: counterSpin is the Spin that handles our counter rules
self.uiSpin = UISpin(spin: counterSpin)
// self.uiSpin is now able to handle UI side effects
// we now want to attach the UI Spin to the rendering function of the ViewController:
self.uiSpin.render(on: self, using: { $0.render(state:) })
// And once the view is ready (in “viewDidLoad” function for instance) let’s start the loop:
self.uiSpin.start()
// the underlying reactive stream will be disposed once the uiSpin will be deinit
func render(state: State) {
switch state {
case .increasing(let value):
self.counterLabel.text = "\(value)"
self.counterLabel.textColor = .green
case .decreasing(let value):
self.counterLabel.text = "\(value)"
self.counterLabel.textColor = .red
}
}
// With RxSwift
Observable
.start(spin: levelsSpin)
.disposed(by: disposeBag)
// With ReactiveSwift
SignalProducer
.start(spin: levelsSpin)
.disposed(by: disposeBag)
let levelsSpin = Spin(initialState: Levels(left: 10, right: 20), reducer: Reducer(levelsReducer)) {
Feedback(effect: leftEffect)
Feedback(effect: rightEffect)
}