Skip to content

Commit

Permalink
Merge pull request MvvmCross#123 from thefex/master
Browse files Browse the repository at this point in the history
Build fix of Preferences/Leanback projects. Fix non-generic fragment restore crash (MvvmCross#110)
  • Loading branch information
martijn00 committed Nov 19, 2015
2 parents 1f99c82 + fb4baf1 commit 2eaf2dc
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 48 deletions.
Expand Up @@ -296,7 +296,7 @@ protected IEnumerable<Fragment> GetCurrentCacheableFragments()
return currentFragments
.Where(fragment => fragment != null)
// we are not interested in fragments which are not supposed to cache!
.Where(fragment => fragment.GetType().IsOwnedViewModelFragment());
.Where(fragment => fragment.GetType().IsCacheableFragmentAttribute());
}

protected IMvxCachedFragmentInfo GetLastFragmentInfo()
Expand Down
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("Cirrious.MvvmCross.Droid.Support.AppCompat")]
Expand Up @@ -38,7 +38,7 @@ protected override void HandleCreateCalled(object sender, MvxValueEventArgs<Bund
{
FragmentView.EnsureSetupInitialized();

if (!FragmentView.GetType().IsOwnedViewModelFragment())
if (!FragmentView.GetType().IsCacheableFragmentAttribute())
return;

Bundle bundle = null;
Expand Down Expand Up @@ -94,7 +94,7 @@ protected override void HandleCreateCalled(object sender, MvxValueEventArgs<Bund

protected override void HandleSaveInstanceStateCalled(object sender, MvxValueEventArgs<Bundle> bundleArgs)
{
if (!FragmentView.GetType().IsOwnedViewModelFragment())
if (!FragmentView.GetType().IsCacheableFragmentAttribute())
return;

var mvxBundle = FragmentView.CreateSaveStateBundle();
Expand Down
Expand Up @@ -42,15 +42,36 @@ public static void OnCreate(this IMvxFragmentView fragmentView, IMvxBundle bundl
return;
}

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

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

view.OnViewCreate(() => cached ?? LoadViewModel(fragmentView, bundle, request));
}

public static Type FindAssociatedViewModelType(this IMvxFragmentView fragmentView)
{
var viewModelType = fragmentView.FindAssociatedViewModelTypeOrNull();

var type = fragmentView.GetType();

if (viewModelType == null)
{
if (!type.IsCacheableFragmentAttribute())
throw new InvalidOperationException($"Your fragment is not generic and it does not have {nameof(MvxFragmentAttribute)} attribute set!");

var cacheableFragmentAttribute = type.GetCacheableFragmentAttribute();
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;
}

public static Fragment ToFragment(this IMvxFragmentView fragmentView)
{
var activity = fragmentView as Fragment;
Expand Down
Expand Up @@ -296,7 +296,7 @@ protected IEnumerable<Fragment> GetCurrentCacheableFragments()
return currentFragments
.Where(fragment => fragment != null)
// we are not interested in fragments which are not supposed to cache!
.Where(fragment => fragment.GetType().IsOwnedViewModelFragment());
.Where(fragment => fragment.GetType().IsCacheableFragmentAttribute());
}

protected IMvxCachedFragmentInfo GetLastFragmentInfo()
Expand Down
33 changes: 33 additions & 0 deletions Cirrious.MvvmCross.Droid.Support.Fragging/MvxFragmentAttribute.cs
@@ -0,0 +1,33 @@
// MvxUnconventionalAttribute.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
//
// Project Lead - Stuart Lodge, @slodge, me@slodge.com

using System;

namespace Cirrious.MvvmCross.Droid.Support.Fragging
{
[AttributeUsage(AttributeTargets.Class)]
public class MvxFragmentAttribute : Attribute
{
public MvxFragmentAttribute()
{

}

/// <summary>
/// That shall be used only if you are using non generic fragments.
/// </summary>
/// <param name="viewModelType"></param>
public MvxFragmentAttribute(Type viewModelType)
{
ViewModelType = viewModelType;
}

internal Type ViewModelType { get; set; }

public bool IsCacheableFragment => true;
}
}
@@ -0,0 +1,32 @@
// MvxConventionAttributeExtensionMethods.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
//
// Project Lead - Stuart Lodge, @slodge, me@slodge.com

using System;
using System.Linq;

namespace Cirrious.MvvmCross.Droid.Support.Fragging
{
public static class MvxFragmentAttributeExtensionMethods
{
public static bool IsCacheableFragmentAttribute(this Type candidateType)
{
var attributes = candidateType.GetCustomAttributes(typeof(MvxFragmentAttribute), true);
return attributes.Length > 0;
}

public static MvxFragmentAttribute GetCacheableFragmentAttribute(this Type fromFragmentType)
{
var attributes = fromFragmentType.GetCustomAttributes(typeof(MvxFragmentAttribute), true);

if (!attributes.Any())
throw new InvalidOperationException($"Type does not have {nameof(MvxFragmentAttribute)} attribute!");

var cacheableFragmentAttribute = attributes.First() as MvxFragmentAttribute;
return cacheableFragmentAttribute;
}
}
}

This file was deleted.

This file was deleted.

Expand Up @@ -65,6 +65,8 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}

public string UniqueImmutableCacheTag => Tag;
}

public abstract class MvxBrowseSupportFragment<TViewModel>
Expand Down
Expand Up @@ -65,5 +65,7 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}
}

public string UniqueImmutableCacheTag => Tag;
}
}
Expand Up @@ -65,5 +65,7 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}
}

public string UniqueImmutableCacheTag => Tag;
}
}
Expand Up @@ -65,7 +65,9 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}
}

public string UniqueImmutableCacheTag => Tag;
}

public abstract class MvxHeadersSupportFragment<TViewModel>
: MvxHeadersSupportFragment
Expand All @@ -87,5 +89,6 @@ public new TViewModel ViewModel
get { return (TViewModel) base.ViewModel; }
set { base.ViewModel = value; }
}
}

}
}
Expand Up @@ -65,5 +65,7 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}
}

public string UniqueImmutableCacheTag => Tag;
}
}
Expand Up @@ -65,7 +65,9 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}
}

public string UniqueImmutableCacheTag => Tag;
}

public abstract class MvxRowsSupportFragment<TViewModel>
: MvxRowsSupportFragment
Expand Down
Expand Up @@ -65,5 +65,7 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}
}

public string UniqueImmutableCacheTag => Tag;
}
}
Expand Up @@ -49,7 +49,9 @@ public virtual IMvxViewModel ViewModel
public virtual void OnViewModelSet()
{
}
}

public string UniqueImmutableCacheTag => Tag;
}

public abstract class MvxPreferenceFragment<TViewModel>
: MvxPreferenceFragment
Expand Down

0 comments on commit 2eaf2dc

Please sign in to comment.