Skip to content

Commit fca5046

Browse files
committed
Beefed up demo view controller
Added a UIBarButtonItem with an Action, and show how to observe ongoing actions executing from multiple UI items
1 parent 760639d commit fca5046

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed
Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,78 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9059" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9060" systemVersion="14F1021" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="gBp-2p-Zj2">
33
<dependencies>
44
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9049"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9051"/>
66
</dependencies>
77
<scenes>
8-
<!--View Controller-->
8+
<!--Navigation Controller-->
9+
<scene sceneID="MwZ-pf-NHd">
10+
<objects>
11+
<navigationController id="gBp-2p-Zj2" sceneMemberID="viewController">
12+
<extendedEdge key="edgesForExtendedLayout"/>
13+
<navigationBar key="navigationBar" contentMode="scaleToFill" id="7rg-kL-XY8">
14+
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
15+
<autoresizingMask key="autoresizingMask"/>
16+
</navigationBar>
17+
<connections>
18+
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="dJY-Br-IiE"/>
19+
</connections>
20+
</navigationController>
21+
<placeholder placeholderIdentifier="IBFirstResponder" id="nxZ-xO-ip6" userLabel="First Responder" sceneMemberID="firstResponder"/>
22+
</objects>
23+
<point key="canvasLocation" x="-112" y="408"/>
24+
</scene>
25+
<!--Action demo-->
926
<scene sceneID="tne-QT-ifu">
1027
<objects>
11-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Demo" customModuleProvider="target" sceneMemberID="viewController">
28+
<viewController title="Action demo" id="BYZ-38-t0r" customClass="ViewController" customModule="Demo" customModuleProvider="target" sceneMemberID="viewController">
1229
<layoutGuides>
1330
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1431
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1532
</layoutGuides>
1633
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
17-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
34+
<rect key="frame" x="0.0" y="64" width="600" height="536"/>
1835
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1936
<subviews>
2037
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SZL-3G-VZh">
21-
<rect key="frame" x="277" y="51" width="46" height="30"/>
38+
<rect key="frame" x="277" y="31" width="46" height="30"/>
2239
<animations/>
2340
<state key="normal" title="Button"/>
2441
</button>
42+
<activityIndicatorView hidden="YES" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" hidesWhenStopped="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="OrY-7C-i77">
43+
<rect key="frame" x="290" y="258" width="20" height="20"/>
44+
</activityIndicatorView>
45+
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Doing some work" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ulN-2s-mAd">
46+
<rect key="frame" x="234" y="229" width="133" height="21"/>
47+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
48+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
49+
<nil key="highlightedColor"/>
50+
</label>
2551
</subviews>
2652
<animations/>
2753
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
2854
<constraints>
55+
<constraint firstItem="OrY-7C-i77" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Dyl-A6-bud"/>
56+
<constraint firstItem="OrY-7C-i77" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="KWX-Rs-PVL"/>
2957
<constraint firstItem="SZL-3G-VZh" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="31" id="Np0-fa-c3f"/>
58+
<constraint firstItem="ulN-2s-mAd" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="a5T-qt-gDR"/>
59+
<constraint firstItem="OrY-7C-i77" firstAttribute="top" secondItem="ulN-2s-mAd" secondAttribute="bottom" constant="8" id="eqf-Tp-OJd"/>
3060
<constraint firstItem="SZL-3G-VZh" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="hb0-j9-0rK"/>
3161
</constraints>
3262
</view>
63+
<extendedEdge key="edgesForExtendedLayout"/>
64+
<navigationItem key="navigationItem" title="Action demo" id="6DL-Fh-JcF">
65+
<barButtonItem key="rightBarButtonItem" title="Bar Item" id="7hK-lr-Izl"/>
66+
</navigationItem>
3367
<connections>
68+
<outlet property="activityIndicator" destination="OrY-7C-i77" id="VCS-wT-Lb8"/>
3469
<outlet property="button" destination="SZL-3G-VZh" id="IT1-GJ-Qic"/>
70+
<outlet property="workingLabel" destination="ulN-2s-mAd" id="17Y-xo-0lM"/>
3571
</connections>
3672
</viewController>
3773
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
3874
</objects>
75+
<point key="canvasLocation" x="648" y="408"/>
3976
</scene>
4077
</scenes>
4178
</document>

Demo/Demo/ViewController.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88

99
import UIKit
1010
import RxSwift
11+
import RxCocoa
1112
import Action
1213

1314
class ViewController: UIViewController {
1415
@IBOutlet weak var button: UIButton!
16+
@IBOutlet weak var workingLabel: UILabel!
17+
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
18+
19+
var disposableBag = DisposeBag()
1520

1621
override func viewDidLoad() {
1722
super.viewDidLoad()
1823

24+
// Demo: add an action to a button in the view
1925
let action = CocoaAction { _ in
2026
return create { observer -> Disposable in
2127
// Do whatever work here.
@@ -24,9 +30,36 @@ class ViewController: UIViewController {
2430
return NopDisposable.instance
2531
}
2632
}
27-
2833
button.rx_action = action
34+
35+
// Demo: add an action to a UIBarButtonItem in the navigation item
36+
self.navigationItem.rightBarButtonItem!.rx_action = CocoaAction {
37+
print("Bar button item was pressed, simulating a 2 second action")
38+
return empty().delaySubscription(1, MainScheduler.sharedInstance)
39+
}
40+
41+
// Demo: obseve the output of both actions, spin an activity indicator
42+
// while performing the work
43+
combineLatest(
44+
button.rx_action!.executing,
45+
self.navigationItem.rightBarButtonItem!.rx_action!.executing) {
46+
// we combine two boolean observable and output one boolean
47+
a,b in
48+
return a || b
49+
}
50+
.distinctUntilChanged()
51+
.subscribeNext {
52+
// every time the execution status changes, spin an activity indicator
53+
[weak self] executing in
54+
self?.workingLabel.hidden = !executing
55+
if (executing) {
56+
self?.activityIndicator.startAnimating()
57+
}
58+
else {
59+
self?.activityIndicator.stopAnimating()
60+
}
61+
}
62+
.addDisposableTo(self.disposableBag)
2963
}
30-
3164
}
3265

0 commit comments

Comments
 (0)