Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View locator changes #1311

Merged
merged 4 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/ReactiveUI.Tests/ViewLocatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using ReactiveUI;
using Splat;
using Xunit;

Expand Down Expand Up @@ -72,6 +71,29 @@ object IViewFor.ViewModel
public IFooViewModel ViewModel { get; set; }
}

public interface StrangeInterfaceNotFollowingConvention { }

public class StrangeClassNotFollowingConvention : StrangeInterfaceNotFollowingConvention { }

public interface IRoutableFooViewModel : IRoutableViewModel { }

public class RoutableFooViewModel : ReactiveObject, IRoutableFooViewModel
{
public IScreen HostScreen { get; set; }
public string UrlPathSegment { get; set; }
}

public class RoutableFooView : IViewFor<IRoutableFooViewModel>
{
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (IRoutableFooViewModel)value; }
}
public IRoutableFooViewModel ViewModel { get; set; }
}


public class DefaultViewLocatorTests
{
[Fact]
Expand Down Expand Up @@ -332,5 +354,40 @@ public void AnErrorIsRaisedIfTheCreationOfTheViewFails()
Assert.Equal("This is a test failure.", ex.Message);
}
}

[Fact]
public void WithOddInterfaceNameDoesntThrowException()
{
var resolver = new ModernDependencyResolver();

resolver.InitializeSplat();
resolver.InitializeReactiveUI();

using (resolver.WithResolver()) {
var fixture = new DefaultViewLocator();

var vm = new StrangeClassNotFollowingConvention();

fixture.ResolveView((StrangeInterfaceNotFollowingConvention)vm);
}
}

[Fact]
public void CanResolveViewFromViewModelWithIRoutableViewModelType()
{
var resolver = new ModernDependencyResolver();

resolver.InitializeSplat();
resolver.InitializeReactiveUI();
resolver.Register(() => new RoutableFooView(), typeof(IViewFor<IRoutableFooViewModel>));

using (resolver.WithResolver()) {
var fixture = new DefaultViewLocator();
var vm = new RoutableFooViewModel();

var result = fixture.ResolveView<IRoutableViewModel>(vm);
Assert.IsType<RoutableFooView>(result);
}
}
}
}
15 changes: 10 additions & 5 deletions src/ReactiveUI/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public DefaultViewLocator(Func<string, string> viewModelToViewFunc = null)
/// </item>
/// <item>
/// <description>
/// Repeat steps 1 and 2 with the type resolved from the modified name.
/// Repeat steps 1-4 with the type resolved from the modified name.
/// </description>
/// </item>
/// </list>
Expand Down Expand Up @@ -107,8 +107,13 @@ public IViewFor ResolveView<T>(T viewModel, string contract = null)
return view;
}

var toggledType = ToggleViewModelType(viewModel);
view = this.AttemptViewResolutionFor(toggledType, contract);
view = this.AttemptViewResolutionFor(ToggleViewModelType(viewModel.GetType()), contract);

if (view != null) {
return view;
}

view = this.AttemptViewResolutionFor(ToggleViewModelType(typeof(T)), contract);

if (view != null) {
return view;
Expand All @@ -120,6 +125,7 @@ public IViewFor ResolveView<T>(T viewModel, string contract = null)

private IViewFor AttemptViewResolutionFor(Type viewModelType, string contract)
{
if (viewModelType == null) return null;
var viewModelTypeName = viewModelType.AssemblyQualifiedName;
var proposedViewTypeName = this.ViewModelToViewFunc(viewModelTypeName);
var view = this.AttemptViewResolution(proposedViewTypeName, contract);
Expand Down Expand Up @@ -169,9 +175,8 @@ private IViewFor AttemptViewResolution(string viewTypeName, string contract)
}
}

private static Type ToggleViewModelType<T>(T viewModel)
private static Type ToggleViewModelType(Type viewModelType)
{
var viewModelType = typeof(T);
var viewModelTypeName = viewModelType.AssemblyQualifiedName;

if (viewModelType.GetTypeInfo().IsInterface) {
Expand Down