aspnetde / MvvmNano

The small and smart MVVM framework made with ❤ for Xamarin.Forms.
MIT License
59 stars 10 forks source link

SetUpMainPage should work with MvvmNanoViewModelBase instead #12

Closed opcodewriter closed 8 years ago

opcodewriter commented 8 years ago

SetUpMainPage accepts only a MvvmNanoViewModel view-model type. If you have a MvvmNanoViewModel<T> view-model, you can't use it with SetUpMainPage.

Scenario: I have a login page and registration page. After use registered successfully, I want to navigate to login page and remove the registration page from back-stack. For that I need to know in presenter whether the registration page should really be removed. So I was thinking to use an initialization parameter for the view-model which I can check in view-presenter and remove registration page from back-stack.

For this particular scenario where I need to remove page from back-stack (a logic for the view presenter), maybe a more correct approach would be to have a presentation parameter in NavigateTo, similar to how MvvmCross has it, which can be used by presenter to do some specific things.

For now, not sure which is the best workaround..

aspnetde commented 8 years ago

I reached this limitation, too, but I didn't have the time to update the framework (will do so later). But: SetUpMainPage() basically does this:

MainPage = new MvvmNanoNavigationPage(GetPageFor<TViewModel>());

So what you really need is a second GetPageFor() method, which accepts a navigation parameter. And here it is:

public MvvmNanoContentPage<TViewModel> GetPageFor<TViewModel, TNavigationParameter>(TNavigationParameter navigationParameter) where TViewModel : IViewModel<TNavigationParameter>
{
    var viewModel = MvvmNanoIoC.Resolve<TViewModel>() as IViewModel<TNavigationParameter>;
    viewModel.Initialize(navigationParameter);

    var page = MvvmNanoIoC
        .Resolve<IPresenter>()
        .CreateViewFor<TViewModel>() as MvvmNanoContentPage<TViewModel>;

    if (page == null)
        throw new MvvmNanoException("Could not create a MvvmNanoContentPage for View Model of type " + typeof(TViewModel) + ".");

    page.SetViewModel(viewModel);

    return page;
}

Having parameters which are directly and only accessible by the Presenter could be a considerable option, for now I am passing those parameters through the Page. So when a ViewModel is being called with a navigation paramter, I can make this available to the presenter with a public property in the Page of the View Model.

opcodewriter commented 8 years ago

Thanks! I think having a clear separation between parameters for presenter and for view-model would be good, otherwise the view-model get a parameter when its logic actually doesn't need one.

aspnetde commented 8 years ago

Agreed and looking forward to your pull request ;)