When this ViewModel
loads we kick off the IsAuthenticatedCommand
:
- If the result of
IsAuthenticatedCommand
isfalse
we set the value ofNeedsToLogin
to true. - If the result of
IsAuthenticatedCommand
istrue
we set the value ofIsAuthenticated
to true.
There are (subscriptions?) to both NeedsToLogin
and IsAuthenticated
that are waiting for the true value.
- Is there a better way to handle this
if
/else
logic? - Should I be worried about race conditions?
public class LoadingViewModel : ReactiveObject, IRoutableViewModel {
private bool _isAuthenticated = false;
public bool IsAuthenticated {
get { return _isAuthenticated; }
set { this.RaiseAndSetIfChanged (ref _isAuthenticated, value); }}
private bool _needsToLogin;
public bool NeedsToLogin {
get { return _needsToLogin; }
set { this.RaiseAndSetIfChanged (ref _needsToLogin, value); }}
public ReactiveCommand<bool> IsAuthenticatedCommand { get; protected set; }
public LoadingViewModel (IScreen screen)
{
HostScreen = screen;
this.WhenAnyValue (x => x.IsAuthenticated)
.Where (x => x == true)
.Subscribe (x => { HostScreen.Router.Navigate.ExecuteAsyncTask (new OnboardingViewModel (HostScreen));});
this.WhenAnyValue (x => x.NeedsToLogin)
.Where (x => x == true)
.Subscribe (_ => { HostScreen.Router.Navigate.ExecuteAsyncTask (new LoginViewModel (HostScreen)); });
//always return false for testing.
IsAuthenticatedCommand = ReactiveCommand.CreateAsyncTask (async (_) => await Task.Run (() => {return false;}));
IsAuthenticatedCommand.Subscribe (_ => {
if (_) { this.IsAuthenticated = true;} else {this.NeedsToLogin = true;}
});
}
}