Skip to content

Instantly share code, notes, and snippets.

@justinvp
Last active December 29, 2015 00:09
Show Gist options
  • Save justinvp/7584108 to your computer and use it in GitHub Desktop.
Save justinvp/7584108 to your computer and use it in GitHub Desktop.
using System;
using MonoTouch.UIKit;
using ReactiveUI;
using ReactiveUI.Cocoa;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
using MonoTouch.Foundation;
using System.ComponentModel;
namespace ReactiveUI.Cocoa
{
public class RouterUINavigationControllerEx : UINavigationController
{
readonly IRoutingState router;
readonly Subject<Unit> orientationChanged = new Subject<Unit>();
public RouterUINavigationControllerEx(IRoutingState router, IViewLocator viewLocator = null)
{
this.router = router;
viewLocator = viewLocator ?? ViewLocator.Current;
router.Navigate.Subscribe (x => {
Console.WriteLine("Gets called!");
var view = viewLocator.ResolveView(x);
view.ViewModel = x;
this.PushViewController((UIViewController)view, true);
});
//var platform = RxApp.DependencyResolver.GetService<IPlatformOperations>();
var vmAndContract = Observable.CombineLatest(
router.Navigate,
orientationChanged,
(vm, _) => new { ViewModel = vm, Contract = MonoTouch.UIKit.UIDevice.CurrentDevice.Orientation.ToString() });
vmAndContract.Subscribe (x => {
Console.WriteLine("Never gets called");
var view = viewLocator.ResolveView(x.ViewModel, x.Contract);
view.ViewModel = x.ViewModel;
this.PushViewController((UIViewController)view, true);
});
router.NavigateBack.Subscribe(_ => this.PopViewControllerAnimated(true));
router.NavigateAndReset.Subscribe (x => {
this.PopToRootViewController(false);
router.Navigate.Execute(x);
});
this.Delegate = new RouterUINavigationControllerDelegate(this, router);
}
public override void DidRotate(UIInterfaceOrientation fromInterfaceOrientation)
{
base.DidRotate(fromInterfaceOrientation);
orientationChanged.OnNext(Unit.Default);
}
bool dontPopWhileYouPop = false;
[Export("navigationBar:shouldPopItem:")]
bool navigationBarShouldPopItem(UINavigationBar navigationBar, UINavigationItem item)
{
if (dontPopWhileYouPop) return true;
RxApp.MainThreadScheduler.Schedule (() => {
if (!router.NavigateBack.CanExecute(null) || dontPopWhileYouPop) return;
dontPopWhileYouPop = true;
router.NavigateBack.Execute(null);
});
return false;
}
class RouterUINavigationControllerDelegate : UINavigationControllerDelegate
{
IRoutingState router;
RouterUINavigationControllerEx parent;
IDisposable prevBackWireup = Disposable.Empty;
public RouterUINavigationControllerDelegate(RouterUINavigationControllerEx parent, IRoutingState router)
{
this.parent = parent;
this.router = router;
}
public override void DidShowViewController (UINavigationController navigationController, UIViewController viewController, bool animated)
{
var viewFor = viewController as IViewFor;
if (viewFor != null) {
var vm = router.GetCurrentViewModel();
viewFor.ViewModel = vm;
viewController.Title = vm.UrlPathSegment;
}
parent.dontPopWhileYouPop = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment