private void onTimeEntryUpdated(EntityUpdate <IThreadSafeTimeEntry> update) { var timeEntry = update.Entity; var shouldBeAdded = timeEntry != null && !timeEntry.IsRunning() && !timeEntry.IsDeleted; var oldCollectionIndex = TimeEntries.IndexOf(c => c.Any(vm => vm.Id == update.Id)); var collectionIndex = TimeEntries.IndexOf(vm => vm.Date == timeEntry.Start.LocalDateTime.Date); var wasMovedIntoDifferentCollection = oldCollectionIndex >= 0 && oldCollectionIndex != collectionIndex; var shouldBeRemoved = shouldBeAdded == false || wasMovedIntoDifferentCollection; if (shouldBeRemoved) { safeRemoveTimeEntry(update.Id); } if (shouldBeAdded) { var timeEntryIndex = collectionIndex < 0 ? -1 : TimeEntries[collectionIndex].IndexOf(vm => vm.Id == update.Id); var timeEntryExistsInTheCollection = timeEntryIndex >= 0; if (timeEntryExistsInTheCollection) { var timeEntryViewModel = new TimeEntryViewModel(timeEntry, durationFormat); TimeEntries.ReplaceInChildCollection(collectionIndex, timeEntryIndex, timeEntryViewModel); return; } safeInsertTimeEntry(timeEntry); } }