private void onTimeEntryDeleted(long id) { var viewModel = TimeEntries.Select(c => c.SingleOrDefault(vm => vm.Id == id)).SingleOrDefault(vm => vm != null); if (viewModel == null) { return; } var collection = TimeEntries.SingleOrDefault(c => c.Date == viewModel.Start.Date); if (collection == null) { return; } collection.Remove(viewModel); var indexToInsert = TimeEntries.IndexOf(collection); TimeEntries.Remove(collection); if (collection.Count > 0) { var newCollection = new TimeEntryViewModelCollection(collection.Date.DateTime, collection); TimeEntries.Insert(indexToInsert, newCollection); } RaisePropertyChanged(nameof(IsEmpty)); }
public TimeEntriesViewModel( ITogglDataSource dataSource, IInteractorFactory interactorFactory, IAnalyticsService analyticsService, ISchedulerProvider schedulerProvider, IRxActionFactory rxActionFactory, ITimeService timeService) { Ensure.Argument.IsNotNull(dataSource, nameof(dataSource)); Ensure.Argument.IsNotNull(interactorFactory, nameof(interactorFactory)); Ensure.Argument.IsNotNull(analyticsService, nameof(analyticsService)); Ensure.Argument.IsNotNull(schedulerProvider, nameof(schedulerProvider)); Ensure.Argument.IsNotNull(rxActionFactory, nameof(rxActionFactory)); Ensure.Argument.IsNotNull(timeService, nameof(timeService)); this.interactorFactory = interactorFactory; this.analyticsService = analyticsService; this.schedulerProvider = schedulerProvider; DelayDeleteTimeEntries = rxActionFactory.FromAction <long[]>(delayDeleteTimeEntries); ToggleGroupExpansion = rxActionFactory.FromAction <GroupId>(toggleGroupExpansion); CancelDeleteTimeEntry = rxActionFactory.FromAction(cancelDeleteTimeEntry); groupsFlatteningStrategy = new TimeEntriesGroupsFlattening(timeService); var deletingOrPressingUndo = timeEntriesPendingDeletionSubject.SelectUnit(); var collapsingOrExpanding = ToggleGroupExpansion.Elements; var visibleTimeEntries = interactorFactory.ObserveAllTimeEntriesVisibleToTheUser().Execute() .Select(timeEntries => timeEntries.Where(isNotRunning)) .ReemitWhen(deletingOrPressingUndo) .Select(timeEntries => timeEntries.Where(isNotDeleted)) .Select(group) .ReemitWhen(collapsingOrExpanding); TimeEntries = Observable.CombineLatest( visibleTimeEntries, dataSource.Preferences.Current, groupsFlatteningStrategy.Flatten) .AsDriver(schedulerProvider); Empty = TimeEntries .Select(groups => groups.None()) .AsDriver(schedulerProvider); Count = TimeEntries .Select(log => log.Sum(day => day.Items.Count)) .AsDriver(schedulerProvider); TimeEntriesPendingDeletion = timeEntriesPendingDeletionSubject.AsObservable().AsDriver(schedulerProvider); }