Skip to content
This repository has been archived by the owner. It is now read-only.

#275 MvxCachingFragmentCompatActivity GetLastFragmentInfo() does not … #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,36 +5,37 @@
//
// Project Lead - Stuart Lodge, @slodge, me@slodge.com

using System;
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Util;
using Android.Views;
using MvvmCross.Platform;
using MvvmCross.Platform.Exceptions;
using MvvmCross.Platform.Platform;
using MvvmCross.Binding.Droid.BindingContext;
using MvvmCross.Droid.Platform;
using MvvmCross.Droid.Views;
using MvvmCross.Core.ViewModels;
using MvvmCross.Core.Views;
using MvvmCross.Droid.Shared.Caching;
using MvvmCross.Droid.Shared.Presenter;
using MvvmCross.Droid.Platform;
using MvvmCross.Droid.Shared.Attributes;
using MvvmCross.Droid.Shared.Caching;
using MvvmCross.Droid.Shared.Fragments;
using MvvmCross.Droid.Shared.Presenter;
using MvvmCross.Droid.Support.V4;
using MvvmCross.Droid.Views;
using MvvmCross.Platform;
using MvvmCross.Platform.Exceptions;
using MvvmCross.Platform.Platform;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MvvmCross.Droid.Support.V7.AppCompat
{
[Register("mvvmcross.droid.support.v7.appcompat.MvxCachingFragmentCompatActivity")]
[Register("mvvmcross.droid.support.v7.appcompat.MvxCachingFragmentCompatActivity")]
public class MvxCachingFragmentCompatActivity : MvxFragmentCompatActivity, IFragmentCacheableActivity, IMvxFragmentHost
{
public const string ViewModelRequestBundleKey = "__mvxViewModelRequest";
private const string SavedFragmentTypesKey = "__mvxSavedFragmentTypes";
private IFragmentCacheConfiguration _fragmentCacheConfiguration;
private string _lastFragmentTag;

public override View OnCreateView(View parent, string name, Context context, IAttributeSet attrs)
{
Expand Down Expand Up @@ -208,8 +209,9 @@ protected virtual void ShowFragment(string tag, int contentId, Bundle bundle, bo
//If we already have a previously created fragment, we only need to send the new parameters
if (fragInfo.CachedFragment != null && fragmentReplaceMode == FragmentReplaceMode.ReplaceFragment)
{
(fragInfo.CachedFragment as Fragment).Arguments.Clear();
(fragInfo.CachedFragment as Fragment).Arguments.PutAll(bundle);
var fragment = fragInfo.CachedFragment.ToFragment();
fragment.Arguments.Clear();
fragment.Arguments.PutAll(bundle);
}
else
{
Expand All @@ -219,15 +221,18 @@ protected virtual void ShowFragment(string tag, int contentId, Bundle bundle, bo
OnFragmentCreated(fragInfo, ft);
}

ft.Replace(fragInfo.ContentId, fragInfo.CachedFragment as Fragment, fragInfo.Tag);
if(fragInfo.CacheFragment)
_lastFragmentTag = fragInfo.Tag;

ft.Replace(fragInfo.ContentId, fragInfo.CachedFragment.ToFragment(), fragInfo.Tag);

//if replacing ViewModel then clear the cache after the fragment
//has been added to the transaction so that the Tag property is not null
//and the UniqueImmutableCacheTag property (if not overridden) has the correct value
if (fragmentReplaceMode == FragmentReplaceMode.ReplaceFragmentAndViewModel)
{
var cache = Mvx.GetSingleton<IMvxMultipleViewModelCache>();
cache.GetAndClear(fragInfo.ViewModelType, GetTagFromFragment(fragInfo.CachedFragment as Fragment));
cache.GetAndClear(fragInfo.ViewModelType, GetTagFromFragment(fragInfo.CachedFragment.ToFragment()));
}

if ((currentFragment != null && fragInfo.AddToBackStack) || forceAddToBackStack)
Expand Down Expand Up @@ -310,14 +315,10 @@ protected virtual IEnumerable<Fragment> GetCurrentCacheableFragments()

protected virtual IMvxCachedFragmentInfo GetLastFragmentInfo()
{
var currentCacheableFragments = GetCurrentCacheableFragments().ToList();
if (!currentCacheableFragments.Any())
throw new InvalidOperationException("Cannot retrieve last fragment as FragmentManager is empty.");

var lastFragment = currentCacheableFragments.Last();
var tagFragment = GetTagFromFragment(lastFragment);
if (_lastFragmentTag == null)
throw new InvalidOperationException("Cannot retrieve last fragment as no Cacheable fragment has been added yet.");

return GetFragmentInfoByTag(tagFragment);
return GetFragmentInfoByTag(_lastFragmentTag);
}

protected virtual string GetTagFromFragment(Fragment fragment)
Expand Down Expand Up @@ -401,6 +402,7 @@ protected virtual string FragmentJavaName(Type fragmentType)

public virtual void OnBeforeFragmentChanging(IMvxCachedFragmentInfo fragmentInfo, FragmentTransaction transaction)
{

}

// Called before the transaction is commited
Expand Down