Skip to content

Commit

Permalink
Support rendering of the MKOverlay class (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohu557 authored and freak4pc committed Feb 12, 2019
1 parent 04ac5c1 commit 8aba33e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
28 changes: 27 additions & 1 deletion Sources/MKMapView+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ extension Reactive where Base: MKMapView {
(_ source: O)
-> Disposable
where O.E == [A] {
return self.annotations(dataSource: RxMapViewReactiveDataSource())(source)
return self.annotations(dataSource: RxMapViewReactiveAnnotationDataSource())(source)
}

public func annotations<
Expand All @@ -263,6 +263,32 @@ extension Reactive where Base: MKMapView {
}
}

// MARK: Binding overlay to the Map
public func overlays<
A: MKOverlay,
O: ObservableType>
(_ source: O)
-> Disposable
where O.E == [A] {
return self.overlays(dataSource: RxMapViewReactiveOverlayDataSource())(source)
}

public func overlays<
DataSource: RxMapViewDataSourceType,
O: ObservableType>
(dataSource: DataSource)
-> (_ source: O)
-> Disposable
where O.E == [DataSource.Element],
DataSource.Element: MKOverlay {
return { source in
return source
.subscribe({ event in
dataSource.mapView(self.base, observedEvent: event)
})
}
}

/// Installs delegate as forwarding delegate on `delegate`.
/// Delegate won't be retained.
///
Expand Down
22 changes: 21 additions & 1 deletion Sources/RxMapViewReactiveDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import MapKit
import RxSwift
import RxCocoa

public class RxMapViewReactiveDataSource<S: MKAnnotation>
public class RxMapViewReactiveAnnotationDataSource<S: MKAnnotation>
: RxMapViewDataSourceType {
public typealias Element = S

Expand All @@ -29,5 +29,25 @@ public class RxMapViewReactiveDataSource<S: MKAnnotation>
}
}.on(observedEvent)
}
}

public class RxMapViewReactiveOverlayDataSource<S: MKOverlay>
: RxMapViewDataSourceType {
public typealias Element = S

var currentOverlay: [S] = []

public func mapView(_ mapView: MKMapView, observedEvent: Event<[S]>) {
Binder(self) { _, newOverlays in
DispatchQueue.main.async {
let diff = Diff.calculateFrom(
previous: self.currentOverlay,
next: newOverlays)
self.currentOverlay = newOverlays
mapView.addOverlays(diff.added)
mapView.removeOverlays(diff.removed)
}
}.on(observedEvent)
}
}

0 comments on commit 8aba33e

Please sign in to comment.