From 386a5acf590f61be2f4e48d031b306a2e9a4c608 Mon Sep 17 00:00:00 2001 From: Kalakoi Date: Wed, 21 May 2025 16:08:09 -0400 Subject: [PATCH] Changed update function to run asynchronously to avoid blocking GUI updates. --- .../ViewModels/CompetitionOverlayViewModel.cs | 119 +++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/osrs-toolbox/ViewModels/CompetitionOverlayViewModel.cs b/osrs-toolbox/ViewModels/CompetitionOverlayViewModel.cs index 2dbf367..4a9f193 100644 --- a/osrs-toolbox/ViewModels/CompetitionOverlayViewModel.cs +++ b/osrs-toolbox/ViewModels/CompetitionOverlayViewModel.cs @@ -38,7 +38,7 @@ namespace osrs_toolbox { DispatcherTimer dt = new DispatcherTimer(); dt.Interval = TimeSpan.FromSeconds(5); - dt.Tick += new EventHandler(TimerTick); + dt.Tick += new EventHandler(TimerTickAsync); dt.Start(); } @@ -47,6 +47,11 @@ namespace osrs_toolbox DoUpdate(null); } + private async void TimerTickAsync(object sender, EventArgs e) + { + await DoUpdateAsync(null); + } + private void DoUpdate(object obj) { GridOutput = new StackPanel(); @@ -159,6 +164,118 @@ namespace osrs_toolbox OnPropertyChanged(nameof(GridOutput)); } + private async Task DoUpdateAsync(object obj) + { + GridOutput = new StackPanel(); + Competition c = new Competition(); + try + { + c = await WiseOldMan.GetCompetitionAsync(CompetitionID).ConfigureAwait(false); + } + catch + { + GridOutput.Children.Add(new OutlinedTextBlock() + { + Text = "Failed to load competition data", + Margin = new Thickness(3), + StrokeThickness = 1, + Stroke = Brushes.Black, + Fill = Brushes.White, + FontSize = 20, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Left, + FontWeight = FontWeights.ExtraBold, + TextWrapping = TextWrapping.Wrap + }); + OnPropertyChanged(nameof(GridOutput)); + return; + } + + int KCSum = 0; + string TempOut = c.title; + GridOutput.Children.Add(new OutlinedTextBlock() + { + Text = c.title, + Margin = new Thickness(3), + StrokeThickness = 1, + Stroke = Brushes.Black, + Fill = Brushes.White, + FontSize = 20, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Left, + FontWeight = FontWeights.ExtraBold, + TextWrapping = TextWrapping.Wrap + }); + foreach (CompetitionParticipation cp in c.participations) + { + StackPanel SubStack = new StackPanel() + { + Orientation = Orientation.Horizontal + }; + + Image typeImage = new Image(); + if (cp.player.type == "ironman") + typeImage.Source = ExternalResources.IronImage; + else typeImage.Source = ExternalResources.MainImage; + typeImage.IsHitTestVisible = false; + + SubStack.Children.Add(typeImage); + + SubStack.Children.Add(new OutlinedTextBlock() + { + Text = cp.player.displayName, + Margin = new Thickness(3), + StrokeThickness = 1, + Stroke = Brushes.Black, + Fill = PlayerName.ToUpper() == cp.player.displayName.ToUpper() ? Brushes.Green : Brushes.White, + FontSize = 20, + FontWeight = FontWeights.ExtraBold, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Left, + IsHitTestVisible = false + }); + + SubStack.Children.Add(new OutlinedTextBlock() + { + Text = string.Format(" - {0} KC", cp.progress.gained.ToString()), + Margin = new Thickness(3), + StrokeThickness = 1, + Stroke = Brushes.Black, + Fill = Brushes.White, + FontSize = 20, + FontWeight = FontWeights.ExtraBold, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Left, + IsHitTestVisible = false + }); + + KCSum += cp.progress.gained; + + bool AddToDisplay = true; + if (HideOtherPlayers && PlayerName.ToUpper() != cp.player.displayName.ToUpper()) + AddToDisplay = false; + else if (HideZeroKC && cp.progress.gained == 0 && PlayerName.ToUpper() != cp.player.displayName.ToUpper()) + AddToDisplay = false; + + if (AddToDisplay) + GridOutput.Children.Add(SubStack); + } + GridOutput.Children.Add(new OutlinedTextBlock() + { + Text = string.Format("Total KC: {0}", KCSum), + Margin = new Thickness(3), + StrokeThickness = 1, + Stroke = Brushes.Black, + Fill = Brushes.White, + FontSize = 20, + FontWeight = FontWeights.ExtraBold, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Left, + IsHitTestVisible = false + }); + OnPropertyChanged(nameof(GridOutput)); + } + private void DoCloseApp(object obj) { Application.Current.Shutdown();