private bool IsLast(EditArtistViewModel vm) { //var last = this.EditArtistViewModels.Count > 0 ? this.EditArtistViewModels[this.EditArtistViewModels.Count - 1] : null; //return object.ReferenceEquals(last, s); return(this._editArtistViewModelsSourceList.Items.LastOrDefault() == vm); }
//private readonly OrderedDictionary #endregion #region ctors public EditArtistsViewModel(IEnumerable <string> initialStrings) { this._stringsSet = new HashSet <string>(initialStrings); var initialStringViewModels = initialStrings.Select(s => new EditArtistViewModel(s)); this._editArtistViewModelsSourceList = new SourceList <EditArtistViewModel>().DisposeWith(this._disposables); this._editArtistViewModelsSourceList.AddRange(initialStringViewModels); this._editArtistViewModelsSourceList .Connect() .Bind(out this._editArtistViewModelsROOC) .DisposeMany() .Subscribe() .DisposeWith(this._disposables); this.WhenSelectedEditArtistViewModelChanged = this.WhenAnyValue(x => x.SelectedEditArtistViewModel).DistinctUntilChanged(); this._hasEditArtistViewModelSelectionOAPH = this.WhenSelectedEditArtistViewModelChanged .Select(x => x != null) .DistinctUntilChanged() .ToProperty(this, nameof(this.HasEditArtistViewModelSelection) //, true // works //, false , scheduler: Scheduler.Immediate ) .DisposeWith(this._disposables); this.WhenSelectedEditArtistViewModelChanged.Subscribe( selectedEAVM => { if (selectedEAVM != null) { this.NewArtistName = null; } }) .DisposeWith(this._disposables); this.AddNew = ReactiveCommand.Create( () => { var newEditArtistViewModel = new EditArtistViewModel(this.NewArtistName); this.NewArtistName = null; this._editArtistViewModelsSourceList.Edit(list => list.Add(newEditArtistViewModel)); //this.SelectedEditArtistViewModel = newEditArtistViewModel; //return newEditArtistViewModel; }, this.WhenAnyValue(x => x.NewArtistName) /*.Throttle(TimeSpan.FromMilliseconds(200))*/.Select(nan => this.IsValidNewArtistName(nan))) .DisposeWith(this._disposables); this.MoveSelectedUp = ReactiveCommand.Create( () => { var selection = this.SelectedEditArtistViewModel; this._editArtistViewModelsSourceList.Edit(list => { var oldIndex = list.IndexOf(selection); if (oldIndex > 0 && list.Remove(selection)) { list.Insert(oldIndex - 1, selection); } }); this.SelectedEditArtistViewModel = selection; }, this.WhenSelectedEditArtistViewModelChanged.Select(x => x != null && !this.IsFirst(x))) .DisposeWith(this._disposables); this.MoveSelectedDown = ReactiveCommand.Create( () => { var selection = this.SelectedEditArtistViewModel; this._editArtistViewModelsSourceList.Edit(list => { var oldIndex = list.IndexOf(selection); if (oldIndex < (list.Count - 1) && list.Remove(selection)) { list.Insert(oldIndex + 1, selection); } }); this.SelectedEditArtistViewModel = selection; }, this.WhenSelectedEditArtistViewModelChanged.Select(x => x != null && !this.IsLast(x))) .DisposeWith(this._disposables); this.TryRemove = ReactiveCommand.Create( (EditArtistViewModel vm) => { bool wasRemoved = false; this._editArtistViewModelsSourceList.Edit(list => { wasRemoved = list.Remove(vm); }); return(wasRemoved); }) .DisposeWith(this._disposables); this.RemoveSelected = ReactiveCommand.Create( () => { this._editArtistViewModelsSourceList.Edit(list => { if (!list.Remove(this.SelectedEditArtistViewModel)) { // TODO: throw exception if passed element is not contained in the list } }); }, this.WhenAnyValue(x => x.SelectedEditArtistViewModel).Select(x => x != null)) .DisposeWith(this._disposables); }
private bool IsFirst(EditArtistViewModel vm) { //var first = this.EditArtistViewModels.Count > 0 ? this.EditArtistViewModels[0] : null; //return object.ReferenceEquals(first, vm); return(this._editArtistViewModelsSourceList.Items.FirstOrDefault() == vm); }