コード例 #1
0
ファイル: ChGKPresenter.cs プロジェクト: fatelord/chgk
		protected void PrepareFragment (IMvxFragmentView fragment, MvxViewModelRequest request)
		{
			var loaderService = Mvx.Resolve<IMvxViewModelLoader> ();
			var viewModel = loaderService.LoadViewModel (request, null);

			fragment.ViewModel = viewModel;
		}
コード例 #2
0
        public static void LoadViewModelFrom(this IMvxFragmentView view, MvxViewModelRequest request, IMvxBundle savedState = null)
        {
            var loader    = Mvx.Resolve <IMvxViewModelLoader>();
            var viewModel = loader.LoadViewModel(request, savedState);

            if (viewModel == null)
            {
                MvxAndroidLog.Instance.Warn("ViewModel not loaded for {0}", request.ViewModelType.FullName);
                return;
            }

            view.ViewModel = viewModel;
        }
コード例 #3
0
        public static void EnsureSetupInitialized(this IMvxFragmentView fragmentView)
        {
            var fragment = fragmentView.ToFragment();

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

            var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(fragment.Activity.ApplicationContext);

            setup.EnsureInitialized();
        }
コード例 #4
0
        public static void EnsureSetupInitialized(this IMvxFragmentView fragmentView)
        {
            var fragment = fragmentView.ToFragment();

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

            var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(fragment.Activity.ApplicationContext);

            setupSingleton.EnsureInitialized();
        }
コード例 #5
0
        public static void EnsureBindingContextIsSet(this IMvxFragmentView fragment, Bundle b0)
        {
            var actualFragment = (Fragment)fragment;

            if (fragment.BindingContext == null)
            {
                fragment.BindingContext = new MvxAndroidBindingContext(actualFragment.Activity,
#warning TODO - work out why this didn't work - actualFragment.GetLayoutInflater(b0)
                                                                       new MvxSimpleLayoutInflater(
                                                                           actualFragment.Activity.LayoutInflater),
                                                                       fragment.DataContext);
            }
        }
コード例 #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.FindAssociatedViewModelTypeOrNull();

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

            view.OnViewCreate(() => cached ?? LoadViewModel(fragmentView, bundle, request));
        }
コード例 #7
0
        protected override Task <bool> ShowDialogFragment(Type view,
                                                          MvxDialogFragmentPresentationAttribute attribute,
                                                          MvxViewModelRequest request)
        {
            var fragmentName = attribute.ViewType.FragmentJavaName();
            var tag          = attribute.Tag ?? fragmentName;

            IMvxFragmentView mvxFragmentView = CreateFragment(attribute, fragmentName);
            var dialog = mvxFragmentView as DialogFragment;

            if (dialog == null)
            {
                throw new MvxException("Fragment {0} does not extend {1}", fragmentName, typeof(DialogFragment).FullName);
            }

            // MvxNavigationService provides an already instantiated ViewModel here,
            // therefore just assign it
            if (request is MvxViewModelInstanceRequest instanceRequest)
            {
                mvxFragmentView.ViewModel = instanceRequest.ViewModelInstance;
            }
            else
            {
                mvxFragmentView.LoadViewModelFrom(request);
            }

            dialog.Cancelable = attribute.Cancelable;

            var ft = CurrentFragmentManager.BeginTransaction();

            OnBeforeFragmentChanging(ft, dialog, attribute, request);

            if (attribute.AddToBackStack)
            {
                ft.AddToBackStack(fragmentName);
            }

            OnFragmentChanging(ft, dialog, attribute, request);

            dialog.Show(ft, tag);

            OnFragmentChanged(ft, dialog, attribute, request);
            return(Task.FromResult(true));
        }
コード例 #8
0
        public static void EnsureBindingContextIsSet(this IMvxFragmentView fragment, LayoutInflater inflater)
        {
            var actualFragment = fragment.ToFragment();

            if (fragment.BindingContext == null)
            {
                fragment.BindingContext = new MvxAndroidBindingContext(actualFragment.Activity,
                                                                       new MvxSimpleLayoutInflaterHolder(inflater),
                                                                       fragment.DataContext);
            }
            else
            {
                var androidContext = fragment.BindingContext as IMvxAndroidBindingContext;
                if (androidContext != null)
                {
                    androidContext.LayoutInflaterHolder = new MvxSimpleLayoutInflaterHolder(inflater);
                }
            }
        }
コード例 #9
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 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));
        }
コード例 #10
0
        public static void EnsureBindingContextIsSet(this IMvxFragmentView fragment, Bundle b0)
        {
            var actualFragment = (Android.Support.V4.App.Fragment)fragment;

            if (fragment.BindingContext == null)
            {
                fragment.BindingContext = new MvxAndroidBindingContext(actualFragment.Activity,
                                                                       new MvxSimpleLayoutInflaterHolder(
                                                                           actualFragment.Activity.LayoutInflater),
                                                                       fragment.DataContext);
            }
            else
            {
                var androidContext = fragment.BindingContext as IMvxAndroidBindingContext;
                if (androidContext != null)
                {
                    androidContext.LayoutInflaterHolder = new MvxSimpleLayoutInflaterHolder(actualFragment.Activity.LayoutInflater);
                }
            }
        }
コード例 #11
0
        protected virtual Task <bool> ShowDialogFragment(
            Type view,
            MvxDialogFragmentPresentationAttribute attribute,
            MvxViewModelRequest request)
        {
            var fragmentName = attribute.ViewType.FragmentJavaName();
            IMvxFragmentView mvxFragmentView = CreateFragment(CurrentActivity.SupportFragmentManager, attribute, attribute.ViewType);
            var dialog = (DialogFragment)mvxFragmentView;

            // MvxNavigationService provides an already instantiated ViewModel here,
            // therefore just assign it
            if (request is MvxViewModelInstanceRequest instanceRequest)
            {
                mvxFragmentView.ViewModel = instanceRequest.ViewModelInstance;
            }
            else
            {
                mvxFragmentView.LoadViewModelFrom(request, null);
            }

            dialog.Cancelable = attribute.Cancelable;

            var ft = CurrentFragmentManager.BeginTransaction();

            OnBeforeFragmentChanging(ft, dialog, attribute, request);

            if (attribute.AddToBackStack)
            {
                ft.AddToBackStack(fragmentName);
            }

            OnFragmentChanging(ft, dialog, attribute, request);

            dialog.Show(ft, fragmentName);

            OnFragmentChanged(ft, dialog, attribute, request);
            return(Task.FromResult(true));
        }
コード例 #12
0
        protected override void ShowDialogFragment(Type view,
                                                   MvxDialogFragmentPresentationAttribute attribute,
                                                   MvxViewModelRequest request)
        {
            var fragmentName = FragmentJavaName(attribute.ViewType);
            IMvxFragmentView mvxFragmentView = CreateFragment(attribute, fragmentName);
            var dialog = (DialogFragment)mvxFragmentView;

            // MvxNavigationService provides an already instantiated ViewModel here,
            // therefore just assign it
            if (request is MvxViewModelInstanceRequest instanceRequest)
            {
                mvxFragmentView.ViewModel = instanceRequest.ViewModelInstance;
            }
            else
            {
                mvxFragmentView.LoadViewModelFrom(request, null);
            }

            dialog.Cancelable = attribute.Cancelable;

            Dialogs.Add(mvxFragmentView.ViewModel, dialog);

            var ft = CurrentFragmentManager.BeginTransaction();

            OnBeforeFragmentChanging(ft, dialog, attribute);

            if (attribute.AddToBackStack == true)
            {
                ft.AddToBackStack(fragmentName);
            }

            OnFragmentChanging(ft, dialog, attribute);

            dialog.Show(ft, fragmentName);

            OnFragmentChanged(ft, dialog, attribute);
        }
コード例 #13
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))
            {
                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);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        public static void EnsureBindingContextIsSet(this IMvxFragmentView fragment, LayoutInflater inflater)
        {
            var actualFragment = fragment.ToFragment();

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

            if (fragment.BindingContext == null)
            {
                fragment.BindingContext = new MvxAndroidBindingContext(actualFragment.Activity,
                                                                       new MvxSimpleLayoutInflaterHolder(inflater),
                                                                       fragment.DataContext);
            }
            else
            {
                var androidContext = fragment.BindingContext as IMvxAndroidBindingContext;
                if (androidContext != null)
                {
                    androidContext.LayoutInflaterHolder = new MvxSimpleLayoutInflaterHolder(inflater);
                }
            }
        }
コード例 #16
0
 public static Fragment ToFragment(this IMvxFragmentView fragmentView)
 {
     return(fragmentView as Fragment);
 }
コード例 #17
0
        protected virtual void PerformShowFragmentTransaction(
            FragmentManager fragmentManager,
            MvxFragmentPresentationAttribute attribute,
            MvxViewModelRequest request)
        {
            var fragmentName = FragmentJavaName(attribute.ViewType);

            IMvxFragmentView fragment = null;

            if (attribute.IsCacheableFragment)
            {
                fragment = (IMvxFragmentView)fragmentManager.FindFragmentByTag(fragmentName);
            }
            fragment = fragment ?? CreateFragment(attribute, fragmentName);

            // MvxNavigationService provides an already instantiated ViewModel here,
            // therefore just assign it
            if (request is MvxViewModelInstanceRequest instanceRequest)
            {
                fragment.ViewModel = instanceRequest.ViewModelInstance;
            }
            else
            {
                var bundle            = new Bundle();
                var serializedRequest = NavigationSerializer.Serializer.SerializeObject(request);
                bundle.PutString(ViewModelRequestBundleKey, serializedRequest);

                var fragmentView = fragment.ToFragment();
                if (fragmentView != null)
                {
                    if (fragmentView.Arguments == null)
                    {
                        fragmentView.Arguments = bundle;
                    }
                    else
                    {
                        fragmentView.Arguments.Clear();
                        fragmentView.Arguments.PutAll(bundle);
                    }
                }
            }

            var ft = fragmentManager.BeginTransaction();

            if (attribute.SharedElements != null)
            {
                foreach (var item in attribute.SharedElements)
                {
                    string name = item.Key;
                    if (string.IsNullOrEmpty(name))
                    {
                        name = ViewCompat.GetTransitionName(item.Value);
                    }
                    ft.AddSharedElement(item.Value, name);
                }
            }
            if (!attribute.EnterAnimation.Equals(int.MinValue) && !attribute.ExitAnimation.Equals(int.MinValue))
            {
                if (!attribute.PopEnterAnimation.Equals(int.MinValue) && !attribute.PopExitAnimation.Equals(int.MinValue))
                {
                    ft.SetCustomAnimations(attribute.EnterAnimation, attribute.ExitAnimation, attribute.PopEnterAnimation, attribute.PopExitAnimation);
                }
                else
                {
                    ft.SetCustomAnimations(attribute.EnterAnimation, attribute.ExitAnimation);
                }
            }
            if (attribute.TransitionStyle != int.MinValue)
            {
                ft.SetTransitionStyle(attribute.TransitionStyle);
            }

            if (attribute.AddToBackStack == true)
            {
                ft.AddToBackStack(fragmentName);
            }

            ft.Replace(attribute.FragmentContentId, (Fragment)fragment, fragmentName);
            ft.CommitAllowingStateLoss();
        }
コード例 #18
0
 private static Android.Support.V4.App.Fragment ToFragment(this IMvxFragmentView fragmentView)
 {
     return(fragmentView as Android.Support.V4.App.Fragment);
 }
コード例 #19
0
        public bool Show(MvxViewModelRequest request)
        {
            try
            {
                if (_enableDrawerOnNextNavigation)
                {
                    this.EnableDrawer();
                    _enableDrawerOnNextNavigation = false;
                }

                var         loaderService = Mvx.Resolve <IMvxViewModelLoader>();
                var         homeViewModel = this.ViewModel as HomeViewModel;
                MvxFragment fragment      = null;

                var section = homeViewModel.GetSectionForViewModelType(request.ViewModelType);
                if (section == MenuSection.Unknown)
                {
                    this.Navigate(request, loaderService);
                    return(true);
                }

                this.CloseSlidingPanel();

                switch (section)
                {
                case MenuSection.Map:
                    _currentSection = MenuSection.Map;

                    this.SupportActionBar.Title = homeViewModel.Title;

                    _navigationView.SetCheckedItem(0);

                    var map = this.FragmentManager.FindFragmentById(Resource.Id.mapView) as MapView;
                    if (map.ViewModel == null)
                    {
                        map.MapClicked += (s, a) => this.CloseSlidingPanel();
                        map.ViewModel   = loaderService.LoadViewModel(request, null /* saved state */);
                    }

                    var transaction = this.FragmentManager.BeginTransaction();
                    foreach (var viewType in _frag2tag.Keys)
                    {
                        if (viewType != typeof(MapView))
                        {
                            var fragmentToRemove = this.FragmentManager.FindFragmentByTag(_frag2tag[viewType]);
                            if (fragmentToRemove != null)
                            {
                                transaction.Remove(fragmentToRemove);
                            }
                        }
                    }

                    this.CloseSlidingPanel();

                    this.FragmentManager.PopBackStackImmediate(null, PopBackStackFlags.None | PopBackStackFlags.Inclusive);
                    transaction.Commit();
                    return(true);

                case MenuSection.Routes:
                    fragment        = this.FindFragment <RoutesView>() ?? new RoutesView();
                    _currentSection = section;
                    break;

                case MenuSection.RouteStops:
                    fragment        = this.FindFragment <RouteStopsView>() ?? new RouteStopsView();
                    _currentSection = section;
                    break;

                case MenuSection.Preferences:
                    fragment        = this.FindFragment <PreferencesView>() ?? new PreferencesView();
                    _currentSection = section;
                    break;

                case MenuSection.About:
                    fragment        = this.FindFragment <AboutView>() ?? new AboutView();
                    _currentSection = section;
                    break;
                }

                IMvxFragmentView mvxFragmentView = fragment;
                if (mvxFragmentView != null && mvxFragmentView.ViewModel == null)
                {
                    mvxFragmentView.ViewModel = loaderService.LoadViewModel(request, null /* saved state */);
                }

                if (!_frag2tag.ContainsKey(fragment.GetType()))
                {
                    _frag2tag[fragment.GetType()] = Guid.NewGuid().ToString();
                }

                this.FragmentManager.BeginTransaction()
                .Replace(Resource.Id.content_frame, fragment, _frag2tag[fragment.GetType()])
                .AddToBackStack(null)
                .Commit();

                var menuItem = homeViewModel.MenuItems.First(x => x.Id == (int)section);
                _navigationView.SetCheckedItem(homeViewModel.MenuItems.IndexOf(menuItem));

                _drawer.CloseDrawer(_navigationView);

                return(true);
            }
            finally
            {
                _drawer.CloseDrawer(_navigationView);
            }
        }