private void Devices_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: var added = ((IAudioDevice)e.NewItems[0]); var allExistingAdded = AllDevices.FirstOrDefault(d => d.Id == added.Id); if (allExistingAdded == null) { AddDevice(added); } break; case NotifyCollectionChangedAction.Remove: var removed = ((IAudioDevice)e.OldItems[0]).Id; var allExisting = AllDevices.FirstOrDefault(d => d.Id == removed); if (allExisting != null) { AllDevices.Remove(allExisting); } break; case NotifyCollectionChangedAction.Reset: AllDevices.Clear(); foreach (var device in _deviceManager.Devices) { AddDevice(device); } break; default: throw new NotImplementedException(); } }
public DevicesListViewModel(IDeviceRelatedRepository repo, IUserSession userSession) { Repository = repo; UserSession = userSession; AllDevices = Repository.AllDevices.ToList(); InitDevicesLocationWithInstances(); FilterDevicesAccordingToCriteria(); ShowAddDeviceViewCommand = RegisterCommandAction( (obj) => { AddDeviceView = new AddDeviceView(); AddDeviceView.DataContext = ResolveDependency <IAddDeviceViewModel>(); AddDeviceView.ShowDialog(); }, (obj) => UserSession.IsAuthorizedUserAllowedTo(UserActions.AddDevice) ); RemoveDeviceCommand = RegisterCommandAction( (obj) => { Repository.RemoveDevice(SelectedDevice); Repository.DeleteAllDeviceMovementHistory(SelectedDevice); Repository.SaveChanges(); AllDevices.Remove(AllDevices.Find(d => d.ID == SelectedDevice.ID)); FilteredDevices.Remove(SelectedDevice); }, (obj) => { if (UserSession.IsAuthorizedUserAllowedTo(UserActions.RemoveDevice)) { return(SelectedDevice != null); } else { return(false); } } ); SubscribeActionOnDeviceAddition( (device) => { device.DeviceType = Repository.AllDeviceTypes.Single(dt => dt.ID == device.DeviceTypeID); device.Cabinet = Repository.FindCabinet(device.CabinetID); device.Cabinet.Housing = DeviceLocationViewModel. AllHousings. First(h => h.ID == device.Cabinet.HousingID); AllDevices.Add(device); if (DevicesFilter.DoesMeetSearchingAndFilteringCriteria(device)) { FilteredDevices.Add(device); } } ); SubscribeActionOnFilteringCriteraChanges( (filteredDevices) => FilteredDevices = filteredDevices.ToObservableCollection() ); }