Skip to content

Commit

Permalink
Removed custom station annotation / badges
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottwilliams committed Oct 9, 2017
1 parent 9f15fd5 commit b73d028
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 67 deletions.
10 changes: 4 additions & 6 deletions Proper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
26C7FB441ED16D6F006B7101 /* RoutesCollectionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26A0FF001E149AF200FF265A /* RoutesCollectionViewModel.swift */; };
26C7FB451ED16D6F006B7101 /* Badge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2696D8001E7E3B1D00ABD701 /* Badge.swift */; };
26C7FB461ED16D6F006B7101 /* POIStationHeaderFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 267725C51E1AACF200274042 /* POIStationHeaderFooterView.swift */; };
26C7FB471ED16D6F006B7101 /* POIStationAnnotationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26ADB0EA1E187151008F49FF /* POIStationAnnotationView.swift */; };
26C7FB481ED16D6F006B7101 /* POIStationAnnotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26ADB0E61E186A9D008F49FF /* POIStationAnnotation.swift */; };
26C7FB491ED16D6F006B7101 /* StationUpcomingTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26ED26F21E117988007FB0F8 /* StationUpcomingTableViewCell.swift */; };
26C7FB4A1ED16D6F006B7101 /* ArrivalTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26A424E31BD2D3F20035754F /* ArrivalTableViewCell.swift */; };
Expand Down Expand Up @@ -839,7 +838,6 @@
26C7FB191ED16D6F006B7101 /* Connection.swift in Sources */,
26C7FB411ED16D6F006B7101 /* SignalChain.swift in Sources */,
26C7FB431ED16D6F006B7101 /* POIViewModel.swift in Sources */,
26C7FB471ED16D6F006B7101 /* POIStationAnnotationView.swift in Sources */,
26C7FB3C1ED16D6F006B7101 /* RouteStop.swift in Sources */,
26517F5E1F8284FC001710FD /* ArrivalsViewModel.swift in Sources */,
26C7FB331ED16D6F006B7101 /* MutableRoute.swift in Sources */,
Expand Down Expand Up @@ -1011,7 +1009,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_NAME = Proper;
Expand Down Expand Up @@ -1053,7 +1051,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_NAME = Proper;
SDKROOT = iphoneos;
Expand All @@ -1074,7 +1072,7 @@
);
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
INFOPLIST_FILE = "$(SRCROOT)/Proper/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_LDFLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = us.transio.Proper;
Expand All @@ -1096,7 +1094,7 @@
"$(PROJECT_DIR)/Carthage/Build/iOS",
);
INFOPLIST_FILE = "$(SRCROOT)/Proper/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = us.transio.Proper;
PRODUCT_NAME = Proper;
Expand Down
63 changes: 12 additions & 51 deletions Proper/Controllers/POIMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class POIMapViewController: UIViewController, ProperViewController {
let isUserLocation: Property<Bool>
var staticCenter: MKPointAnnotation? = nil

fileprivate var annotationForView = NSMapTable<MKAnnotationView, POIStationAnnotation>()
fileprivate var annotationForStation = [MutableStation: POIStationAnnotation]()
fileprivate var polylines = [MutableRoute: MKPolyline]()
fileprivate var routeForPolyline = [MKPolyline: MutableRoute]()
fileprivate let updateRegionLock = NSLock()
Expand Down Expand Up @@ -103,8 +105,8 @@ class POIMapViewController: UIViewController, ProperViewController {
let diff = Dwifft.diff(prev, next)
for step in diff {
switch step {
case let .insert(idx, station):
self.addAnnotation(for: station, at: idx)
case let .insert(_, station):
self.addAnnotation(for: station)
case let .delete(_, station):
self.deleteAnnotations(for: station)
}
Expand Down Expand Up @@ -151,54 +153,21 @@ class POIMapViewController: UIViewController, ProperViewController {
}).map({ _, _ in () })
}

func annotations(for station: MutableStation) -> [POIStationAnnotation] {
return self.map.annotations.flatMap({ $0 as? POIStationAnnotation })
.filter({ $0.station == station })
}
func stations(within range: CountableClosedRange<Int>) -> [POIStationAnnotation] {
return map.annotations.flatMap({ ($0 as? POIStationAnnotation) })
.filter({ range.contains($0.index) })
}
func stations(from idx: Int) -> [POIStationAnnotation] {
return map.annotations.flatMap({ ($0 as? POIStationAnnotation) })
.filter({ $0.index >= idx })
}

func addAnnotation(for station: MutableStation, at idx: Int) {
func addAnnotation(for station: MutableStation) {
guard let position = station.position.value else {
return
}
let distanceString = POIViewModel.distanceString(self.center.producer.map({ ($0, position) }))
let annotation = POIStationAnnotation(station: station,
locatedAt: position,
index: idx,
distance: distanceString)
stations(from: idx).forEach { $0.index += 1 }
map.addAnnotation(annotation)
annotationForStation[station] = annotation
}

func deleteAnnotations(for station: MutableStation) {
let annotations = self.annotations(for: station)
let idx = annotations.min(by: { $0.index < $1.index }).map({ $0.index })!
map.removeAnnotations(annotations)
self.stations(from: idx+1).forEach { $0.index -= 1 }
}

func reorderAnnotations(withIndex fi: Int, to ti: Int) {
if fi < ti {
self.stations(within: fi...ti).forEach { annotation in
switch annotation.index {
case fi: annotation.index = ti
case _: annotation.index -= 1
}
}
} else {
self.stations(within: ti...fi).forEach { annotation in
switch annotation.index {
case fi: annotation.index = ti
case _: annotation.index += 1
}
}
if let annotation = annotationForStation.removeValue(forKey: station) {
map.removeAnnotation(annotation)
}
}
}
Expand All @@ -207,10 +176,9 @@ class POIMapViewController: UIViewController, ProperViewController {
extension POIMapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? POIStationAnnotation {
let view =
mapView.dequeueReusableAnnotationView(withIdentifier: "stationAnnotation") as? POIStationAnnotationView
?? POIStationAnnotationView(annotation: annotation, reuseIdentifier: "stationAnnotation")
view.apply(annotation: annotation)
let view = mapView.dequeueReusableAnnotationView(withIdentifier: "station") as? MKMarkerAnnotationView ??
MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "station")
annotationForView.setObject(annotation, forKey: view)
return view
}

Expand All @@ -231,15 +199,8 @@ extension POIMapViewController: MKMapViewDelegate {
return MKOverlayRenderer()
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if let station = ((view as? POIStationAnnotationView)?.annotation as? POIStationAnnotation)?.station {
self.parent?.performSegue(withIdentifier: "showStation", sender: station)
}
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

if let annotation = (view as? POIStationAnnotationView)?.annotation as? POIStationAnnotation {
if let annotation = annotationForView.object(forKey: view) {
disposable += onSelect.apply(annotation.station).start()
}
}
Expand Down
11 changes: 1 addition & 10 deletions Proper/Views/POIStationAnnotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@ import MapKit
class POIStationAnnotation: NSObject, MKAnnotation {
let station: MutableStation
let stationPosition: Point
let badge: Badge
let distance: Property<String?>

var index: Int {
didSet { badge.set(numericalIndex: index) }
}

init(station: MutableStation, locatedAt position: Point, index: Int,
distance: SignalProducer<String, NoError>)
init(station: MutableStation, locatedAt position: Point, distance: SignalProducer<String, NoError>)
{
self.station = station
self.stationPosition = position
self.index = index

self.badge = Badge(alphabetIndex: index, seedForColor: station)
self.distance = Property(initial: nil, then: distance.map(Optional.init))
}

Expand Down

0 comments on commit b73d028

Please sign in to comment.