Show login dialog then a main form on successful connection

Topics: Actions & Coroutines, Bootstrappers & IoC, Getting Started, UI Architecture
Dec 3, 2012 at 5:56 PM

Hi, I am using Caliburn Micro and have a login view model which is displayed on startup.

I am using a separate class which handles all of the connection to the server and provides simple call back events to the ViewModel, this is the ITransportClient.

The users enters their credentials, hits login, and the dialog shows the connection process along a number of states (connecting, validating username, downloading configuration). In the background the Login ViewModel will have called ITransportClient.Login().

If logged in OK and all steps are completed, the form should then close and the main window ViewModel should open up. If the credentials are incorrect or a problem downloading settings, an error should be displayed and the login form remain.

If connection is lost to the server (indicated via ITransportClient event), then the application should attempt to reconnect a number of times, and if the server remains offline for a configurable period of time then the login window should be displayed again.

  1. How would I best handle the switching between the login dialog and the main window as per the above flow?
  2. How can the login ViewModel close itself, I see IWindowManager only has ShowDialogShowPopupand ShowWindow methods?
  3. What is the best way to separate the above out, allowing the login window to be closed external to the Login ViewModel, and for the login window to be displayed when the user logs out the main window? Should this be done in the bootstrap, or should a separate ViewModel shell be created for this?

Many thanks, additional info below:

My bootstrapper:

public class SimpleInjectorBootstrapper : Caliburn.Micro.Bootstrapper
{
   private Container container;

   protected override void Configure()
   {
       this.container = new Container();
       this.container.Register<IWindowManager, WindowManager>();
       this.container.Register<IEventAggregator, EventAggregator>();
       this.container.Register<IAppViewModel, AppViewModel>();
       this.container.Register<ILoginViewModel, LoginViewModel>();
       this.container.RegisterSingle<ITransportClient, Transport.WCF.TransportClient>();
   }

   protected override object GetInstance(Type serviceType, string key)
   {
       return this.container.GetInstance(serviceType);
   }

   protected override IEnumerable<object> GetAllInstances(Type serviceType)
   {
       return this.container.GetAllInstances(serviceType);
   }

   protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
   {
       base.OnStartup(sender, e);
       var loginViewModel= this.container.GetInstance<ILoginViewModel>();
       var windowManager = this.container.GetInstance<IWindowManager>();
       windowManager.ShowWindow(loginViewModel);
   }
}

My LoginView model is below:

public class LoginViewModel : PropertyChangedBase, ILoginViewModel
{
    private readonly ITransportClient transportClient;
    private readonly IWindowManager windowManager;
    private string connectionStatus;

    public LoginViewModel(ITransportClient transportClient, IWindowManager windowManager)
    {
        this.transportClient = transportClient;
        this.windowManager = windowManager;
        this.transportClient.ConnectionEvent += new TransportConnectionEventHandler(UpdateStatusHandler);
    }

    public void Login()
    {
        // set from view, to be done via property, implement later
        var username = "test";
        var password = "test";

        var result = this.transportClient.Login(username, password);

        // if result is ok, we should close our viewmodel, however we 
        // cant call IWindowManager.Close(this) as only show methods exist
        // perhaps this is better handled elsewhere?
    }

    public void UpdateStatusHandler(string status)
    {
        this.ConnectionStatus = status;
    }

    public string ConnectionStatus
    {
        get
        {
            return this.connectionStatus;
        }

        set
        {
            this.connectionStatus = value;
            NotifyOfPropertyChange(() => ConnectionStatus);
        }
    }
}