public static void RegisterFragmentViewToCacheIfNeeded(this IMvxFragmentView fragmentView, Type fragmentParentActivityType) { Fragment representedFragment = fragmentView.ToFragment(); if (representedFragment == null) { throw new InvalidOperationException($"Represented type: {fragmentView.GetType()} is not a Fragment!"); } var fragmentParentActivtiy = representedFragment.Activity; if (fragmentParentActivtiy == null) { throw new InvalidOperationException("Something wrong happend, fragment has no activity attached during registration!"); } IFragmentCacheableActivity cacheableActivity = fragmentParentActivtiy as IFragmentCacheableActivity; if (cacheableActivity == null) { throw new InvalidOperationException($"Fragment has activity attached but it does not implement {nameof(IFragmentCacheableActivity)} ! Cannot register fragment to cache!"); } if (string.IsNullOrEmpty(fragmentView.UniqueImmutableCacheTag)) { throw new InvalidOperationException("Contract failed - Fragment tag is null! Fragment tags are not set by default, you should add tag during FragmentTransaction or override UniqueImmutableCacheTag in your Fragment class."); } var fragmentCacheConfiguration = cacheableActivity.FragmentCacheConfiguration; fragmentCacheConfiguration.RegisterFragmentToCache(fragmentView.UniqueImmutableCacheTag, fragmentView.GetType(), fragmentView.FindAssociatedViewModelType(fragmentParentActivityType)); }
private static IMvxViewModel LoadViewModel(this IMvxFragmentView fragmentView, IMvxBundle savedState, MvxViewModelRequest request = null) { var viewModelType = fragmentView.FindAssociatedViewModelTypeOrNull(); if (viewModelType == typeof(MvxNullViewModel)) { return(new MvxNullViewModel()); } if (viewModelType == null || viewModelType == typeof(IMvxViewModel)) { MvxTrace.Trace("No ViewModel class specified for {0} in LoadViewModel", fragmentView.GetType().Name); } if (request == null) { request = MvxViewModelRequest.GetDefaultRequest(viewModelType); } var loaderService = Mvx.Resolve <IMvxViewModelLoader>(); var viewModel = loaderService.LoadViewModel(request, savedState); return(viewModel); }
public static async ValueTask <IMvxViewModel?> LoadViewModel(this IMvxFragmentView fragmentView, IMvxBundle?savedState, Type fragmentParentActivityType, MvxViewModelRequest?request = null) { var viewModelType = fragmentView.FindAssociatedViewModelType(fragmentParentActivityType); if (viewModelType == typeof(MvxNullViewModel)) { return(new MvxNullViewModel()); } if (viewModelType == null || viewModelType == typeof(IMvxViewModel)) { MvxLog.Instance.Trace("No ViewModel class specified for {0} in LoadViewModel", fragmentView.GetType().Name); } if (request == null) { request = MvxViewModelRequest.GetDefaultRequest(viewModelType); } var viewModelCache = Mvx.IoCProvider.Resolve <IMvxChildViewModelCache>(); if (viewModelCache.Exists(viewModelType)) { var viewModelCached = viewModelCache.Get(viewModelType); viewModelCache.Remove(viewModelType); return(viewModelCached); } var loaderService = Mvx.IoCProvider.Resolve <IMvxViewModelLoader>(); return(await loaderService.LoadViewModel(request, savedState).ConfigureAwait(false)); }
public static void OnCreate(this IMvxFragmentView fragmentView, IMvxBundle bundle, MvxViewModelRequest request = null) { if (fragmentView.ViewModel != null) { Mvx.Trace("Fragment {0} already has a ViewModel, skipping ViewModel rehydration", fragmentView.GetType().ToString()); return; } var view = fragmentView as IMvxView; var viewModelType = fragmentView.FindAssociatedViewModelTypeOrNull(); var cache = Mvx.Resolve <IMvxMultipleViewModelCache>(); var cached = cache.GetAndClear(viewModelType); view.OnViewCreate(() => cached ?? LoadViewModel(fragmentView, bundle, request)); }
public static IMvxViewModel LoadViewModel(this IMvxFragmentView fragmentView, IMvxBundle savedState, Type fragmentParentActivityType, MvxViewModelRequest request = null) { var viewModelType = fragmentView.FindAssociatedViewModelType(fragmentParentActivityType); if (viewModelType == typeof(MvxNullViewModel)) { return(new MvxNullViewModel()); } if (viewModelType == null || viewModelType == typeof(IMvxViewModel)) { MvxTrace.Trace("No ViewModel class specified for {0} in LoadViewModel", fragmentView.GetType().Name); } if (request == null) { request = MvxViewModelRequest.GetDefaultRequest(viewModelType); } var viewModelCache = Mvx.Resolve <IMvxChildViewModelCache>(); if (viewModelCache.Exists(viewModelType)) { var viewModelCached = viewModelCache.Get(viewModelType); viewModelCache.Remove(viewModelType); return(viewModelCached); } var loaderService = Mvx.Resolve <IMvxViewModelLoader>(); var viewModel = loaderService.LoadViewModel(request, savedState); return(viewModel); }
public static Type FindAssociatedViewModelType(this IMvxFragmentView fragmentView, Type fragmentActivityParentType) { var viewModelType = fragmentView.FindAssociatedViewModelTypeOrNull(); var type = fragmentView.GetType(); if (viewModelType == null) { if (!type.HasMvxFragmentAttribute()) { throw new InvalidOperationException($"Your fragment is not generic and it does not have {nameof(MvxFragmentAttribute)} attribute set!"); } var cacheableFragmentAttribute = type.GetMvxFragmentAttribute(fragmentActivityParentType); if (cacheableFragmentAttribute.ViewModelType == null) { throw new InvalidOperationException($"Your fragment is not generic and it does not use {nameof(MvxFragmentAttribute)} with ViewModel Type constructor."); } viewModelType = cacheableFragmentAttribute.ViewModelType; } return(viewModelType); }