Пример #1
0
        protected override async void OnBackgroundActivated(BackgroundActivatedEventArgs args)
        {
            var taskInstance = args.TaskInstance;

            taskInstance.Canceled += TaskInstance_Canceled;
            base.OnBackgroundActivated(args);
            var triggerDetails = taskInstance.TriggerDetails;
            var taskName       = taskInstance.Task.Name;

            switch (taskName)
            {
            case "AppTrigger":
                _AppTriggerDeferral = taskInstance.GetDeferral();
                if (!(triggerDetails is ApplicationTriggerDetails appTriggerDetails))
                {
                    throw new InvalidOperationException();
                }
                await _ExExecSession.RunActionAsExtentedAction(() =>
                {
                    ExecutionService.RunActionInUiThread(async() =>
                    {
                        try
                        {
                            var appArgs = appTriggerDetails.Arguments;
                            if (!appArgs.TryGetValue("action", out object action))
                            {
                                throw new ArgumentNullException(nameof(action));
                            }
                            if (!appArgs.TryGetValue("what", out object what))
                            {
                                throw new ArgumentNullException(nameof(what));
                            }
                            if (!appArgs.TryGetValue("type", out object type))
                            {
                                throw new ArgumentNullException(nameof(type));
                            }
                            if (!appArgs.TryGetValue("location", out object location))
                            {
                                throw new ArgumentNullException(nameof(location));
                            }
                            if (!appArgs.TryGetValue("filter", out object filter))
                            {
                                throw new ArgumentNullException(nameof(filter));
                            }
                            if (!appArgs.TryGetValue("sendMessage", out object sendMessage))
                            {
                                throw new ArgumentNullException(nameof(type));
                            }

                            if (!(action is string a) || StringHelper.IsNullOrEmptyOrWhiteSpace(a))
                            {
                                throw new ArgumentException($"'{nameof(action)}' has an invalid value");
                            }

                            if (!(what is string w) || StringHelper.IsNullOrEmptyOrWhiteSpace(w))
                            {
                                throw new ArgumentException($"'{nameof(what)}' has an invalid value");
                            }

                            if (!(type is string t) || StringHelper.IsNullOrEmptyOrWhiteSpace(t))
                            {
                                throw new ArgumentNullException(nameof(type));
                            }

                            if (!(location is string l) || StringHelper.IsNullOrEmptyOrWhiteSpace(l))
                            {
                                throw new ArgumentException($"'{nameof(location)}' has an invalid value");
                            }

                            if (!(filter is string f) || StringHelper.IsNullOrEmptyOrWhiteSpace(f))
                            {
                                throw new ArgumentException($"'{nameof(filter)}' has an invalid value");
                            }

                            if (!(sendMessage is bool sm))
                            {
                                throw new ArgumentException($"'{nameof(sendMessage)}' has an invalid value");
                            }

                            if (a == "sync")
                            {
                                if (w == "notifications")
                                {
                                    var notifications = new ObservableCollection <Octokit.Notification>();
                                    if (l == "online")
                                    {
                                        var filters = f.Split(',');
                                        bool isAll  = false, isParticipating = false, isUnread = true;
                                        if (filter != null && filters.Length > 0)
                                        {
                                            isAll           = filters.Contains("all", StringComparer.OrdinalIgnoreCase);
                                            isParticipating = filters.Contains("participating", StringComparer.OrdinalIgnoreCase);
                                            isUnread        = filters.Contains("unread", StringComparer.OrdinalIgnoreCase);
                                        }
                                        notifications = await NotificationsService.GetAllNotificationsForCurrentUser(isAll, isParticipating);

                                        if (t == "toast")
                                        {
                                            if (sm)
                                            {
                                                if (isAll)
                                                {
                                                    NotificationsViewmodel.AllNotifications = notifications;
                                                    SendMessage(new UpdateAllNotificationsCountMessageType {
                                                        Count = notifications?.Count ?? 0
                                                    });
                                                }
                                                else if (isParticipating)
                                                {
                                                    NotificationsViewmodel.ParticipatingNotifications = notifications;
                                                    SendMessage(new UpdateParticipatingNotificationsCountMessageType {
                                                        Count = notifications?.Count ?? 0
                                                    });
                                                }
                                                else if (isUnread)
                                                {
                                                    AppViewmodel.UnreadNotifications = notifications;
                                                    SendMessage(new UpdateUnreadNotificationsCountMessageType {
                                                        Count = notifications?.Count ?? 0
                                                    });
                                                }
                                            }
                                            if (SettingsService.Get <bool>(SettingsKeys.IsToastEnabled))
                                            {
                                                await AppViewmodel.UnreadNotifications?.ShowToasts();
                                            }
                                        }
                                        else if (t == "tiles")
                                        {
                                            var tile = await notifications[0].BuildTiles();
                                            TileUpdateManager
                                            .CreateTileUpdaterForApplication()
                                            .Update(tile);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ToastHelper.ShowMessage(ex.Message, ex.ToString());
                        }
                    });
                }, ExExecSession_Revoked, _AppTriggerDeferral);

                break;

            case "SyncNotifications":
                _SyncDeferral = taskInstance.GetDeferral();
                await _ExExecSession.RunActionAsExtentedAction(() =>
                {
                    ExecutionService.RunActionInUiThread(async() =>
                    {
                        await BackgroundTaskService.LoadUnreadNotifications(true);
                    });
                }, ExExecSession_Revoked, _SyncDeferral);

                break;

            case "ToastNotificationAction":
                _ToastDeferral = taskInstance.GetDeferral();
                if (!(triggerDetails is ToastNotificationActionTriggerDetail toastTriggerDetails))
                {
                    throw new ArgumentException();
                }
                await _ExExecSession.RunActionAsExtentedAction(() =>
                {
                    ExecutionService.RunActionInUiThread(async() =>
                    {
                        try
                        {
                            var toastArgs      = QueryString.Parse(toastTriggerDetails.Argument);
                            var notificationId = toastArgs["notificationId"] as string;
                            await NotificationsService.MarkNotificationAsRead(notificationId);
                            await BackgroundTaskService.LoadUnreadNotifications(true);
                        }
                        catch (Exception ex)
                        {
                            ToastHelper.ShowMessage(ex.Message, ex.ToString());
                        }
                    });
                }, ExExecSession_Revoked, _ToastDeferral);

                break;

                //case "ToastNotificationChangedTask":
                //var toastChangedTriggerDetails = taskInstance.TriggerDetails as ToastNotificationHistoryChangedTriggerDetail;
                //var collectionId = toastChangedTriggerDetails.CollectionId;
                //var changedType = toastChangedTriggerDetails.ChangeType;
                //if (changedType == ToastHistoryChangedType.Removed)
                //{

                //}
                //break;
            }
        }