Skip to content

Instantly share code, notes, and snippets.

@rdelrosario
Created June 20, 2021 13:39
Show Gist options
  • Save rdelrosario/d3a1e23d1d6e061e565effde697fa66b to your computer and use it in GitHub Desktop.
Save rdelrosario/d3a1e23d1d6e061e565effde697fa66b to your computer and use it in GitHub Desktop.
namespace ReactiveToDoSample.ViewModels
{
public class HomeViewModel : ViewModelBase
{
public HomeViewModel(IParameterViewStackService navigationService, IItemManager itemManager) : base(navigationService)
{
DeleteCommand = ReactiveCommand.Create<Item>(itemManager.Remove);
itemManager
.ItemChanges
.Bind(out _items)
.DisposeMany()
.Subscribe()
.DisposeWith(Subscriptions);
itemManager.AddOrUpdate(new Item(Guid.NewGuid().ToString(), "Family vacation planning"));
itemManager.AddOrUpdate(new Item(Guid.NewGuid().ToString(), "Buy Christmas Gifts"));
itemManager.AddOrUpdate(new Item(Guid.NewGuid().ToString(), "Go to the Bank"));
itemManager.AddOrUpdate(new Item(Guid.NewGuid().ToString(), "Buy Milk"));
AddCommand = ReactiveCommand.CreateFromObservable(() => NavigationService.PushModal<ItemViewModel>());
ViewCommand = ReactiveCommand.CreateFromObservable<Item, Unit>((item) =>
{
SelectedItem = null;
return NavigationService.PushModal<ItemViewModel>(new NavigationParameter()
{
{ NavigationParameterConstants.ItemId , item.Id }
});
});
this.WhenAnyValue(x => x.SelectedItem)
.Where(x => x != null)
.InvokeCommand(ViewCommand)
.DisposeWith(Subscriptions);
}
public ReactiveCommand<Unit, Unit> AddCommand { get; }
public ReactiveCommand<Item, Unit> ViewCommand { get; }
public ReactiveCommand<Item, Unit> DeleteCommand { get; }
public Item SelectedItem
{
get => _selectedItem;
set => this.RaiseAndSetIfChanged(ref _selectedItem, value);
}
public ReadOnlyObservableCollection<Item> Items => _items;
public override string Id => "Reactive ToDo";
private readonly ReadOnlyObservableCollection<Item> _items;
private Item _selectedItem;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment