/// <summary> /// WhenLoaded allows you to register a Func to be called OnViewLoaded. /// </summary> /// <param name="item">Object that supports loading.</param> /// <param name="block"> /// The method to be called when the corresponding View is loaded. /// It returns a list of Disposables that will be cleaned up when the View is unloaded. /// </param> /// <param name="view"> /// The IViewFor will ordinarily also host the View Model, but in the event it is not, /// a class implementing <see cref="IViewFor<T>" /> can be supplied here. /// </param> /// <returns>A Disposable that cleans up this registration.</returns> public static IDisposable WhenLoaded <T>(this IViewFor <T> item, Func <IEnumerable <IDisposable> > block, IViewFor <T> view) where T : class { if (item == null) { throw new ArgumentNullException(nameof(item)); } var loadedFetcher = _loadedFetcherCache.Get(item.GetType()); if (loadedFetcher == null) { throw new ArgumentException($"Don't know how to detect when {item.GetType().FullName} is loaded/unloaded, you may need to implement {nameof(ILoadedForViewFetcher)}"); } var viewEvents = loadedFetcher.GetLoadedForView(item); var vmDisposable = Disposable.Empty; if ((view ?? item) is IViewFor v) { vmDisposable = HandleViewModelOnViewLoaded(v, viewEvents); } var viewDisposable = typeof(IActivate).IsAssignableFrom(typeof(T)) ? HandleViewOnActivatedAndLoaded((view ?? item), block, viewEvents) : HandleViewOnLoaded(block, viewEvents); return(new CompositeDisposable(vmDisposable, viewDisposable)); }
public static IDisposable WhenActivated(this IViewFor This, Func <IEnumerable <IDisposable> > block) { var activationFetcher = activationFetcherCache.Get(This.GetType()); if (activationFetcher == null) { throw new ArgumentException( String.Format( "Don't know how to detect when {0} is activated/deactivated, you may need to implement IActivationForViewFetcher", This.GetType().FullName)); } var activationEvents = activationFetcher.GetActivationForView(This); var viewDisposable = new SerialDisposable(); return(new CompositeDisposable( activationEvents.Item1.Subscribe(_ => viewDisposable.Disposable = new CompositeDisposable(block())), activationEvents.Item2.Subscribe(_ => viewDisposable.Disposable = Disposable.Empty), handleViewModelActivation(This, activationEvents), viewDisposable)); }
public static void ViewCreated(this IViewFor This, Func <object> viewModelFunc) { // fail fast if viewmodel already set if (This.ViewModel != null) { return; } var viewModel = viewModelFunc(); if (viewModel == null) { LogHost.Default.Info("ViewModel not loaded for view {0}", This.GetType().Name); return; } This.ViewModel = viewModel; }
public ViewModelBase ResolveViewModel(IViewFor viewFor, IEnumerable <object> parameters = null) { var paramList = parameters?.ToList(); switch (viewFor) { case DashboardPage _: return(new DashboardViewModel( _navigationService.Value, _pushNotificationService.Value, _userInteractionService.Value)); case AuthPage _: return(new AuthViewModel( _authService.Value, _userInteractionService.Value, _schedulerService.Value)); case CloudMessagingPage _: return(new CloudMessagingViewModel( _pushNotificationService.Value, _userInteractionService.Value)); case RemoteConfigPage _: return(new RemoteConfigViewModel( _userInteractionService.Value, _firebaseRemoteConfig.Value)); case StoragePage _: return(new StorageViewModel( _userInteractionService.Value, _firebaseStorage.Value)); } throw new ArgumentException($"Couldn't resolve corresponding viewmodel for IViewFor: {viewFor.GetType()}"); }