public async Task SyncToTimeline()
 {
     if (PlayerData != null)
     {
         IsSyncIdle = false;
         var bungie = new BungieService(Shared.SharedData.BungieApiKey);
         for (int i = 0; i < PlayerData.ActivityHistoryLists.Count; i++)
         {
             await Task.WhenAll(from pgcr in PlayerData.ActivityHistoryLists[i]
                                select Task.Run(async() =>
             {
                 var getActivityDefinition = bungie.GetActivityDefinitionAsync(pgcr.Pgcr.ActivityDetails.ReferenceId);
                 var getModeDefinition     = bungie.GetActivityModeDefinitionAsync(pgcr.Pgcr.ActivityDetails.Mode);
                 var activity = new DestinyUserActivity(pgcr.Pgcr, await getActivityDefinition, await getModeDefinition,
                                                        PlayerData.CharacterIDs[i]);
                 var msGraph = new MsGraphService(Shared.SharedData.MsGraphClientId);
                 await msGraph.CreateOrReplaceActivityAsync(activity.Activity);
             }));
         }
         IsSyncIdle = true;
     }
 }
        private async void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            var bungie = new BungieService(Shared.SharedData.BungieApiKey);

            var adaptiveCardRenderer = new AdaptiveCardRenderer
            {
                HostConfig = new AdaptiveHostConfig
                {
                    FontFamily = "Segoe UI",
                    FontSizes  = new AdaptiveFontSizesConfig
                    {
                        Small      = 12,
                        Default    = 14,
                        Large      = 20,
                        ExtraLarge = 24
                    },
                    FontWeights = new AdaptiveFontWeightsConfig
                    {
                        Lighter = 200,
                        Default = 400,
                        Bolder  = 700
                    },
                    ContainerStyles = new AdaptiveContainerStylesDefinition
                    {
                        Default = new AdaptiveContainerStyleDefinition
                        {
                            BackgroundColor  = Color.FromArgb(255, 83, 84, 84),
                            ForegroundColors = new AdaptiveColorsConfig
                            {
                                Default = new AdaptiveColorConfig
                                {
                                    Default = Color.FromArgb(255, 255, 255, 255),
                                    Subtle  = Color.FromArgb(255, 156, 158, 159)
                                },
                                Attention = new AdaptiveColorConfig
                                {
                                    Default = Color.FromArgb(255, 255, 0, 0),
                                    Subtle  = Color.FromArgb(221, 255, 0, 0)
                                },
                                Good = new AdaptiveColorConfig
                                {
                                    Default = Color.FromArgb(255, 0, 255, 0),
                                    Subtle  = Color.FromArgb(221, 0, 255, 0)
                                }
                            }
                        }
                    },
                    ImageSizes = new AdaptiveImageSizesConfig
                    {
                        Small  = 40,
                        Medium = 80,
                        Large  = 120
                    },
                    ImageSet = new AdaptiveImageSetConfig
                    {
                        ImageSize      = ImageSize.Medium,
                        MaxImageHeight = 100
                    }
                }
            };

            var getActivityDefinition = bungie.GetActivityDefinitionAsync(Pgcr.ActivityDetails.ReferenceId);
            var getModeDefinition     = bungie.GetActivityModeDefinitionAsync(Pgcr.ActivityDetails.Mode);

            var activity     = new DestinyUserActivity(Pgcr, await getActivityDefinition, await getModeDefinition, CharacterId);
            var renderedCard = adaptiveCardRenderer.RenderAdaptiveCardFromJsonString(activity.Activity.VisualElements.Content.ToJson());

            if (renderedCard.FrameworkElement != null)
            {
                Grid uiCard = renderedCard.FrameworkElement as Grid;
                uiCard.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                var background = uiCard.Children.OfType <Image>().FirstOrDefault();
                if (background != null)
                {
                    background.Opacity = 0.5;
                }
                uiCard.Width     = 316;
                uiCard.Height    = 174;
                uiCard.Margin    = new Thickness(0, 0, 0, 24);
                LayoutRoot.Child = uiCard;
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            deferral = taskInstance.GetDeferral();

            try
            {
                var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();

                if (NetworkInterface.GetIsNetworkAvailable() &&
                    (connectionCost.NetworkCostType == NetworkCostType.Unknown ||
                     connectionCost.NetworkCostType == NetworkCostType.Unrestricted))
                {
                    ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

                    var player = JsonConvert.DeserializeObject <DestinyPlayerInformation>((string)localSettings.Values["player"]);

                    int    platform  = player.MembershipType;
                    string accountId = player.MembershipID;
                    int    mode      = 5;

                    var bungie = new BungieService(SharedData.BungieApiKey);
                    await bungie.DownloadDestinyManifest();

                    foreach (var characterId in player.CharacterIDs)
                    {
                        var history = await bungie.GetActivityHistory(platform, accountId, characterId, mode);

                        await Task.WhenAll(from pgcr in history
                                           select Task.Run(async() =>
                        {
                            var getActivityDefinition = bungie.GetActivityDefinitionAsync(pgcr.ActivityDetails.ReferenceId);
                            var getModeDefinition     = bungie.GetActivityModeDefinitionAsync(pgcr.ActivityDetails.Mode);

                            var activity = new DestinyUserActivity(pgcr, await getActivityDefinition, await getModeDefinition, characterId);

                            var msGraph = new MsGraphService(SharedData.MsGraphClientId);
                            await msGraph.CreateOrReplaceActivityAsync(activity.Activity);
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                ToastContent toastContent = new ToastContent()
                {
                    Launch = "refresh-activity",
                    Visual = new ToastVisual()
                    {
                        BindingGeneric = new ToastBindingGeneric()
                        {
                            Children =
                            {
                                new AdaptiveText
                                {
                                    Text = "An error occured while refreshing Destiny 2 activity history"
                                }
                            }
                        }
                    }
                };

                var toast = new ToastNotification(toastContent.GetXml());
                toast.ExpirationTime = DateTime.Now.AddDays(2);
                ToastNotificationManager.CreateToastNotifier().Show(toast);
            }

            deferral.Complete();
        }