4
\$\begingroup\$

When this ViewModel loads we kick off the IsAuthenticatedCommand:

  • If the result of IsAuthenticatedCommand is false we set the value of NeedsToLogin to true.
  • If the result of IsAuthenticatedCommand is true we set the value of IsAuthenticated to true.

There are (subscriptions?) to both NeedsToLogin and IsAuthenticated that are waiting for the true value.

  1. Is there a better way to handle this if/else logic?
  2. 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;}
        });
    }
}
\$\endgroup\$
  • \$\begingroup\$ My biggest concern is why you have two bools basically answering the same question. \$\endgroup\$ – Jeff Vanzella Dec 31 '14 at 4:07
1
\$\begingroup\$

You need to format your code neatly; this will improve readability, which will decrease bugs. First, you have this:

public bool IsAuthenticated {
get { return _isAuthenticated; }
set { this.RaiseAndSetIfChanged (ref _isAuthenticated, value); }}

This should be:

public bool IsAuthenticated
{
    get { return _isAuthenticated; }
    set { this.RaiseAndSetIfChanged (ref _isAuthenticated, value); }
}

The if/else statement should be written like this:

IsAuthenticatedCommand.Subscribe (_ => {
    if (_) { this.IsAuthenticated = true; }
    else { this.NeedsToLogin = true; }
});

Also, you need to be consistent with your spacing; sometimes you have a space between braces, sometimes you don't:

if (_) { this.IsAuthenticated = true;} else {this.NeedsToLogin = true;}

I am no expert at lambda statements, but your lambda with the if/else looks fine to me.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.