示例#1
0
        public static void OnCreate(this IMvxFragmentView fragmentView, IMvxBundle bundle, MvxViewModelRequest request = null)
        {
            var cache = Mvx.IoCProvider.Resolve <IMvxMultipleViewModelCache>();

            if (fragmentView.ViewModel != null)
            {
                // check if ViewModel instance was cached. If so, clear it and ignore previous instance
                cache.GetAndClear(fragmentView.ViewModel.GetType(), fragmentView.UniqueImmutableCacheTag);
                return;
            }

            var fragment = fragmentView.ToFragment();

            if (fragment == null)
            {
                throw new MvxException($"{nameof(OnCreate)} called on an {nameof(IMvxFragmentView)} which is not an Android Fragment: {fragmentView}");
            }

            // as it is called during onCreate it is safe to assume that fragment has Activity attached.
            var viewModelType = fragmentView.FindAssociatedViewModelType(fragment.Activity.GetType());
            var view          = fragmentView as IMvxView;

            var cached = cache.GetAndClear(viewModelType, fragmentView.UniqueImmutableCacheTag);

            view.OnViewCreate(() => cached ?? fragmentView.LoadViewModel(bundle, fragment.Activity.GetType(), request));
        }
示例#2
0
        public static void OnCreate(this IMvxFragmentView fragmentView, IMvxBundle bundle, MvxViewModelRequest request = null)
        {
            if (fragmentView.ViewModel != null)
            {
                //TODO call MvxViewModelLoader.Reload when it's added in MvvmCross, tracked by #1165
                //until then, we're going to re-run the viewmodel lifecycle here.
                Android.Views.MvxFragmentExtensions.RunViewModelLifecycle(fragmentView.ViewModel, bundle, request);

                return;
            }

            var fragment = fragmentView.ToFragment();

            if (fragment == null)
            {
                throw new MvxException($"{nameof(OnCreate)} called on an {nameof(IMvxFragmentView)} which is not an Android Fragment: {fragmentView}");
            }

            // as it is called during onCreate it is safe to assume that fragment has Activity attached.
            var viewModelType = fragmentView.FindAssociatedViewModelType(fragment.Activity.GetType());
            var view          = fragmentView as IMvxView;

            var cache  = Mvx.Resolve <IMvxMultipleViewModelCache>();
            var cached = cache.GetAndClear(viewModelType, fragmentView.UniqueImmutableCacheTag);

            view.OnViewCreate(() => cached ?? fragmentView.LoadViewModel(bundle, fragment.Activity.GetType(), request));
        }
示例#3
0
        public static void OnCreate(this IMvxFragmentView fragmentView, IMvxBundle bundle, MvxViewModelRequest request = null)
        {
            if (fragmentView.ViewModel != null)
            {
                //TODO call MvxViewModelLoader.Reload when it's added in MvvmCross, tracked by #1165
                //until then, we're going to re-run the viewmodel lifecycle here.
                RunViewModelLifecycle(fragmentView.ViewModel, bundle, request);

                return;
            }

            Fragment fragment = fragmentView as Fragment;

            if (fragmentView == null)
            {
                throw new InvalidOperationException($"Something really weird. ${nameof(fragmentView)} passed is not a Fragment!");
            }

            // as it is called during onCreate it is safe to assume that fragment has Activity attached.
            var viewModelType = fragmentView.FindAssociatedViewModelType(fragment.Activity.GetType());
            var view          = fragmentView as IMvxView;

            var cache  = Mvx.Resolve <IMvxMultipleViewModelCache>();
            var cached = cache.GetAndClear(viewModelType, fragmentView.UniqueImmutableCacheTag);

            view.OnViewCreate(() => cached ?? LoadViewModel(fragmentView, bundle, fragment.Activity.GetType(), request));
        }
示例#4
0
        private 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 loaderService = Mvx.Resolve <IMvxViewModelLoader>();
            var viewModel     = loaderService.LoadViewModel(request, savedState);

            return(viewModel);
        }
示例#5
0
        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));
        }
示例#6
0
        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.FindAssociatedViewModelType();

            var cache  = Mvx.Resolve <IMvxMultipleViewModelCache>();
            var cached = cache.GetAndClear(viewModelType, fragmentView.UniqueImmutableCacheTag);

            view.OnViewCreate(() => cached ?? LoadViewModel(fragmentView, bundle, request));
        }
示例#7
0
        public static void OnCreate(this IMvxFragmentView fragmentView, IMvxBundle bundle, MvxViewModelRequest request = null)
        {
            if (fragmentView.ViewModel != null)
            {
                //TODO call MvxViewModelLoader.Reload when it's added in MvvmCross, tracked by #1165
                //until then, we're going to re-run the viewmodel lifecycle here.
                RunViewModelLifecycle(fragmentView.ViewModel, bundle, request);

                return;
            }

            var viewModelType = fragmentView.FindAssociatedViewModelType();
            var view          = fragmentView as IMvxView;

            var cache  = Mvx.Resolve <IMvxMultipleViewModelCache>();
            var cached = cache.GetAndClear(viewModelType, fragmentView.UniqueImmutableCacheTag);

            view.OnViewCreate(() => cached ?? LoadViewModel(fragmentView, bundle, request));
        }
示例#8
0
        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))
            {
                MvxLogHost.Default?.Log(LogLevel.Trace, "No ViewModel class specified for {fragmentViewType} 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>();
            var viewModel     = loaderService.LoadViewModel(request, savedState);

            return(viewModel);
        }
示例#9
0
        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));
        }