예제 #1
0
        internal static void UpdateFragments(NavigationStack <TViewModel> navigationStack, AppCompatActivity appCompatActivity, List <IFragmentInnerStack> fragmentsToPop, List <IFragmentInnerStack> fragmentsToPush, IFragmentActivity fragmentActivity)
        {
            FragmentTransaction transaction = null;

            var dialogFragmentToPush = fragmentsToPush?.Where(x => x is DialogFragmentInnerStack <TViewModel>).Cast <DialogFragmentInnerStack <TViewModel> >().ToList();
            var normalFragmentToPush = fragmentsToPush?.Where(x => x is FragmentInnerStack <TViewModel>).ToList();
            List <FragmentInnerStack <TViewModel> > fragmentListToPop = null;

            if (fragmentsToPop != null)
            {
                foreach (IFragmentInnerStack fragmentStack in fragmentsToPop)
                {
                    if (fragmentStack is DialogFragmentInnerStack <TViewModel> dialogFragmentInnerStack)
                    {
                        dialogFragmentInnerStack.Fragment.DismissAllowingStateLoss();
                    }
                    else if (fragmentStack is FragmentInnerStack <TViewModel> fragmentInnerStack)
                    {
                        if (fragmentListToPop is null)
                        {
                            fragmentListToPop = new List <FragmentInnerStack <TViewModel> >(fragmentsToPop.Count);
                        }

                        fragmentListToPop.Add(fragmentInnerStack);
                    }
                }

                if (fragmentListToPop != null)
                {
                    transaction = appCompatActivity.SupportFragmentManager.BeginTransaction();
                    foreach (FragmentInnerStack <TViewModel> fragmentStack in fragmentListToPop)
                    {
                        transaction = transaction.Remove(fragmentStack.Fragment);
                    }
                }
            }

            if (normalFragmentToPush != null && normalFragmentToPush.Count > 0 && fragmentActivity != null)
            {
                foreach (IFragmentInnerStack fragmentStack in normalFragmentToPush)
                {
                    if (transaction is null)
                    {
                        transaction = appCompatActivity.SupportFragmentManager.BeginTransaction();
                    }

                    transaction.AddToBackStack(fragmentStack.FragmentTag);
                    transaction = transaction.Replace(fragmentActivity.FragmentContainerId, fragmentStack.Fragment, fragmentStack.FragmentTag);
                }
            }
            else if (transaction != null)
            {
                ActivityInnerStack <TViewModel> activityTop;
                if (dialogFragmentToPush != null && dialogFragmentToPush.Count > 0)
                {
                    activityTop = navigationStack.FindFirstOfType <ActivityInnerStack <TViewModel> >();
                }
                else
                {
                    activityTop = navigationStack.Top as ActivityInnerStack <TViewModel>;
                }

                if (activityTop != null && activityTop.FragmentStack.Count > 0 && activityTop.FragmentStack[activityTop.FragmentStack.Count - 1] is FragmentInnerStack <TViewModel> fragmentInnerStack)
                {
                    transaction = transaction.Replace(fragmentActivity.FragmentContainerId, fragmentInnerStack.Fragment, fragmentInnerStack.FragmentTag);
                }
            }

            transaction?.CommitAllowingStateLoss();

            if (dialogFragmentToPush != null && dialogFragmentToPush.Count > 0)
            {
                foreach (DialogFragmentInnerStack <TViewModel> dialogFragment in dialogFragmentToPush)
                {
                    dialogFragment.Fragment.Show(appCompatActivity.SupportFragmentManager, dialogFragment.FragmentTag);
                }
            }
        }
예제 #2
0
 public NavigationPresenter(IViewModelLocatorService <TViewModel> viewModelLocatorService)
 {
     _navigationStack = new NavigationStack <TViewModel>(viewModelLocatorService);
 }