Skip to content

Instantly share code, notes, and snippets.

@sger
Last active January 6, 2016 18:34
Show Gist options
  • Select an option

  • Save sger/07f41bf80571c38f3433 to your computer and use it in GitHub Desktop.

Select an option

Save sger/07f41bf80571c38f3433 to your computer and use it in GitHub Desktop.

Revisions

  1. sger revised this gist Jan 6, 2016. 1 changed file with 96 additions and 1 deletion.
    97 changes: 96 additions & 1 deletion RxLocation.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,99 @@
    enum Action {
    case UpdateLocation
    case FailWithError
    }
    }

    func updateLocationAction()->Observable<CLLocation> {
    return create { observer in
    self.locationManager.rx_didUpdateLocations.take(1).subscribe { event in
    switch event {
    case .Next(let value):
    let location = value.last!
    observer.onNext(location)
    break
    case .Completed:
    observer.onCompleted()
    break
    default: break
    }
    }.addDisposableTo(self.disposeBag)
    return NopDisposable.instance
    }
    }

    func failWithErrorAction()->Observable<NSError> {
    return create { observer in
    self.locationManager.rx_didFailWithError
    .subscribeNext { error in
    observer.onError(error)
    }.addDisposableTo(self.disposeBag)
    return NopDisposable.instance
    }
    }

    func runActions() -> Observable <CLLocation> {
    return create {observer in

    let actions:[Observable<Action>] = [
    self.updateLocationAction().map { _ in return .UpdateLocation },
    self.failWithErrorAction().map { _ in return .FailWithError }
    ]

    actions
    .toObservable()
    .merge()
    .debug("actions")
    .take(1)
    .subscribe { event in
    print("EVENT ACTION \(event)")
    switch event {
    case .Next(let value):
    print("next value \(value)")
    if value == .UpdateLocation {
    self.fetchCurrentLocation().subscribeNext { location in
    observer.onNext(location)
    observer.onCompleted()
    }.addDisposableTo(self.disposeBag)
    }
    break
    case .Completed:
    break
    case .Error(let value):
    observer.onError(value)
    break
    }
    }.addDisposableTo(self.disposeBag)
    return NopDisposable.instance
    }
    }

    //run

    self.locationController.runActions().subscribe { event in
    print("event \(event)")
    switch event {
    case .Next(let location):


    break
    case .Completed:

    break
    case .Error(let error):


    break
    }

    }.addDisposableTo(self.disposeBag)
    locationManager.startUpdatingLocation()
    break
    case .Completed:

    break
    case .Error(let error):

    observer.onError(error)
    break
    }
    }.addDisposableTo(self.disposeBag)
  2. sger created this gist Jan 6, 2016.
    4 changes: 4 additions & 0 deletions RxLocation.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    enum Action {
    case UpdateLocation
    case FailWithError
    }