Santiago
Santiago
Reputation Top 1%
Santiago
6 Snippets
Published
1 Channel
Created
2 Channels
Following
141 points
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Trend Maker
Junior Popular Coder
Serious Popular Coder
Junior Autobiographer
Serious Autobiographer

Recent Snippets See all snippets by Santiago

public by  430  3  4  0

CoreLocatonManager + Rx

Reactive extension for Core Location manager
class RxCLLocationManagerDelegateProxy: DelegateProxy, CLLocationManagerDelegate, DelegateProxyType {
  class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
    let locationManager: CLLocationManager = object as! CLLocationManager
    locationManager.delegate = delegate as? CLLocationManagerDelegate
  }
  class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
    let locationManager: CLLocationManager = object as! CLLocationManager
    return locationManager.delegate
  }
}

extension Reactive where Base: CLLocationManager {
  var delegate: DelegateProxy {
    return RxCLLocationManagerDelegateProxy.proxyForObject(base)
  }

  var didUpdateLocations: Observable<[CLLocation]> {
    return delegate.methodInvoked(#selector(CLLocationManagerDelegate.locationManager(_:didUpdateLocations:)))
      .map { parameters in
        return parameters[1] as! [CLLocation]
      }
  }
}
;

public by  350  0  4  0

MKMapView + Rx

Reactive extension for map view
class RxMKMapViewDelegateProxy: DelegateProxy, MKMapViewDelegate, DelegateProxyType {
  class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
    let mapView: MKMapView = (object as? MKMapView)!
    return mapView.delegate
  }

  class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
    let mapView: MKMapView = (object as? MKMapView)!
    mapView.delegate = delegate as? MKMapViewDelegate
  }
}

extension Reactive where Base: MKMapView {
  public var delegate: DelegateProxy {
    return RxMKMapViewDelegateProxy.proxyForObject(base)
  }

  public func setDelegate(_ delegate: MKMapViewDelegate) -> Disposable {
    return RxMKMapViewDelegateProxy.installForwardDelegate(
      delegate,
      retainDelegate: false,
      onProxyForObject: self.base
    )
  }

  var overlays: UIBindingObserver<Base, [MKOverlay]> {
    return UIBindingObserver(UIElement: self.base) { mapView, overlays in
      mapView.removeOverlays(mapView.overlays)
      mapView.addOverlays(overlays)
    }
  }

  public var regionDidChangeAnimated: ControlEvent<Bool> {
    let source = delegate
      .methodInvoked(#selector(MKMapViewDelegate.mapView(_:regionDidChangeAnimated:)))
      .map { parameters in
        return (parameters[1] as? Bool) ?? false
      }
    return ControlEvent(events: source)
  }
}
;

public by  325  0  5  0

BindableType protocol implementation

protocol BindableType
{
  associatedtype ViewModelType

  var viewModel: ViewModelType! { get set }

  func bindViewModel()
}

extension BindableType where Self: UIViewController 
{
  mutating func bindViewModel(to model: Self.ViewModelType) {
    viewModel = model
    loadViewIfNeeded()
    bindViewModel()
  }
}
;

public by Santiago  343  0  4  0

UINavigationControllerDelegate + Rx

Rx extension for navigation controller delegate
class RxNavigationControllerDelegateProxy: DelegateProxy, DelegateProxyType, UINavigationControllerDelegate {

  static func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
    guard let navigationController = object as? UINavigationController else {
      fatalError()
    }
    return navigationController.delegate
  }

  static func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
    guard let navigationController = object as? UINavigationController else {
      fatalError()
    }
    if delegate == nil {
      navigationController.delegate = nil
    } else {
      guard let delegate = delegate as? UINavigationControllerDelegate else {
        fatalError()
      }
      navigationController.delegate = delegate
    }
  }
}

extension Reactive where Base: UINavigationController {
  /**
     Reactive wrapper for `delegate`.
     For more information take a look at `DelegateProxyType` protocol documentation.
     */
  public var delegate: DelegateProxy {
    return RxNavigationControllerDelegateProxy.proxyForObject(base)
  }
}
;

public by Santiago  352  1  5  0

SceneCoordinatorType protocol

protocol SceneCoordinatorType 
{
  init(window: UIWindow)

  /// transition to another scene
  @discardableResult
  func transition(to scene: Scene, type: SceneTransitionType) -> Observable<Void>

  /// pop scene from navigation stack or dismiss current modal
  @discardableResult
  func pop(animated: Bool) -> Observable<Void>
}

extension SceneCoordinatorType {
  @discardableResult
  func pop() -> Observable<Void> {
    return pop(animated: true)
  }
}

enum Scene {
  case tasks(TasksViewModel)
  case editTask(EditTaskViewModel)
}

enum SceneTransitionType {
  // you can extend this to add animated transition types,
  // interactive transitions and even child view controllers!

  case root       // make view controller the root view controller
  case push       // push view controller to navigation stack
  case modal      // present view controller modally
}

extension Scene {
  func viewController() -> UIViewController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    switch self {
    case .tasks(let viewModel):
      let nc = storyboard.instantiateViewController(withIdentifier: "Tasks") as! UINavigationController
      var vc = nc.viewControllers.first as! TasksViewController
      vc.bindViewModel(to: viewModel)
      return nc

    case .editTask(let viewModel):
      let nc = storyboard.instantiateViewController(withIdentifier: "EditTask") as! UINavigationController
      var vc = nc.viewControllers.first as! EditTaskViewController
      vc.bindViewModel(to: viewModel)
      return nc
    }
  }
}

						
;