Tornike Gomareli
Tornike Gomareli

Reputation: 1709

Disable and Enable Button on Textfield change with ReactiveUI

I'm new in reactive programming and also a newbie for using ReactiveUI so I need a little help.

I'm working on a project, we are using Xamarin-iOS for iOS platform.

I have simple Login ViewController, where I want to enable button immediately when TextField state will change.

I'have those fields.

        private string user;
        private string password;
        private ReactiveCommand<Unit,bool> loginCommand;
        private ReactiveCommand<Unit,Unit> canselCommand;

And I'm using this.WhenAnyValue method

var canLogin = this.WhenAnyValue(x => x.user, x => x.password, (userName, password) =>
                                              !string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password));

            this.loginCommand = ReactiveCommand.CreateFromObservable(
            () =>
               Observable
                .Return(this.passwordTextField.Text == "Tornike"))
                .Delay(TimeSpan.FromSeconds(1.5))
                .TakeUntil(this.canselCommand);

Where user and password are private string fields. And also I have passwordTextField and userNameTextField UILabels on UI.

Everywhere there are redline errors, and I'm scared a little bit.

So how I can just enable the button when textFields will change? Thanks.

Upvotes: 2

Views: 1079

Answers (2)

mm8
mm8

Reputation: 169270

canLogin should be the second argument passed to the CreateFromObservable method. The first argument should be a Func<IObservable<bool>>. This should compile:

this.loginCommand = ReactiveCommand.CreateFromObservable(
() =>
    Observable
    .Return(this.passwordTextField.Text == "Tornike")
    .Delay(TimeSpan.FromSeconds(1.5))
    .TakeUntil(this.canselCommand), canLogin);

But for the IObservable<bool> to produce new values and refresh the state of the command whenever user or password is changed, you should implement user and password as properties and raise the PropertyChanged event whenever they are set to a new value, e.g.:

public string User
{
    get { return user;}
    set { this.RaiseAndSetIfChanged(ref user, value);
}

Upvotes: 1

Luis
Luis

Reputation: 437

You need to create properties for using the WhenAnyValue extension method:

public string User
{
    get { return user;}
    set { this.RaiseAndSetIfChanged(ref user, value);
}

public string Password
{
    get { return password;}
    set { this.RaiseAndSetIfChanged(ref password, value);
}

And in the command inicialization:

this.loginCommand = ReactiveCommand.CreateFromObservable(
        () =>
           Observable
            .Return(this.passwordTextField.Text == "Tornike"))
            .Delay(TimeSpan.FromSeconds(1.5))
            .TakeUntil(this.canselCommand),
canExecute: canLogin);

Use User and Password in the WhenAnyValue instead user/password fields. Also, use that properties in your code and in the bind (for TextFields).

Upvotes: 0

Related Questions