Skip to content

Commit

Permalink
Examples updated to use standard binding. Introduced basic editing to…
Browse files Browse the repository at this point in the history
… show functionality that wasn't possible before (e.g., it was not possible to observe itemDeleted in previous implementation).
  • Loading branch information
Granfalloner committed Nov 14, 2018
1 parent 95d994a commit 021988d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
21 changes: 13 additions & 8 deletions Example/RxRealmDataSources/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -22,9 +22,8 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<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">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<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">
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" rowHeight="47" id="0Fv-lJ-pdU" customClass="PersonCell" customModule="RxRealmDataSources_Example" customModuleProvider="target">
Expand Down Expand Up @@ -58,6 +57,12 @@
</tableView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="9wF-Hp-Zl1" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" id="KsW-LD-wdn"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="9wF-Hp-Zl1" secondAttribute="bottom" id="U79-jT-Gz8"/>
<constraint firstAttribute="trailing" secondItem="9wF-Hp-Zl1" secondAttribute="trailing" id="Z66-lL-rSb"/>
<constraint firstItem="9wF-Hp-Zl1" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" id="pAN-54-93Z"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="bPo-jC-4xi"/>
<connections>
Expand Down Expand Up @@ -141,7 +146,7 @@
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="e5b-i0-Sf8" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="NYx-HQ-0Jl">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
Expand All @@ -168,7 +173,7 @@
<rect key="frame" x="0.0" y="35" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="TUc-X7-1yK" id="oFC-nh-48l">
<rect key="frame" x="0.0" y="0.0" width="342" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="341" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<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">
Expand All @@ -188,7 +193,7 @@
<rect key="frame" x="0.0" y="79" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="iET-qa-wxc" id="CDi-eZ-kOd">
<rect key="frame" x="0.0" y="0.0" width="342" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="341" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<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">
Expand Down
33 changes: 31 additions & 2 deletions Example/RxRealmDataSources/CollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CollectionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(CollectionViewController.handleLongGesture(gesture:)))
collectionView.addGestureRecognizer(longPressGesture)

// create data source
let dataSource = RxCollectionViewRealmDataSource<Lap>(cellIdentifier: "Cell", cellType: LapCollectionCell.self) {cell, ip, lap in
cell.customLabel.text = "\(ip.row). \(lap.text)"
Expand All @@ -36,7 +39,7 @@ class CollectionViewController: UIViewController {

// bind to collection view
laps
.bind(to: collectionView.rx.realmChanges(dataSource))
.bind(to: collectionView.rx.items(dataSource: dataSource))
.disposed(by: bag)

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

collectionView.rx.dataSource.methodInvoked(#selector(UICollectionViewDataSource.collectionView(_:moveItemAt:to:)))
.subscribe(onNext: { a in
guard let from = a[1] as? IndexPath, let to = a[2] as? IndexPath else { return }
try! realm.write {
let laps = realm.objects(Timer.self).first!.laps
laps.swapAt(from.row, to.row)
}
})
.disposed(by: bag)

// demo inserting and deleting data
data.start()
}
}

@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case .began:
guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else { break }
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
data.stop()
case .changed:
collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
case .ended:
collectionView.endInteractiveMovement()
data.start()
default:
collectionView.cancelInteractiveMovement()
data.start()
}
}
}
6 changes: 5 additions & 1 deletion Example/RxRealmDataSources/DataRandomizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Int {

class DataRandomizer {

private let bag = DisposeBag()
private var bag = DisposeBag()

lazy var config: Realm.Configuration = {
var config = Realm.Configuration.defaultConfiguration
Expand Down Expand Up @@ -96,4 +96,8 @@ class DataRandomizer {
})
.disposed(by: bag)
}

func stop() {
bag = DisposeBag()
}
}
23 changes: 22 additions & 1 deletion Example/RxRealmDataSources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = editButtonItem

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

// bind to table view
laps
.bind(to: tableView.rx.realmChanges(dataSource))
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)

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

tableView.rx.itemDeleted.asObservable()
.subscribe(onNext: { indexPath in
try! realm.write {
let laps = realm.objects(Timer.self).first!.laps
realm.delete(laps[indexPath.row])
}
})
.disposed(by: bag)

// demo inserting and deleting data
data.start()
}

override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.setEditing(editing, animated: animated)

if editing {
data.stop()
} else {
data.start()
}
}
}

0 comments on commit 021988d

Please sign in to comment.