Create custom presenter for my mvvmcross project

mvvmcross

Solution

A presenter is just something that implements https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Core/Core/Views/IMvxViewPresenter.cs

public interface IMvxViewPresenter
{
    void Show(MvxViewModelRequest request);
    void ChangePresentation(MvxPresentationHint hint);
}

This is are really simple interface and it allows shared portable code like ViewModels to request changes in the display.

For the case where you want a `Show` request to change the entire UI from one display paradigm (modal navigation controller) to another (sliding panels), then one way to do this is to implement a presenter which has two child presenters and which simply switches them over.

In pseudo code this might look like:

public class MyPresenter : IMvxViewPresenter
{
    private IMvxViewPresenter _currentPresenter;

    private ModalPresenter _modalPresenter;
    private SlidingPresenter _slidingPresenter;

    private enum Style
    {
       Modal, Panels
    }

    private Style _currentStyle;

    public MyPresenter()
    {
        // do whatever you need to do here to:
        //  - construct _modalPresenter and _slidingPresenter
        //  - make _modalPresenter attached to the window (via root view controller)
        //  - make _slidingPresenter hidden/unattached

        _currentStyle = Style.Modal;
        _currentPresenter = _modalPresenter;
    }


    public void Show(MvxViewModelRequest request)
    {
        if (_currentStyle == Style.Modal &&
            request.ViewModelType == typeof(WhateverViewModelIndicatesTheSwitchIsNeeded))
        {
            DoSwitch(request);
            return;
        }

        _currentPresenter.Show(request);
    }

    public void ChangePresentation(MvxPresentationHint hint)
    {
        _currentPresenter.ChangePresentation(hint);
    }

    private void DoSwitch(MvxViewModelRequest request)
    {
        // do whatever is necessary to:
        // - remove _modalPresenter from the window
        // - add _panelPresenter to the window
        // - show `request` within _panelPresenter 

        _currentPresenter = _panelPresenter;
        _currentStyle = Style.Panelsl
    }
}

Obviously, there are some details to fill in within this pseudo-code - e.g. there are some viewcontrollers to be added and removed from the window - but this is just standard iOS manipulation - e.g. see lots of questions and answers like Changing root view controller of a iOS Window and Change rootViewController from uiviewcontroller to uinavigationcontroller

Problem

I'm working on a project for iOS using mvvmcross. App navigation goes like this: first it starts from the splash screen (1), them it navigates to (2), a view to select between 3 options, in view (3) and (4) you get a list and also could navigate back to (2), if you select an item in (3) you navigate to (5) in a modal way. Lastly, all navigation end up in (6), a view with an hamburger menu. So I have traditional navigation(with back button), modal views and a hamburger menu at the end. It would be great if someone could help me or guide me to see how to create a custom presenter for this navigation scheme. I'm using MvxModalNavSupportTouchViewPresenter and a SlidingPanelsNavigationViewController, but don't know how to swap them when I navigate from (2,4,5) to (6)

Original source