Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

BuildIt.States from a ViewModel

In my previous post on State Management using BuildIt.States I demonstrated using the BuildIt.States library to declare states for a page. However, it’s not enough to declare states in the UI space. The management of state should be done in the ViewModel space, making it possible to test and keep the logic separate from the UI implementation.

To do this, I can create another instance of the StateManager within the ViewModel for the page. I need to declare the same states, just without any of the additional attributes required to update the user interface. The ViewModel ends up looking like the following – I’ve included a StateName property and a method, UpdateState, which can be invoked to change the state.

public class MainViewModel : NotifyBase, IHasStates
{
    public IStateManager StateManager { get; } = new StateManager();
    private string stateName;
    public string StateName
    {
        get { return stateName; }
        set
        {
            stateName = value;
            OnPropertyChanged();
        }
    }
    public MainViewModel()
    {
        StateManager.Group<MainStates>()
            .DefineState(MainStates.StateOne)
            .DefineState(MainStates.StateTwo)
            .DefineState(MainStates.StateThree)
            .DefineState(MainStates.StateFour);
    }
    public async Task UpdatState(MainStates state)
    {
        var ok = await StateManager.GoToState(state);
        if (ok)
        {
            StateName = state.ToString();
        }
    }
}

Now, in the UI space, the page needs to wire to and unwire from the StateChanged event handler that is available on each of the StateGroup entities in the StateManager.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var sm = ViewModel.StateManager;
    var grp = sm.StateGroups[typeof(MainStates)] as StateGroup<MainStates, DefaultTransition>;
    grp.StateChanged += Grp_StateChanged;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);

    var sm = ViewModel.StateManager;
    var grp = sm.StateGroups[typeof(MainStates)] as StateGroup<MainStates, DefaultTransition>;
    grp.StateChanged -= Grp_StateChanged;
}

private void Grp_StateChanged(object sender, StateEventArgs<MainStates> e)
{
    manager.GoToState(e.State, e.UseTransitions);
}

I also updated each of the event handlers for the buttons to call the UpdateState method on the ViewModel. Now I can control the state of the UI by updating the state of the StateManager within the ViewModel.

Pingbacks and trackbacks (1)+

Comments are closed