Skip to content

Instantly share code, notes, and snippets.

@escamoteur
Created November 22, 2016 07:19
Show Gist options
  • Save escamoteur/d4be185fd7021a409ce34689699b4eb6 to your computer and use it in GitHub Desktop.
Save escamoteur/d4be185fd7021a409ce34689699b4eb6 to your computer and use it in GitHub Desktop.
RxUI example
public class LoginPageModel : ReactiveObject, IBasePageModel
{
public ReactiveCommand<Unit, bool> SignInCommmand;
public ReactiveCommand<Unit, bool> CreateAccountCommmand;
public ReactiveCommand<Unit, bool> ForgotPassWordCommmand;
public extern bool IsBusy { [ObservableAsProperty] get; }
[Reactive]
public string UserName { get; set; }
[Reactive]
public string Password { get; set; }
public LoginPageModel()
{
var canLogin =
this.WhenAnyValue(x => x.UserName, x => x.Password,
(user, pass) => !String.IsNullOrWhiteSpace(user) && !String.IsNullOrWhiteSpace(pass)
)
.DistinctUntilChanged();
ForgotPassWordCommmand = ReactiveCommand.CreateFromTask(() =>
XamvvmCore.CurrentFactory.SetNewRootAndResetAsync(this.GetCurrentPage(),
this.GetPageFromCache<ForgotPasswordPageModel>(), true));
CreateAccountCommmand = ReactiveCommand.CreateFromTask(() =>
XamvvmCore.CurrentFactory.SetNewRootAndResetAsync(this.GetCurrentPage(),
this.GetPageFromCache<CreateAccountPageModel>(), true));
SignInCommmand = ReactiveCommand.CreateFromTask(Login ,canLogin);
SignInCommmand.ThrownExceptions.ObserveOn(SynchronizationContext.Current).Subscribe(OnLoginException);
}
private async void OnLoginException(Exception exception)
{
if ((exception.GetType()) == typeof(JcLoginFailureException))
{
await UserDialogs.Instance.AlertAsync("Your username or password were incorrect", "Login failure");
System.Diagnostics.Debug.WriteLine(exception.ToString());
return;
}
throw exception;
}
private async Task<bool> Login()
{
var jcUser = await Locator.Current.GetService<IJistClientManager>().LogInUser(UserName, Password);
return await XamvvmCore.CurrentFactory.SetNewRootAndResetAsync(this.GetCurrentPage(),
this.GetPageFromCache<MainMenuTabPageModel>(), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment