diff --git a/ReactiveUIDemo/ReactiveUIDemo.iOS/Main.cs b/ReactiveUIDemo/ReactiveUIDemo.iOS/Main.cs index cfb0575..97ae6db 100644 --- a/ReactiveUIDemo/ReactiveUIDemo.iOS/Main.cs +++ b/ReactiveUIDemo/ReactiveUIDemo.iOS/Main.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -using Foundation; -using UIKit; +using UIKit; namespace ReactiveUIDemo.iOS { diff --git a/ReactiveUIDemo/ReactiveUIDemo.iOS/ReactiveUIDemo.iOS.csproj b/ReactiveUIDemo/ReactiveUIDemo.iOS/ReactiveUIDemo.iOS.csproj index 5694d0d..5bd4add 100644 --- a/ReactiveUIDemo/ReactiveUIDemo.iOS/ReactiveUIDemo.iOS.csproj +++ b/ReactiveUIDemo/ReactiveUIDemo.iOS/ReactiveUIDemo.iOS.csproj @@ -139,7 +139,7 @@ - {F5534636-F3BA-43CC-B276-DAD365A7EC72} + {C3060E03-DF8C-4422-8EA2-B80BF41DF56A} ReactiveUIDemo diff --git a/ReactiveUIDemo/ReactiveUIDemo/Services/LoginService.cs b/ReactiveUIDemo/ReactiveUIDemo/Services/LoginService.cs index d12b7db..d17486e 100644 --- a/ReactiveUIDemo/ReactiveUIDemo/Services/LoginService.cs +++ b/ReactiveUIDemo/ReactiveUIDemo/Services/LoginService.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; using System.Threading.Tasks; namespace ReactiveUIDemo.Services diff --git a/ReactiveUIDemo/ReactiveUIDemo/ViewModel/ItemsViewModel.cs b/ReactiveUIDemo/ReactiveUIDemo/ViewModel/ItemsViewModel.cs index 19ec5b6..c9a08c3 100644 --- a/ReactiveUIDemo/ReactiveUIDemo/ViewModel/ItemsViewModel.cs +++ b/ReactiveUIDemo/ReactiveUIDemo/ViewModel/ItemsViewModel.cs @@ -1,11 +1,7 @@ -using ReactiveUI; -using ReactiveUIDemo.Model; -using System; -using System.Collections.Generic; -using System.Diagnostics; +using System; using System.Reactive.Linq; -using System.Text; -using System.Threading.Tasks; +using ReactiveUI; +using ReactiveUIDemo.Model; namespace ReactiveUIDemo.ViewModel { @@ -23,35 +19,28 @@ public ReactiveList Todos private Todo _selectedTodo; public Todo SelectedTodo { - get => _selectedTodo; - set => this.RaiseAndSetIfChanged(ref _selectedTodo , value); + get => _selectedTodo; + set => this.RaiseAndSetIfChanged(ref _selectedTodo, value); } - private ObservableAsPropertyHelper _canAdd; - public bool CanAdd => _canAdd?.Value ?? false; - private string _todoTitl; public string TodoTitle { get { return _todoTitl; } - set {this.RaiseAndSetIfChanged(ref _todoTitl, value); } + set { this.RaiseAndSetIfChanged(ref _todoTitl, value); } } public ReactiveCommand AddCommand { get; private set; } public ItemsViewModel(IScreen hostScreen = null) : base(hostScreen) { - this.WhenAnyValue(x => x.TodoTitle, - title => - !String.IsNullOrEmpty(title)).ToProperty(this, x => x.CanAdd, out _canAdd); + var canAdd = this.WhenAnyValue(x => x.TodoTitle, title => !String.IsNullOrEmpty(title)); - AddCommand = ReactiveCommand.CreateFromTask( () => + AddCommand = ReactiveCommand.Create(() => { Todos.Add(new Todo() { Title = TodoTitle }); TodoTitle = string.Empty; - return Task.CompletedTask; - - }, this.WhenAnyValue(x => x.CanAdd, canAdd => canAdd && canAdd)); + }, canAdd); //Dont forget to set ChangeTrackingEnabled to true. Todos = new ReactiveList() { ChangeTrackingEnabled = true }; @@ -64,16 +53,18 @@ public ItemsViewModel(IScreen hostScreen = null) : base(hostScreen) ///Lets detect when ever a todo Item is marked as done ///IF it is, it is sent to the bottom of the list ///Else nothing happens - Todos.ItemChanged.Where(x => x.PropertyName == "IsDone" && x.Sender.IsDone) + Todos + .ItemChanged + .Where(x => x.PropertyName == "IsDone" && x.Sender.IsDone) .Select(x => x.Sender) .Subscribe(x => - { - if (x.IsDone) - { - Todos.Remove(x); - Todos.Add(x); - } - }); + { + if (x.IsDone) + { + Todos.Remove(x); + Todos.Add(x); + } + }); } } } diff --git a/ReactiveUIDemo/ReactiveUIDemo/ViewModel/LoginViewModel.cs b/ReactiveUIDemo/ReactiveUIDemo/ViewModel/LoginViewModel.cs index e4500d3..fe4ae92 100644 --- a/ReactiveUIDemo/ReactiveUIDemo/ViewModel/LoginViewModel.cs +++ b/ReactiveUIDemo/ReactiveUIDemo/ViewModel/LoginViewModel.cs @@ -1,10 +1,9 @@ -using ReactiveUI; -using ReactiveUIDemo.Services; -using System; -using System.Collections.Generic; -using System.Reactive.Concurrency; -using System.Text; +using System; +using System.Reactive; +using System.Reactive.Linq; using System.Text.RegularExpressions; +using ReactiveUI; +using ReactiveUIDemo.Services; namespace ReactiveUIDemo.ViewModel { @@ -27,54 +26,35 @@ public string Password set => this.RaiseAndSetIfChanged(ref _password, value); } - /// - /// This is an Oaph Observable propperty helper, - /// Which is used to determine whether a subsequent action - /// Could be performed or not depending on its value - /// This condition is calculated every time its value changes. - /// - ObservableAsPropertyHelper _validLogin; - public bool ValidLogin - { - get { return _validLogin?.Value ?? false; } - } - - public ReactiveCommand LoginCommand { get; private set; } - + public ReactiveCommand LoginCommand { get; private set; } + public LoginViewModel(ILogin login, IScreen hostScreen = null) : base(hostScreen) { _loginService = login; - this.WhenAnyValue(x => x.UserName, x => x.Password, + var canLogin = this.WhenAnyValue(x => x.UserName, x => x.Password, (email, password) => ( - ///Validate the password + //Validate the password !string.IsNullOrEmpty(password) && password.Length > 5 ) && ( - ///Validate teh email. + //Validate the email. !string.IsNullOrEmpty(email) - && - Regex.Matches(email, "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$").Count == 1 - )) - .ToProperty(this, v => v.ValidLogin, out _validLogin); - - LoginCommand = ReactiveCommand.CreateFromTask(async () => - { - - var lg = await login.Login(_userName, _password); - if (lg) - { - HostScreen.Router - .Navigate - .Execute(new ItemsViewModel()) - .Subscribe(); - } - }, this.WhenAnyValue(x => x.ValidLogin, x => x.ValidLogin, (validLogin, valid) => ValidLogin && valid)); - + && + Regex.Matches(email, "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$").Count == 1 + )); + + LoginCommand = ReactiveCommand.CreateFromTask(_ => login.Login(_userName, _password), canLogin); + + LoginCommand + .Where(logged => logged) + .SelectMany(logged => HostScreen + .Router + .Navigate + .Execute(new ItemsViewModel())) + .Subscribe(); } - - } }