11using Daybreak . Shared . Models . ColorPalette ;
22using Daybreak . Shared . Models . Themes ;
33using Daybreak . Shared . Services . Screenshots ;
4+ using Microsoft . Extensions . Logging ;
5+ using System . Extensions . Core ;
46using System . Windows . Extensions . Services ;
57
68namespace Daybreak . Shared . Services . Themes ;
@@ -11,13 +13,15 @@ namespace Daybreak.Shared.Services.Themes;
1113/// </summary>
1214public sealed class GameScreenshotsTheme (
1315 IThemeManager themeManager ,
14- IScreenshotService screenshotService )
16+ IScreenshotService screenshotService ,
17+ ILogger < GameScreenshotsTheme > logger )
1518 : Theme ( ThemeName , AccentColor . Orange , new StaticBackground ( string . Empty ) , LightDarkMode . SystemSynchronized , string . Empty ) , IApplicationLifetimeService
1619{
1720 public const string ThemeName = "Dynamic Screenshots" ;
1821
1922 private readonly IScreenshotService screenshotService = screenshotService ;
2023 private readonly IThemeManager themeManager = themeManager ;
24+ private readonly ILogger < GameScreenshotsTheme > logger = logger ;
2125
2226 private CancellationTokenSource ? cancellationTokenSource ;
2327
@@ -27,27 +31,35 @@ private async Task PeriodicallyUpdateGameScreenshots(CancellationToken cancellat
2731 * The theme needs to always cycle through entries, so that when the user selects this theme, it's already initialized.
2832 * We rely on ScreenshotService to cache screenshot entries in memory.
2933 */
34+ var scopedLogger = this . logger . CreateScopedLogger ( ) ;
3035 while ( ! cancellationToken . IsCancellationRequested )
3136 {
32- var entry = await this . screenshotService . GetRandomScreenshot ( cancellationToken ) ;
33- if ( entry is null )
37+ try
3438 {
39+ var entry = await this . screenshotService . GetRandomScreenshot ( cancellationToken ) ;
40+ if ( entry is null )
41+ {
42+ await Task . Delay ( TimeSpan . FromSeconds ( 30 ) , cancellationToken ) ;
43+ continue ;
44+ }
45+
46+ this . AccentColor = entry . AccentColor ;
47+ this . Background = new StaticBackground ( entry . FilePath ) ;
48+ this . Mode = entry . LightDarkMode ;
49+ this . Filter = entry . LightDarkMode is LightDarkMode . Light
50+ ? "blur(3px) brightness(1.3) sepia(0.2) saturate(1.2)"
51+ : "blur(2px) brightness(1.2) hue-rotate(10deg) saturate(1.1)" ;
52+ if ( this == this . themeManager . CurrentTheme )
53+ {
54+ this . themeManager . ReapplyTheme ( ) ;
55+ }
56+
3557 await Task . Delay ( TimeSpan . FromSeconds ( 30 ) , cancellationToken ) ;
36- continue ;
3758 }
38-
39- this . AccentColor = entry . AccentColor ;
40- this . Background = new StaticBackground ( entry . FilePath ) ;
41- this . Mode = entry . LightDarkMode ;
42- this . Filter = entry . LightDarkMode is LightDarkMode . Light
43- ? "blur(3px) brightness(1.3) sepia(0.2) saturate(1.2)"
44- : "blur(2px) brightness(1.2) hue-rotate(10deg) saturate(1.1)" ;
45- if ( this == this . themeManager . CurrentTheme )
59+ catch ( Exception ex )
4660 {
47- this . themeManager . ReapplyTheme ( ) ;
61+ scopedLogger . LogError ( ex , "An error occurred while updating game screenshots theme." ) ;
4862 }
49-
50- await Task . Delay ( TimeSpan . FromSeconds ( 30 ) , cancellationToken ) ;
5163 }
5264 }
5365
0 commit comments