Skip to content

Commit

Permalink
Make RxCoreLocation compatible with RxSwift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelPlantard committed Mar 6, 2018
1 parent dd9742c commit da1f97c
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "ReactiveX/RxSwift" ~> 4.0
github "ReactiveX/RxSwift" ~> 3.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "Quick/Nimble" "v7.0.2"
github "Quick/Quick" "v1.2.0"
github "ReactiveX/RxSwift" "4.0.0"
github "ReactiveX/RxSwift" "3.6.1"
19 changes: 15 additions & 4 deletions Sources/CLLocationManagerDelegateEvents+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@
// Copyright © 2017 RxCoreLocation. All rights reserved.
//

import CoreLocation
import class CoreLocation.CLLocationManager
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif

extension CLLocationManager {

/// Factory method that enables subclasses to implement their own `delegate`.
///
/// - returns Instance of delegate proxy that wraps `delegate`.
public func createRxDelegateProxy() -> RxCLLocationManagerDelegateProxy {
return RxCLLocationManagerDelegateProxy(parentObject: self)
}
}

/// extension wrapper With Base class `CLLocationManager`
/// Wraps events coming form `CLLocationManagerDelegat`
extension Reactive where Base: CLLocationManager {

/// Reactive wrapper for `CLLocationManagerDelegate`.
public var delegate: RxCLLocationManagerDelegate {
return RxCLLocationManagerDelegateProxy.proxy(for: base)
public var delegate: DelegateProxy {
return RxCLLocationManagerDelegateProxy.proxyForObject(base)
}

/// Reactive wrapper for `func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)`
Expand Down
2 changes: 1 addition & 1 deletion Sources/CLLocationManagerEvents+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,4 @@ extension CLLocationManager {
}
}
#endif
}
}
31 changes: 31 additions & 0 deletions Sources/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,44 @@ import CoreLocation
import RxCocoa
#endif

func castOptionalOrFatalError<T>(_ value: Any?) -> T? {
if value == nil {
return nil
}
let v: T = castOrFatalError(value)
return v
}

func castOrFatalError<T>(_ value: Any!) -> T {
let maybeResult: T? = value as? T
guard let result = maybeResult else {
rxFatalError("Failure converting from \(value) to \(T.self)")
}

return result
}

func castOrFatalError<T>(_ value: AnyObject!, message: String) -> T {
let maybeResult: T? = value as? T
guard let result = maybeResult else {
rxFatalError(message)
}

return result
}

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 rxFatalError(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) -> Swift.Never {
// The temptation to comment this line is great, but please don't, it's for your own good. The choice is yours.
fatalError(lastMessage(), file: file, line: line)
}

func clAuthorizationStatus(_ args: [Any]) throws -> CLAuthorizationEvent {
let manager = try castOrThrow(CLLocationManager.self, args[0])
guard let rawValue = args[1] as? Int32,
Expand Down
47 changes: 21 additions & 26 deletions Sources/RxCLLocationManagerDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,38 @@
// Copyright © 2017 RxCoreLocation. All rights reserved.
//

import CoreLocation
import class CoreLocation.CLLocationManager
import protocol CoreLocation.CLLocationManagerDelegate
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif

public typealias RxCLLocationManagerDelegate = DelegateProxy<CLLocationManager, CLLocationManagerDelegate>

extension CLLocationManager: HasDelegate {
public typealias Delegate = CLLocationManagerDelegate
}

open class RxCLLocationManagerDelegateProxy: RxCLLocationManagerDelegate, DelegateProxyType, CLLocationManagerDelegate {

public class RxCLLocationManagerDelegateProxy: DelegateProxy, CLLocationManagerDelegate, DelegateProxyType {
/// Type of parent object
public weak private(set) var manager: CLLocationManager?


// MARK: Initializers

/// Init with ParentObject
public init(parentObject: ParentObject) {
manager = parentObject
super.init(parentObject: parentObject, delegateProxy: RxCLLocationManagerDelegateProxy.self)
}

/// Register self to known implementations
public static func registerKnownImplementations() {
self.register { parent -> RxCLLocationManagerDelegateProxy in
RxCLLocationManagerDelegateProxy(parentObject: parent)
}
public required init(parentObject: AnyObject) {
manager = castOrFatalError(parentObject)

super.init(parentObject: parentObject)
}


// MARK: DelegateProxyType conforms

/// Gets the current `CLLocationManagerDelegate` on `CLLocationManager`
open class func currentDelegate(for object: ParentObject) -> CLLocationManagerDelegate? {
return object.delegate
public static func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
let manager: CLLocationManager = castOrFatalError(object)

return manager.delegate
}

/// Set the CLLocationManagerDelegate for `CLLocationManager`
open class func setCurrentDelegate(_ delegate: CLLocationManagerDelegate?, to object: ParentObject) {
object.delegate = delegate
public static func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
let manager: CLLocationManager = castOrFatalError(object)
manager.delegate = castOptionalOrFatalError(delegate)
}
}
15 changes: 6 additions & 9 deletions Tests/ForwardsEventsBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
// Copyright © 2017 RxCoreLocation. All rights reserved.
//

import Foundation
import Quick
import class CoreLocation.CLLocationManager
import Nimble
import RxSwift
import RxCocoa
import RxTest
import CoreLocation
import Quick
import func RxCocoa.driveOnScheduler
import class RxTest.TestScheduler
@testable import RxCoreLocation


struct ForwardsEventsBehaviorContext {
let sut: CLLocationManager
let scheduler: TestScheduler
Expand Down Expand Up @@ -54,7 +51,7 @@ class ForwardsEventsBehavior: Quick.Behavior<ForwardsEventsBehaviorContext> {

describe("Has Events Behavior") {
it("sentMessage") {
SharingScheduler.mock(scheduler: scheduler) {
driveOnScheduler(scheduler) {
let sentMessage = scheduler.record(source: sut.rx.delegate.sentMessage(selector))
invoked()
scheduler.start()
Expand All @@ -63,7 +60,7 @@ class ForwardsEventsBehavior: Quick.Behavior<ForwardsEventsBehaviorContext> {
}

it("methodInvoke") {
SharingScheduler.mock(scheduler: scheduler) {
driveOnScheduler(scheduler) {
let methodInvoked = scheduler.record(source: sut.rx.delegate.methodInvoked(selector))
invoked()
scheduler.start()
Expand Down
12 changes: 5 additions & 7 deletions Tests/HasEventsBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
// Copyright © 2017 RxCoreLocation. All rights reserved.
//

import Foundation
import Quick
import Nimble
import RxSwift
import RxCocoa
import RxTest
import CoreLocation
import Quick
import func RxCocoa.driveOnScheduler
import class RxSwift.Observable
import class RxTest.TestScheduler
@testable import RxCoreLocation

struct HasEventsBehaviorContext<T> {
Expand Down Expand Up @@ -42,7 +40,7 @@ class HasEventsBehavior<T>: Quick.Behavior<HasEventsBehaviorContext<T>> {

describe("Has Events Behavior") {
it("Actually got the event") {
SharingScheduler.mock(scheduler: scheduler) {
driveOnScheduler(scheduler) {
let recorded = scheduler.record(source: observable)
scheduler.start()
expect(recorded.events.count) == 1
Expand Down
6 changes: 2 additions & 4 deletions Tests/RxCoreLocationSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
// Copyright © 2017 RxCoreLocation. All rights reserved.
//

import CoreLocation
import Quick
import Nimble
import CoreLocation
import RxSwift
import RxCocoa
import RxTest
import class RxTest.TestScheduler
@testable import RxCoreLocation

class RxCoreLocationSpec: QuickSpec {
Expand Down
10 changes: 4 additions & 6 deletions Tests/RxTest+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
// Copyright © 2017 RxCoreLocation. All rights reserved.
//

import Foundation
import Quick
import Nimble
import RxSwift
import RxTest
import protocol RxSwift.ObservableConvertibleType
import class RxTest.TestScheduler
import class RxTest.TestableObserver

extension TestScheduler {
/// Builds testable observer for s specific observable sequence, binds it's results and sets up disposal.
Expand All @@ -24,4 +22,4 @@ extension TestScheduler {
}
return observer
}
}
}

0 comments on commit da1f97c

Please sign in to comment.