-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathGMSMapViewDelegateProxy.swift
128 lines (102 loc) · 4.08 KB
/
GMSMapViewDelegateProxy.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// GMSMapViewDelegateProxy.swift
// Example
//
// Created by Gabriel Araujo on 28/10/17.
// Copyright © 2017 Gen X Hippies Company. All rights reserved.
//
import Foundation
import UIKit
import CoreLocation
import RxSwift
import RxCocoa
import GoogleMaps
public typealias GMSHandleTapMarker = (GMSMarker) -> (Bool)
public typealias GMSHandleTapOverlay = (GMSOverlay) -> (Void)
public typealias GMSHandleMarkerInfo = (GMSMarker) -> (UIView?)
public typealias GMSHandleTapMyLocationButton = () -> (Bool)
class GMSMapViewDelegateProxy : DelegateProxy<GMSMapView, GMSMapViewDelegate>, DelegateProxyType, GMSMapViewDelegate {
var handleTapMarker: GMSHandleTapMarker? = nil
var handleTapOverlay: GMSHandleTapOverlay? = nil
var handleTapMyLocationButton: GMSHandleTapMyLocationButton? = nil
var handleMarkerInfoWindow: GMSHandleMarkerInfo? = nil
var handleMarkerInfoContents: GMSHandleMarkerInfo? = nil
let didTapMyLocationButtonEvent = PublishSubject<Void>()
let didTapMarkerEvent = PublishSubject<GMSMarker>()
let didTapOverlayEvent = PublishSubject<GMSOverlay>()
/// Typed parent object.
public weak private(set) var mapView: GMSMapView?
/// - parameter tabBar: Parent object for delegate proxy.
public init(gsMapView: ParentObject) {
self.mapView = gsMapView
super.init(parentObject: gsMapView, delegateProxy: GMSMapViewDelegateProxy.self)
}
// Register known implementations
public static func registerKnownImplementations() {
self.register { GMSMapViewDelegateProxy(gsMapView: $0) }
}
/// For more information take a look at `DelegateProxyType`.
open class func currentDelegate(for object: ParentObject) -> GMSMapViewDelegate? {
return object.delegate
}
/// For more information take a look at `DelegateProxyType`.
open class func setCurrentDelegate(_ delegate: GMSMapViewDelegate?, to object: ParentObject) {
object.delegate = delegate
}
public func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
return self.didHandleTap(marker)
}
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) -> Void {
return self.didHandleTapOverlay(overlay)
}
public func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return self.markerInfoWindow(marker: marker)
}
public func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
return self.markerInfoContents(marker: marker)
}
public func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {
return self.didTapMyLocationButton()
}
}
extension GMSMapViewDelegateProxy {
public func didHandleTap(_ marker: GMSMarker) -> Bool {
didTapMarkerEvent.onNext(marker)
return handleTapMarker?(marker) ?? false
}
public func didHandleTapOverlay(_ overlay: GMSOverlay) -> Void {
didTapOverlayEvent.onNext(overlay)
return handleTapOverlay?(overlay) ?? ()
}
public func didTapMyLocationButton() -> Bool {
didTapMyLocationButtonEvent.onNext(())
return handleTapMyLocationButton?() ?? false
}
public func markerInfoWindow(marker: GMSMarker) -> UIView? {
return handleMarkerInfoWindow?(marker)
}
public func markerInfoContents(marker: GMSMarker) -> UIView? {
return handleMarkerInfoContents?(marker)
}
}
// - MARK: Internal Helpers
func castOptionalOrFatalError<T>(_ value: Any?) -> T? {
if value == nil {
return nil
}
let v: T = castOrFatalError(value)
return v
}
func castOrThrow<T>(_ resultType: T.Type, _ object: Any) throws -> T {
guard let returnValue = object as? T else {
throw RxCocoaError.castingError(object: object, targetType: resultType)
}
return returnValue
}
func castOrFatalError<T>(_ value: Any!) -> T {
let maybeResult: T? = value as? T
guard let result = maybeResult else {
fatalError("Failure converting from \(value ?? "") to \(T.self)")
}
return result
}