private void OnBackgroundEvent() { try { var lastPersistence = new WinPersistenceLayer(); IWorkbook newWorkbook = lastPersistence.Open(); using (this.workbook.WithDuplicateProtection()) { foreach (ITask newTask in newWorkbook.Tasks) { if (this.workbook.Tasks.All(t => t.Id != newTask.Id)) { // this is a new task we don't have in the "current" workbook var oldTask = this.workbook.CreateTask(newTask.Id); ModelHelper.CloneTask(oldTask, newTask, this.workbook); } var existingTask = this.workbook.Tasks.FirstOrDefault(t => t.Id == newTask.Id); if (existingTask != null && existingTask.Modified < newTask.Modified) { // this is a task that has been updated ModelHelper.CloneTask(existingTask, newTask, this.workbook); } } lastPersistence.CloseDatabase(); } } catch (Exception ex) { TrackingManagerHelper.Exception(ex, "BackgroundTaskManager.OnBackgroundEvent"); } }
public async void Run(IBackgroundTaskInstance taskInstance) { var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail; if (details != null && !string.IsNullOrWhiteSpace(details.Argument)) { var persistenceLayer = new WinPersistenceLayer(automaticSave: false); var workbook = persistenceLayer.Open(tryUpgrade: true); workbook.Initialize(); var platformService = new PlatformService( ApplicationVersion.GetAppVersion(), workbook.Settings.GetValue <string>(CoreSettings.DeviceId), () => string.Empty); // important: load alarm manager so that we update reminders properly is a recurring task is created var alarmManager = new AlarmManager(workbook); LaunchArgumentDescriptor descriptor = LaunchArgumentsHelper.GetDescriptorFromArgument(workbook, details.Argument); if (descriptor.Task != null && descriptor.Type == LaunchArgumentType.CompleteTask) { descriptor.Task.IsCompleted = true; persistenceLayer.Save(); UpdateSyncMetadata(workbook, platformService, descriptor); var tileManager = new TileManager(workbook, new TrackingManager(false, DeviceFamily.Unkown), null, true); tileManager.LoadSecondaryTilesAsync().Wait(500); tileManager.UpdateTiles(); // signal changes (usefull if app is currently running) waitHandle.Set(); } } taskInstance.GetDeferral().Complete(); }
private async Task UpdateWorkbookAsync(SynchronizationMetadata metadata) { // load a new workbook from the persisted db to have the latest version var lastPersistence = new WinPersistenceLayer(); IWorkbook lastWorkbook = lastPersistence.Open(); foreach (string folderName in metadata.AfterSyncEditedFolders) { var newFolder = lastWorkbook.Folders.FirstOrDefault(f => f.Name.Equals(folderName, StringComparison.OrdinalIgnoreCase)); var oldFolder = this.workbook.Folders.FirstOrDefault(f => newFolder != null ? f.Id == newFolder.Id : f.Name == folderName); if (newFolder == null) { // folder was deleted this.workbook.RemoveFolder(folderName); } else if (oldFolder != null) { // folder was updated ModelHelper.CloneFolder(oldFolder, newFolder); } else { // folder was created oldFolder = this.workbook.AddFolder(folderName, newFolder.SyncId, newFolder.Id); ModelHelper.CloneFolder(oldFolder, newFolder); } } metadata.AfterSyncEditedFolders.Clear(); foreach (string contextName in metadata.AfterSyncEditedContexts) { var newContext = lastWorkbook.Contexts.FirstOrDefault(c => c.Name.Equals(contextName, StringComparison.OrdinalIgnoreCase)); var oldContext = this.workbook.Contexts.FirstOrDefault(c => newContext != null ? c.Id == newContext.Id : c.Name == contextName); if (newContext == null) { // context was deleted this.workbook.RemoveContext(contextName); } else if (oldContext != null) { // context was updated oldContext.Name = newContext.Name; oldContext.Order = newContext.Order; } else { // context was created this.workbook.AddContext(contextName, newContext.SyncId, newContext.Id); } } metadata.AfterSyncEditedContexts.Clear(); foreach (string smartviewName in metadata.AfterSyncEditedSmartViews) { var newSmartView = lastWorkbook.SmartViews.FirstOrDefault(c => c.Name.Equals(smartviewName, StringComparison.OrdinalIgnoreCase)); var oldSmartView = this.workbook.SmartViews.FirstOrDefault(c => newSmartView != null ? c.Id == newSmartView.Id : c.Name == smartviewName); if (newSmartView == null) { // smart view was deleted this.workbook.RemoveSmartView(smartviewName); } else if (oldSmartView != null) { // smart view was updated oldSmartView.Name = newSmartView.Name; oldSmartView.Order = newSmartView.Order; oldSmartView.Rules = newSmartView.Rules; } else { // smart view was created var smartview = this.workbook.AddSmartView(smartviewName, newSmartView.Rules, newSmartView.Id); smartview.SyncId = newSmartView.SyncId; } } metadata.AfterSyncEditedSmartViews.Clear(); foreach (int id in metadata.AfterSyncEditedTasks) { var oldTask = this.workbook.Tasks.FirstOrDefault(t => t.Id == id); var newTask = lastWorkbook.Tasks.FirstOrDefault(t => t.Id == id); if (newTask == null) { // task was deleted if (oldTask != null) { oldTask.Delete(); } } else if (oldTask != null) { // task was updated ModelHelper.CloneTask(oldTask, newTask, this.workbook); } else { // task was created oldTask = this.workbook.CreateTask(newTask.Id); ModelHelper.CloneTask(oldTask, newTask, this.workbook); } } metadata.AfterSyncEditedTasks.Clear(); lastPersistence.CloseDatabase(); await this.WriteSyncMetadataAsync(metadata); }