From 443e633a28de62dc15fa7003976c8d2dca3dd658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Alves?= Date: Sat, 21 Oct 2017 22:45:59 +0200 Subject: [PATCH] Use Swfit 4 multiline string literals for Steps code property. So readable <3 --- .../RxGesture-OSX/Base.lproj/Main.storyboard | 68 +++++---- Example/RxGesture-OSX/ViewController.swift | 120 ++++++++++++++-- Example/RxGesture/Base.lproj/Main.storyboard | 92 +++++++----- Example/RxGesture/ViewController.swift | 131 ++++++++++++++++-- 4 files changed, 331 insertions(+), 80 deletions(-) diff --git a/Example/RxGesture-OSX/Base.lproj/Main.storyboard b/Example/RxGesture-OSX/Base.lproj/Main.storyboard index 0c9a0e9..6df4a40 100644 --- a/Example/RxGesture-OSX/Base.lproj/Main.storyboard +++ b/Example/RxGesture-OSX/Base.lproj/Main.storyboard @@ -1,8 +1,11 @@ - + - + + + + @@ -645,7 +648,7 @@ - + @@ -654,8 +657,11 @@ - + + + + @@ -670,51 +676,51 @@ - + - - + + - + - + - + - + - - + + - - + + - + - + - + + - + - + @@ -749,16 +765,20 @@ + - + + + - + + @@ -773,7 +793,7 @@ - + diff --git a/Example/RxGesture-OSX/ViewController.swift b/Example/RxGesture-OSX/ViewController.swift index 2bc6905..0b5d594 100644 --- a/Example/RxGesture-OSX/ViewController.swift +++ b/Example/RxGesture-OSX/ViewController.swift @@ -90,6 +90,10 @@ class MacViewController: NSViewController { nextStepObserver.onNext(.previous) } + @IBAction func nextStep(_ sender: Any) { + nextStepObserver.onNext(.next) + } + func updateStep(_ step: Step, at index: Int) { stepBag = DisposeBag() @@ -104,7 +108,15 @@ class MacViewController: NSViewController { lazy var clickStep: Step = Step( title: "Click the square", - code: "view.rx\n\t.clickGesture()\n\t.when(.recognized)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .clickGesture() + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: CATransform3DIdentity) @@ -121,7 +133,15 @@ class MacViewController: NSViewController { lazy var doubleClickStep: Step = Step( title: "Double click the square", - code: "view.rx\n\t.clickGesture(numberOfClicksRequired: 2)\n\t.when(.recognized)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .clickGesture(numberOfClicksRequired: 2) + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: CATransform3DIdentity) @@ -138,7 +158,15 @@ class MacViewController: NSViewController { lazy var rightClickStep: Step = Step( title: "Right click the square", - code: "view.rx\n\t.rightClickGesture()\n\t.when(.recognized)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .rightClickGesture() + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: CATransform3DIdentity) @@ -155,7 +183,15 @@ class MacViewController: NSViewController { lazy var anyClickStep: Step = Step( title: "Click any button (left or right)", - code: "view.rx\n\t.anyGesture(.click(), .rightClick())\n\t.when(.recognized)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .anyGesture(.click(), .rightClick()) + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: CATransform3DMakeScale(1.5, 1.5, 1.0)) @@ -166,12 +202,21 @@ class MacViewController: NSViewController { .when(.recognized) .subscribe(onNext: { _ in nextStep.onNext(.next) - }).disposed(by: stepBag) + }) + .disposed(by: stepBag) }) lazy var pressStep: Step = Step( title: "Long press the square", - code: "view.rx\n\t.pressGesture()\n\t.when(.began)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .pressGesture() + .when(.began) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateBackgroundColor(to: .red) @@ -188,7 +233,26 @@ class MacViewController: NSViewController { lazy var panStep: Step = Step( title: "Drag the square around", - code: "view.rx\n\t.panGesture()\n\t.when(.changed)\n\t.asTranslation()\n\t.subscribe(onNext: {...})\n\nview.rx\n\t.anyGesture(\n\t\t(.pan(), state: .ended),\n\t\t(.click(), state: .recognized)\n\t)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .panGesture() + .when(.changed) + .asTranslation() + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + + view.rx + .anyGesture( + (.pan(), when: .ended), + (.click(), when: .recognized) + ) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, label, nextStep, stepBag in view.animateTransform(to: CATransform3DIdentity) @@ -216,7 +280,26 @@ class MacViewController: NSViewController { lazy var rotateStep: Step = Step( title: "Rotate the square with your trackpad, or click if you do not have a trackpad", - code: "view.rx\n\t.rotationGesture()\n\t.when(.changed)\n\t.asRotation()\n\t.subscribe(onNext: {...})\n\nview.rx\n\t.anyGesture(\n\t\t(.rotation(), state: .ended),\n\t\t(.click(), state: .recognized)\n\t)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .rotationGesture() + .when(.changed) + .asRotation() + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + + view.rx + .anyGesture( + (.rotation(), when: .ended), + (.click(), when: .recognized) + ) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, label, nextStep, stepBag in view.animateTransform(to: CATransform3DIdentity) @@ -244,7 +327,26 @@ class MacViewController: NSViewController { lazy var magnificationStep: Step = Step( title: "Pinch the square with your trackpad, or click if you do not have a trackpad", - code: "view.rx\n\t.magnificationGesture()\n\t.when(.changed)\n\t.asScale()\n\t.subscribe(onNext: {...})\n\nview.rx\n\t.anyGesture(\n\t\t(.magnification(), state: .ended),\n\t\t(.click(), state: .recognized)\n\t)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .magnificationGesture() + .when(.changed) + .asScale() + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + + view.rx + .anyGesture( + (.magnification(), when: .ended), + (.click(), when: .recognized) + ) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, label, nextStep, stepBag in view.animateTransform(to: CATransform3DIdentity) diff --git a/Example/RxGesture/Base.lproj/Main.storyboard b/Example/RxGesture/Base.lproj/Main.storyboard index f3279ec..f34b135 100644 --- a/Example/RxGesture/Base.lproj/Main.storyboard +++ b/Example/RxGesture/Base.lproj/Main.storyboard @@ -1,19 +1,26 @@ - + - + + + + + + Menlo-Regular + + - + @@ -36,30 +43,39 @@ - - + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + @@ -110,6 +121,11 @@ + + + + + diff --git a/Example/RxGesture/ViewController.swift b/Example/RxGesture/ViewController.swift index 871c9b3..de1e816 100644 --- a/Example/RxGesture/ViewController.swift +++ b/Example/RxGesture/ViewController.swift @@ -72,6 +72,10 @@ class ViewController: UIViewController { nextStepObserver.onNext(.previous) } + @IBAction func nextStep(_ sender: Any) { + nextStepObserver.onNext(.next) + } + func updateStep(_ step: Step, at index: Int) { stepBag = DisposeBag() @@ -86,7 +90,15 @@ class ViewController: UIViewController { lazy var tapStep: Step = Step( title: "Tap the red square", - code: "view.rx\n\t.tapGesture()\n\t.when(.recognized)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .tapGesture() + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: .identity) @@ -103,7 +115,15 @@ class ViewController: UIViewController { lazy var doubleTapStep: Step = Step( title: "Double tap the green square", - code: "view.rx\n\t.tapGesture(numberOfTapsRequired: 2)\n\t.when(.recognized)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .tapGesture(numberOfTapsRequired: 2) + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: .identity) @@ -120,7 +140,15 @@ class ViewController: UIViewController { lazy var swipeDownStep: Step = Step( title: "Swipe the blue square down", - code: "view.rx\n\t.swipeGesture(.down)\n\t.when(.recognized)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .swipeGesture(.down) + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: .identity) @@ -137,7 +165,15 @@ class ViewController: UIViewController { lazy var swipeHorizontallyStep: Step = Step( title: "Swipe horizontally the blue square (e.g. left or right)", - code: "view.rx\n\t.swipeGesture([.left, .right])\n\t.when(.recognized)\n\t.subscribeNext {", + code: """ + view.rx + .swipeGesture([.left, .right]) + .when(.recognized) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: CGAffineTransform(scaleX: 1.0, y: 2.0)) @@ -154,7 +190,16 @@ class ViewController: UIViewController { lazy var longPressStep: Step = Step( title: "Do a long press", - code: "view.rx\n\t.longPressGesture()\n\t.when(.began)\n\t.subscribe(onNext: {...})", + code: """ + view.rx + .longPressGesture() + .when(.began) + .observeOn(MainScheduler.asyncInstance) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, _, nextStep, stepBag in view.animateTransform(to: CGAffineTransform(scaleX: 2.0, y: 2.0)) @@ -172,7 +217,24 @@ class ViewController: UIViewController { lazy var panStep: Step = Step( title: "Drag the square to a different location", - code: "let panGesture = view.rx\n\t.panGesture()\n\t.shareReplay(1)\n\npanGesture\n\t.when(.changed)\n\t.asTranslation()\n\t.subscribe(onNext: {...})\n\npanGesture\n\t.when(.ended)\n\t.subscribe(onNext: {...})", + code: """ + let panGesture = view.rx.panGesture().share(replay: 1) + + panGesture + .when(.changed) + .asTranslation() + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + + panGesture + .when(.ended) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, label, nextStep, stepBag in view.animateTransform(to: .identity) @@ -199,7 +261,24 @@ class ViewController: UIViewController { lazy var rotateStep: Step = Step( title: "Rotate the square", - code: "let rotationGesture = view.rx\n\t.rotationGesture()\n\t.shareReplay(1)\n\nrotationGesture\n\t.when(.changed)\n\t.asRotation()\n\t.subscribe(onNext: {...})\n\nrotationGesture\n\t.when(.ended)\n\t.subscribe(onNext: {...})", + code: """ + let rotationGesture = view.rx.rotationGesture().share(replay: 1) + + rotationGesture + .when(.changed) + .asRotation() + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + + rotationGesture + .when(.ended) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, label, nextStep, stepBag in view.animateTransform(to: .identity) @@ -226,7 +305,24 @@ class ViewController: UIViewController { lazy var pinchStep: Step = Step( title: "Pinch the square", - code: "let pinchGesture = view.rx\n\t.pinchGesture()\n\t.shareReplay(1)\n\npinchGesture\n\t.when(.changed)\n\t.asScale()\n\t.subscribe(onNext: {...})\n\npinchGesture\n\t.when(.ended)\n\t.subscribe(onNext: {...})", + code: """ + let pinchGesture = view.rx.pinchGesture().share(replay: 1) + + pinchGesture + .when(.changed) + .asScale() + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + + pinchGesture + .when(.ended) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, label, nextStep, stepBag in view.animateTransform(to: .identity) @@ -253,7 +349,24 @@ class ViewController: UIViewController { lazy var transformStep: Step = Step( title: "Transform the square", - code: "let transformGestures = view.rx\n\t.transformGestures()\n\t.shareReplay(1)\n\ntransformGestures\n\t.when(.changed)\n\t.asTransform()\n\t.subscribe(onNext: {...})\n\ntransformGestures\n\t.when(.ended)\n\t.subscribe(onNext: {...})", + code: """ + let transformGestures = view.rx.transformGestures().share(replay: 1) + + transformGestures + .when(.changed) + .asTransform() + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + + transformGestures + .when(.ended) + .subscribe(onNext: { _ in + // Do something + }) + .disposed(by: disposeBag) + """, install: { view, label, nextStep, stepBag in view.animateTransform(to: .identity)