Created
June 20, 2021 13:40
-
-
Save rdelrosario/e5aacc16f560749fbd4bccf964baec79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ReactiveToDoSample.ViewModels | |
{ | |
public class ItemViewModel : ViewModelBase | |
{ | |
public ItemViewModel(IParameterViewStackService navigationService, IItemManager itemManager) : base(navigationService) | |
{ | |
_itemManager = itemManager; | |
var canExecute = this.WhenAnyValue(x => x.Title, (title) => !string.IsNullOrEmpty(title)); | |
SaveCommand = ReactiveCommand.Create(ExecuteSave, canExecute); | |
CloseCommand = ReactiveCommand.CreateFromObservable(() => NavigationService.PopModal()); | |
SaveCommand | |
.InvokeCommand(CloseCommand) | |
.DisposeWith(Subscriptions); | |
this.WhenAnyValue(x => x.ItemId) | |
.Where(x => x != null) | |
.Select(x => _itemManager.Get(x)) | |
.Where(x => x.HasValue) | |
.Select(x => x.Value) | |
.Subscribe(x => | |
{ | |
Title = x.Title; | |
}) | |
.DisposeWith(Subscriptions); | |
} | |
public override IObservable<Unit> WhenNavigatingTo(INavigationParameter parameter) | |
{ | |
if (parameter.TryGetValue(NavigationParameterConstants.ItemId, out string itemId)) | |
{ | |
ItemId = itemId; | |
} | |
return base.WhenNavigatedTo(parameter); | |
} | |
private void ExecuteSave() => _itemManager.AddOrUpdate(new Item(ItemId ?? Guid.NewGuid().ToString(), Title)); | |
public ReactiveCommand<Unit, Unit> SaveCommand { get; } | |
public ReactiveCommand<Unit, Unit> CloseCommand { get; } | |
public override string Id => string.Empty; | |
public string Title | |
{ | |
get => _title; | |
set => this.RaiseAndSetIfChanged(ref _title, value); | |
} | |
private string ItemId | |
{ | |
get => _itemId; | |
set => this.RaiseAndSetIfChanged(ref _itemId, value); | |
} | |
private string _title; | |
private string _description; | |
private readonly IItemManager _itemManager; | |
private string _itemId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment