public async Task SaveAsync()
        {
            if (IsManual)
            {
                return;
            }

            if (!data.PublicInstancePropertiesEqual(initialState))
            {
                data = await TimeEntryModel.PrepareForSync(data);

                data = await TimeEntryModel.SaveTimeEntryDataAsync(data).ConfigureAwait(false);
            }

            if (!AreEqual(initialTagList, TagList))
            {
                await ResetTagRelatioships(data, TagList).ConfigureAwait(false);

                data = await TimeEntryModel.SaveTimeEntryDataAsync(data).ConfigureAwait(false);
            }
        }
Esempio n. 2
0
        private async Task <TimeEntryData> SaveTagRelationships(TimeEntryData timeEntry, List <TagData> newTagList)
        {
            // Create tag list.
            var dataStore            = ServiceContainer.Resolve <IDataStore> ();
            var existingTagRelations = new List <TimeEntryTagData> ();

            var tags = await dataStore.Table <TimeEntryTagData> ()
                       .Where(r => r.TimeEntryId == timeEntry.Id && r.DeletedAt == null)
                       .ToListAsync();

            existingTagRelations.AddRange(tags);

            // Delete unused tag relations:
            var deleteTasks = existingTagRelations
                              .Where(oldTagRelation => newTagList.All(newTag => newTag.Id != oldTagRelation.TagId))
                              .Select(tagRelation => new TimeEntryTagModel(tagRelation).DeleteAsync())
                              .ToList();

            // Create new tag relations:
            var createTasks = newTagList
                              .Where(newTag => existingTagRelations.All(oldTagRelation => oldTagRelation.TagId != newTag.Id))
                              .Select(data => new TimeEntryTagModel {
                TimeEntry = new TimeEntryModel(timeEntry), Tag = new TagModel(data)
            }.SaveAsync())
                              .ToList();

            await Task.WhenAll(deleteTasks.Concat(createTasks));

            if (deleteTasks.Any <Task> () || createTasks.Any <Task> ())
            {
                timeEntry = await TimeEntryModel.PrepareForSync(timeEntry);

                timeEntry = await TimeEntryModel.SaveTimeEntryDataAsync(timeEntry);
            }

            return(timeEntry);
        }