Skip to content

Commit 43d0d03

Browse files
committed
Examples updated to use standard binding. Introduced basic editing to show functionality that wasn't possible before (e.g., it was not possible to observe itemDeleted in previous implementation).
1 parent 58c3dd2 commit 43d0d03

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

Example/RxRealmDataSources/Base.lproj/Main.storyboard

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="e5b-i0-Sf8">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="e5b-i0-Sf8">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
99
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
@@ -22,9 +22,8 @@
2222
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
2323
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2424
<subviews>
25-
<tableView clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="47" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="9wF-Hp-Zl1">
26-
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
27-
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
25+
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="47" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="9wF-Hp-Zl1">
26+
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
2827
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
2928
<prototypes>
3029
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" rowHeight="47" id="0Fv-lJ-pdU" customClass="PersonCell" customModule="RxRealmDataSources_Example" customModuleProvider="target">
@@ -58,6 +57,12 @@
5857
</tableView>
5958
</subviews>
6059
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
60+
<constraints>
61+
<constraint firstItem="9wF-Hp-Zl1" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" id="KsW-LD-wdn"/>
62+
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="9wF-Hp-Zl1" secondAttribute="bottom" id="U79-jT-Gz8"/>
63+
<constraint firstAttribute="trailing" secondItem="9wF-Hp-Zl1" secondAttribute="trailing" id="Z66-lL-rSb"/>
64+
<constraint firstItem="9wF-Hp-Zl1" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" id="pAN-54-93Z"/>
65+
</constraints>
6166
</view>
6267
<navigationItem key="navigationItem" id="bPo-jC-4xi"/>
6368
<connections>
@@ -141,7 +146,7 @@
141146
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="e5b-i0-Sf8" sceneMemberID="viewController">
142147
<toolbarItems/>
143148
<navigationBar key="navigationBar" contentMode="scaleToFill" id="NYx-HQ-0Jl">
144-
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
149+
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
145150
<autoresizingMask key="autoresizingMask"/>
146151
</navigationBar>
147152
<nil name="viewControllers"/>
@@ -168,7 +173,7 @@
168173
<rect key="frame" x="0.0" y="35" width="375" height="44"/>
169174
<autoresizingMask key="autoresizingMask"/>
170175
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="TUc-X7-1yK" id="oFC-nh-48l">
171-
<rect key="frame" x="0.0" y="0.0" width="342" height="43"/>
176+
<rect key="frame" x="0.0" y="0.0" width="341" height="43.5"/>
172177
<autoresizingMask key="autoresizingMask"/>
173178
<subviews>
174179
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="1. Table View Data Source" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FOy-6e-7Rv">
@@ -188,7 +193,7 @@
188193
<rect key="frame" x="0.0" y="79" width="375" height="44"/>
189194
<autoresizingMask key="autoresizingMask"/>
190195
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="iET-qa-wxc" id="CDi-eZ-kOd">
191-
<rect key="frame" x="0.0" y="0.0" width="342" height="43"/>
196+
<rect key="frame" x="0.0" y="0.0" width="341" height="43.5"/>
192197
<autoresizingMask key="autoresizingMask"/>
193198
<subviews>
194199
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="2. Collection View Data Source" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="lyi-Vs-zYj">

Example/RxRealmDataSources/CollectionViewController.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class CollectionViewController: UIViewController {
2424
override func viewDidLoad() {
2525
super.viewDidLoad()
2626

27+
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(CollectionViewController.handleLongGesture(gesture:)))
28+
collectionView.addGestureRecognizer(longPressGesture)
29+
2730
// create data source
2831
let dataSource = RxCollectionViewRealmDataSource<Lap>(cellIdentifier: "Cell", cellType: LapCollectionCell.self) {cell, ip, lap in
2932
cell.customLabel.text = "\(ip.row). \(lap.text)"
@@ -36,7 +39,7 @@ class CollectionViewController: UIViewController {
3639

3740
// bind to collection view
3841
laps
39-
.bind(to: collectionView.rx.realmChanges(dataSource))
42+
.bind(to: collectionView.rx.items(dataSource: dataSource))
4043
.disposed(by: bag)
4144

4245
// bind to vc title
@@ -53,8 +56,34 @@ class CollectionViewController: UIViewController {
5356
.bind(to: rx.title)
5457
.disposed(by: bag)
5558

59+
collectionView.rx.dataSource.methodInvoked(#selector(UICollectionViewDataSource.collectionView(_:moveItemAt:to:)))
60+
.subscribe(onNext: { a in
61+
guard let from = a[1] as? IndexPath, let to = a[2] as? IndexPath else { return }
62+
try! realm.write {
63+
let laps = realm.objects(Timer.self).first!.laps
64+
laps.swapAt(from.row, to.row)
65+
}
66+
})
67+
.disposed(by: bag)
68+
5669
// demo inserting and deleting data
5770
data.start()
5871
}
59-
}
6072

73+
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
74+
switch(gesture.state) {
75+
case .began:
76+
guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else { break }
77+
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
78+
data.stop()
79+
case .changed:
80+
collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
81+
case .ended:
82+
collectionView.endInteractiveMovement()
83+
data.start()
84+
default:
85+
collectionView.cancelInteractiveMovement()
86+
data.start()
87+
}
88+
}
89+
}

Example/RxRealmDataSources/DataRandomizer.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension Int {
1818

1919
class DataRandomizer {
2020

21-
private let bag = DisposeBag()
21+
private var bag = DisposeBag()
2222

2323
lazy var config: Realm.Configuration = {
2424
var config = Realm.Configuration.defaultConfiguration
@@ -96,4 +96,8 @@ class DataRandomizer {
9696
})
9797
.disposed(by: bag)
9898
}
99+
100+
func stop() {
101+
bag = DisposeBag()
102+
}
99103
}

Example/RxRealmDataSources/ViewController.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ViewController: UIViewController {
2323

2424
override func viewDidLoad() {
2525
super.viewDidLoad()
26+
navigationItem.rightBarButtonItem = editButtonItem
2627

2728
// create data source
2829
let dataSource = RxTableViewRealmDataSource<Lap>(cellIdentifier: "Cell", cellType: PersonCell.self) {cell, ip, lap in
@@ -36,7 +37,7 @@ class ViewController: UIViewController {
3637

3738
// bind to table view
3839
laps
39-
.bind(to: tableView.rx.realmChanges(dataSource))
40+
.bind(to: tableView.rx.items(dataSource: dataSource))
4041
.disposed(by: bag)
4142

4243
// bind to vc title
@@ -53,7 +54,27 @@ class ViewController: UIViewController {
5354
.bind(to: rx.title)
5455
.disposed(by: bag)
5556

57+
tableView.rx.itemDeleted.asObservable()
58+
.subscribe(onNext: { indexPath in
59+
try! realm.write {
60+
let laps = realm.objects(Timer.self).first!.laps
61+
realm.delete(laps[indexPath.row])
62+
}
63+
})
64+
.disposed(by: bag)
65+
5666
// demo inserting and deleting data
5767
data.start()
5868
}
69+
70+
override func setEditing(_ editing: Bool, animated: Bool) {
71+
super.setEditing(editing, animated: animated)
72+
tableView.setEditing(editing, animated: animated)
73+
74+
if editing {
75+
data.stop()
76+
} else {
77+
data.start()
78+
}
79+
}
5980
}

0 commit comments

Comments
 (0)