Skip to content

Commit

Permalink
POITableViewController is programatically generated too. Yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottwilliams committed Jun 4, 2017
1 parent 491ba70 commit e3a5067
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 76 deletions.
33 changes: 15 additions & 18 deletions Proper/Controllers/POITableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ import CoreLocation
class POITableViewController: UITableViewController, ProperViewController {
typealias Distance = CLLocationDistance

var stations: Property<[MutableStation]>!
var mapPoint: SignalProducer<Point, NoError>!
var stations: Property<[MutableStation]>
var mapPoint: Property<Point>
let dataSource = POITableDataSource()

internal var connection: ConnectionType = Connection.cachedInstance
internal var disposable = CompositeDisposable()

static let headerViewHeight = CGFloat(55)

init(style: UITableViewStyle, stations: Property<[MutableStation]>, mapPoint: Property<Point>) {
self.stations = stations
self.mapPoint = mapPoint
super.init(style: style)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

/// Returns a producer of Ops with side effects added to manipulate the table and data source with changes
/// described by the table operations. The point where this view's signal chain becomes Very Imperative.
func modifyTable(producer: SignalProducer<[POIViewModel.Op], ProperError>) ->
Expand Down Expand Up @@ -63,7 +73,6 @@ class POITableViewController: UITableViewController, ProperViewController {
}
}

// TODO - In Swift 3, index sets conform to `SetAlgebra`, so we can do this without intermediate index sets.
let deleted = sectionDeletions.subtracting(sectionInsertions)
let inserted = sectionInsertions.subtracting(sectionDeletions)
let reloaded = sectionDeletions.intersection(sectionInsertions)
Expand Down Expand Up @@ -117,15 +126,15 @@ class POITableViewController: UITableViewController, ProperViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// TODO - show vehicle details upon selection
// In the meantime, segue to the station.
performSegue(withIdentifier: "showStation", sender: dataSource.stations[indexPath.section])
parent?.performSegue(withIdentifier: "showStation", sender: dataSource.stations[indexPath.section])
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "stationHeader")
as! POIStationHeaderFooterView
let (station, badge, _) = dataSource.table[section]
let distance = POIViewModel.distanceString(SignalProducer.combineLatest(mapPoint,
station.position.producer.skipNil()))
let position = station.position.producer.skipNil()
let distance = POIViewModel.distanceString(mapPoint.producer.combineLatest(with: position))

header.apply(station: station, badge: badge, distance: distance)
return header
Expand All @@ -134,16 +143,4 @@ class POITableViewController: UITableViewController, ProperViewController {
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return POITableViewController.headerViewHeight
}

// MARK: Segue management
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier ?? "" {
case "showStation":
let station = sender as! MutableStation
let dest = segue.destination as! StationViewController
dest.station = station
default:
return
}
}
}
49 changes: 29 additions & 20 deletions Proper/Controllers/POIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class POIViewController: UIViewController, ProperViewController, UISearchControl
/// Map annotation for the point of interest represented by this view. Only used for static locations.
let annotation = MKPointAnnotation()

var table: POITableViewController?
var mapController: POIMapViewController?
var tableController: POITableViewController!
var mapController: POIMapViewController!

/// The point tracked by the POI view. May be either the user's location or a static point. While the view is
/// visible, this point is from `staticLocation` or `deviceLocation`, depending on whether a static location was
Expand Down Expand Up @@ -61,6 +61,8 @@ class POIViewController: UIViewController, ProperViewController, UISearchControl
return deviceLocation.take(untilReplacement: staticLocation)
}

private let searchScheduler = QueueScheduler(qos: .userInitiated, name: "searchScheduler")

// MARK: Conformances
internal var connection: ConnectionType = Connection.cachedInstance
internal var disposable = CompositeDisposable()
Expand All @@ -81,36 +83,48 @@ class POIViewController: UIViewController, ProperViewController, UISearchControl

// MARK: Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

private func loadMapController() {
let onSelect: Action<MutableStation, (), NoError> = Action { station in
guard let table = self.table else { return SignalProducer.empty }
let section = table.dataSource.index(of: station)
let row = (table.dataSource.arrivals[section].isEmpty) ? NSNotFound : 0
table.tableView.scrollToRow(at: IndexPath(row: row, section: section),
at: .top,
animated: true)
let section = self.tableController.dataSource.index(of: station)
let row = (self.tableController.dataSource.arrivals[section].isEmpty) ? NSNotFound : 0
self.tableController.tableView.scrollToRow(at: IndexPath(row: row, section: section),
at: .top,
animated: true)
return SignalProducer.empty
}

let mapController = POIMapViewController(center: point,
zoom: zoom,
routes: Property(routes),
onSelect: onSelect,
isUserLocation: Property(isUserLocation))
addChildViewController(mapController)
stackView.insertArrangedSubview(mapController.view, at: 0)
NSLayoutConstraint.activate([/*mapController.view.topAnchor.constraint(equalTo: view.topAnchor),*/
mapController.view.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4)])
mapController.view.heightAnchor.constraint(equalTo: stackView.heightAnchor, multiplier: 0.4).isActive = true
mapController.didMove(toParentViewController: self)
self.mapController = mapController
}

private func loadTableController() {
let tableController = POITableViewController(style: .plain,
stations: Property(stations),
mapPoint: Property(point))
addChildViewController(tableController)
stackView.addArrangedSubview(tableController.view)
tableController.view.heightAnchor.constraint(equalTo: stackView.heightAnchor, multiplier: 0.6).isActive = true
tableController.didMove(toParentViewController: self)
self.tableController = tableController
}

override func viewDidLoad() {
super.viewDidLoad()
loadMapController()
loadTableController()

// Clear the title until the signals created in `viewWillAppear` set one.
navigationItem.title = nil
}

let searchScheduler = QueueScheduler(qos: .userInitiated, name: "searchScheduler")

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

Expand Down Expand Up @@ -177,11 +191,6 @@ class POIViewController: UIViewController, ProperViewController, UISearchControl

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier ?? "" {
case "embedPOITable":
let dest = segue.destination as! POITableViewController
table = dest
dest.stations = Property(stations)
dest.mapPoint = point.producer
case "showStation":
let station = sender as! MutableStation
let dest = segue.destination as! StationViewController
Expand Down
41 changes: 3 additions & 38 deletions Proper/UI/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="603"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="750" verticalCompressionResistancePriority="850" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="CSf-iZ-5O3">
<rect key="frame" x="0.0" y="0.0" width="375" height="603"/>
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" verticalCompressionResistancePriority="850" mapType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="mTc-ty-gnj">
<rect key="frame" x="0.0" y="0.0" width="375" height="200"/>
Expand Down Expand Up @@ -184,18 +184,6 @@
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillProportionally" translatesAutoresizingMaskIntoConstraints="NO" id="U8p-rr-iqe">
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
<subviews>
<containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="UDr-cM-Tvs">
<rect key="frame" x="0.0" y="0.0" width="375" height="603"/>
<connections>
<segue destination="Y4w-9g-53p" kind="embed" identifier="embedPOITable" id="RV9-C9-YHM"/>
</connections>
</containerView>
</subviews>
<constraints>
<constraint firstItem="UDr-cM-Tvs" firstAttribute="leading" secondItem="U8p-rr-iqe" secondAttribute="leading" id="Bsv-Mb-8hm"/>
<constraint firstAttribute="trailing" secondItem="UDr-cM-Tvs" secondAttribute="trailing" id="mu5-10-xH0"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
Expand All @@ -216,29 +204,6 @@
</objects>
<point key="canvasLocation" x="2955" y="9"/>
</scene>
<!--POI Table View Controller-->
<scene sceneID="oBj-09-S9f">
<objects>
<tableViewController id="Y4w-9g-53p" userLabel="POI Table View Controller" customClass="POITableViewController" customModule="Proper" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="1oI-ac-QXE">
<rect key="frame" x="0.0" y="0.0" width="375" height="603"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<inset key="separatorInset" minX="56" minY="0.0" maxX="0.0" maxY="0.0"/>
<sections/>
<connections>
<outlet property="dataSource" destination="Y4w-9g-53p" id="rBA-Kj-7E6"/>
<outlet property="delegate" destination="Y4w-9g-53p" id="phU-Yr-7Dg"/>
</connections>
</tableView>
<connections>
<segue destination="BYZ-38-t0r" kind="show" identifier="showStation" id="lR4-DG-6cG"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="3CG-Qi-18H" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3735" y="9"/>
</scene>
<!--Arrivals Table View Controller-->
<scene sceneID="4Sb-s7-PcK">
<objects>
Expand Down Expand Up @@ -700,6 +665,6 @@
<image name="cb-default" width="64" height="64"/>
</resources>
<inferredMetricsTieBreakers>
<segue reference="lR4-DG-6cG"/>
<segue reference="SMx-yQ-t7g"/>
</inferredMetricsTieBreakers>
</document>

0 comments on commit e3a5067

Please sign in to comment.