Search code examples
iosmvvmcross

Registration screen workflow before rest of app in mvvmcross


We have a registration flow in our MVVMCross based app which the user must pass through on first use - before using any functionality in the app.

Currently, we are popping up the registration view from within the first ViewModel's Init() but this feels clunky, eg:

 public class HomeViewModel: MvxViewModel{
   public void Init(){
        if (!RegistrationComplete){
           ShowViewModel<RegisterViewModel>();
        }
    }
  }

Instead of above, we considered setting the RegistrationViewModel as the AppStart then 'swapping' app start back to HomeViewModel, but we are unable to determine how to accomplish this 'swap'

What would be the recommended way to accomplish this register to continue type behavior in mvvmcross?


Solution

  • One way to do this is introduced in https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#custom-imvxappstart

    It uses a CustomAppStart object:

    public class CustomAppStart
        : MvxNavigatingObject
        , IMvxAppStart
    {
        public void Start(object hint = null)
        {
            var auth = Mvx.Resolve<IAuth>();
            if (auth.Check())
            {
                ShowViewModel<HomeViewModel>();
            }
            else
            {
                ShowViewModel<LoginViewModel>();
            }
        }
    }
    

    This is registered in app.cs as:

    RegisterAppStart(new CustomAppStart());