Skip to content

Commit

Permalink
Increase AndroidViewPresenter verbosity // Resolve IMvxAndroidCurrent…
Browse files Browse the repository at this point in the history
…TopActivity just once
  • Loading branch information
nmilcoff committed Aug 12, 2017
1 parent aca343a commit 32092d6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class MvxAppCompatViewPresenter : MvxAndroidViewPresenter
{
public MvxAppCompatViewPresenter(IEnumerable<Assembly> androidViewAssemblies) : base(androidViewAssemblies)
{

}

protected new ConditionalWeakTable<IMvxViewModel, DialogFragment> Dialogs { get; } = new ConditionalWeakTable<IMvxViewModel, DialogFragment>();

protected new FragmentManager CurrentFragmentManager
{
get
Expand Down Expand Up @@ -65,7 +65,7 @@ protected override MvxBasePresentationAttribute GetAttributeForViewModel(Type vi
{
attribute = item;
break;
}
}
}
}

Expand All @@ -81,19 +81,29 @@ protected override MvxBasePresentationAttribute GetAttributeForViewModel(Type vi

var viewType = ViewsContainer.GetViewType(viewModelType);
if (viewType.IsSubclassOf(typeof(DialogFragment)))
{
MvxTrace.Trace($"PresentationAttribute not found for {viewModelType.Name}. " +
$"Assuming DialogFragment presentation");
return new MvxDialogFragmentPresentationAttribute();
}
if (viewType.IsSubclassOf(typeof(Fragment)))
{
MvxTrace.Trace($"PresentationAttribute not found for {viewModelType.Name}. " +
$"Assuming Fragment presentation");
return new MvxFragmentPresentationAttribute(GetCurrentActivityViewModelType(), Android.Resource.Id.Content);
}

MvxTrace.Trace($"PresentationAttribute not found for {viewModelType.Name}. " +
$"Assuming Activity presentation");
return new MvxActivityPresentationAttribute() { ViewModelType = viewModelType };
}

protected override void ShowActivity(Type view,
MvxActivityPresentationAttribute attribute,
protected override void ShowActivity(Type view,
MvxActivityPresentationAttribute attribute,
MvxViewModelRequest request)
{
var intent = CreateIntentForRequest(request);
if(attribute.Extras != null)
if (attribute.Extras != null)
intent.PutExtras(attribute.Extras);

var activity = CurrentActivity;
Expand Down Expand Up @@ -178,7 +188,7 @@ protected override void ShowFragment(Type view,
if (attribute.TransitionStyle != int.MinValue)
ft.SetTransitionStyle(attribute.TransitionStyle);

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

ft.Add(attribute.FragmentContentId, (Fragment)fragment, fragmentName);
Expand Down
44 changes: 32 additions & 12 deletions MvvmCross/Droid/Droid/Views/MvxAndroidViewPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,28 @@ public class MvxAndroidViewPresenter : MvxViewPresenter, IMvxAndroidViewPresente
public const string ViewModelRequestBundleKey = "__mvxViewModelRequest";
protected MvxViewModelRequest _pendingRequest;

protected virtual Activity CurrentActivity => Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
protected virtual FragmentManager CurrentFragmentManager => CurrentActivity.FragmentManager;

protected virtual ConditionalWeakTable<MvxBasePresentationAttribute, IMvxFragmentView> CachedFragments { get; } = new ConditionalWeakTable<MvxBasePresentationAttribute, IMvxFragmentView>();
protected virtual ConditionalWeakTable<IMvxViewModel, DialogFragment> Dialogs { get; } = new ConditionalWeakTable<IMvxViewModel, DialogFragment>();

private IMvxAndroidCurrentTopActivity _mvxAndroidCurrentTopActivity;
protected virtual Activity CurrentActivity
{
get
{
if (_mvxAndroidCurrentTopActivity == null)
_mvxAndroidCurrentTopActivity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>();
return _mvxAndroidCurrentTopActivity.Activity;
}
}

private IMvxAndroidActivityLifetimeListener _activityLifetimeListener;
protected IMvxAndroidActivityLifetimeListener ActivityLifetimeListener
{
get
{
if(_activityLifetimeListener == null)
if (_activityLifetimeListener == null)
_activityLifetimeListener = Mvx.Resolve<IMvxAndroidActivityLifetimeListener>();
return _activityLifetimeListener;
}
Expand All @@ -44,20 +54,20 @@ protected IMvxAndroidActivityLifetimeListener ActivityLifetimeListener
private IMvxViewModelTypeFinder _viewModelTypeFinder;
protected IMvxViewModelTypeFinder ViewModelTypeFinder
{
get
get
{
if (_viewModelTypeFinder == null)
_viewModelTypeFinder = Mvx.Resolve<IMvxViewModelTypeFinder>();
return _viewModelTypeFinder;
}
return _viewModelTypeFinder;
}
}

private IMvxViewsContainer _viewsContainer;
protected IMvxViewsContainer ViewsContainer
{
get
{
if(_viewsContainer == null)
if (_viewsContainer == null)
_viewsContainer = Mvx.Resolve<IMvxViewsContainer>();
return _viewsContainer;
}
Expand Down Expand Up @@ -104,15 +114,15 @@ protected virtual void ActivityLifetimeListener_ActivityChanged(object sender, M
Show(_pendingRequest);
_pendingRequest = null;
}
else if(e.ActivityState == MvxActivityState.OnCreate && e.Extras is Bundle savedBundle)
else if (e.ActivityState == MvxActivityState.OnCreate && e.Extras is Bundle savedBundle)
{
//TODO: Restore fragments from bundle
}
else if(e.ActivityState == MvxActivityState.OnSaveInstanceState && e.Extras is Bundle outBundle)
else if (e.ActivityState == MvxActivityState.OnSaveInstanceState && e.Extras is Bundle outBundle)
{
//TODO: Save fragments into bundle
}
else if(e.ActivityState == MvxActivityState.OnDestroy)
else if (e.ActivityState == MvxActivityState.OnDestroy)
{
//TODO: Should be check for Fragments on this Activity and destroy them?
}
Expand All @@ -136,7 +146,7 @@ private void RegisterAttributes()
foreach (var attribute in typeWithAttribute.GetBasePresentationAttributes())
{
//TODO: Can we set the viewType from somewhere else?
if(attribute.ViewType == null)
if (attribute.ViewType == null)
attribute.ViewType = typeWithAttribute;
ViewModelToPresentationAttributeMap[viewModelType].Add(attribute);
}
Expand Down Expand Up @@ -197,10 +207,20 @@ protected virtual MvxBasePresentationAttribute GetAttributeForViewModel(Type vie
var viewType = ViewsContainer.GetViewType(viewModelType);

if (viewType.IsSubclassOf(typeof(DialogFragment)))
{
MvxTrace.Trace($"PresentationAttribute not found for {viewModelType.Name}. " +
$"Assuming DialogFragment presentation");
return new MvxDialogFragmentPresentationAttribute();
}
if (viewType.IsSubclassOf(typeof(Fragment)))
{
MvxTrace.Trace($"PresentationAttribute not found for {viewModelType.Name}. " +
$"Assuming Fragment presentation");
return new MvxFragmentPresentationAttribute(GetCurrentActivityViewModelType(), Android.Resource.Id.Content);
}

MvxTrace.Trace($"PresentationAttribute not found for {viewModelType.Name}. " +
$"Assuming Activity presentation");
return new MvxActivityPresentationAttribute() { ViewModelType = viewModelType };
}

Expand Down Expand Up @@ -314,7 +334,7 @@ protected virtual void ShowFragment(
}
if (!attribute.EnterAnimation.Equals(int.MinValue) && !attribute.ExitAnimation.Equals(int.MinValue))
{
if(!attribute.PopEnterAnimation.Equals(int.MinValue) && !attribute.PopExitAnimation.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);
Expand Down Expand Up @@ -462,7 +482,7 @@ protected virtual string FragmentJavaName(Type fragmentType)
return Class.FromType(fragmentType).Name;
}

protected virtual IMvxFragmentView CreateFragment(MvxBasePresentationAttribute attribute,
protected virtual IMvxFragmentView CreateFragment(MvxBasePresentationAttribute attribute,
string fragmentName)
{
try
Expand Down

0 comments on commit 32092d6

Please sign in to comment.