From 48875195bf9f965ccac2df67a0b35abc99c3336c Mon Sep 17 00:00:00 2001 From: Lightczx <1686188646@qq.com> Date: Tue, 20 Feb 2024 14:25:33 +0800 Subject: [PATCH] add wallpaper api --- .../Model/Entity/SettingEntry.Constant.cs | 1 + .../Snap.Hutao/Service/AppOptions.cs | 24 +++++++++- .../BackgroundImage/BackgroundImage.cs | 2 + .../BackgroundImage/BackgroundImageService.cs | 6 +-- .../BackgroundImage/BackgroundImageType.cs | 13 ++++++ .../Service/Cultivation/CultivationService.cs | 2 +- .../IInventoryDbService.cs | 2 +- .../IInventoryService.cs | 2 +- .../InventoryDbService.cs | 2 +- .../InventoryService.cs | 2 +- .../Hutao/Wallpaper/HutaoWallpaperClient.cs | 44 +++++++++++++++++++ .../Web/Hutao/Wallpaper/Wallpaper.cs | 19 ++++++++ .../Snap.Hutao/Web/HutaoEndpoints.cs | 12 ++++- 13 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageType.cs rename src/Snap.Hutao/Snap.Hutao/Service/{Inventroy => Inventory}/IInventoryDbService.cs (91%) rename src/Snap.Hutao/Snap.Hutao/Service/{Inventroy => Inventory}/IInventoryService.cs (76%) rename src/Snap.Hutao/Snap.Hutao/Service/{Inventroy => Inventory}/InventoryDbService.cs (98%) rename src/Snap.Hutao/Snap.Hutao/Service/{Inventroy => Inventory}/InventoryService.cs (82%) create mode 100644 src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/HutaoWallpaperClient.cs create mode 100644 src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/Wallpaper.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs index f22ffdf9..9b70a6db 100644 --- a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs +++ b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs @@ -13,6 +13,7 @@ internal sealed partial class SettingEntry public const string Culture = "Culture"; public const string SystemBackdropType = "SystemBackdropType"; + public const string BackgroundImageType = "BackgroundImageType"; public const string AnnouncementRegion = "AnnouncementRegion"; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs index cdccd19b..1a13e560 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs @@ -5,6 +5,7 @@ using Snap.Hutao.Core.Windowing; using Snap.Hutao.Model; using Snap.Hutao.Model.Entity; using Snap.Hutao.Service.Abstraction; +using Snap.Hutao.Service.BackgroundImage; using Snap.Hutao.Web.Hoyolab; namespace Snap.Hutao.Service; @@ -15,6 +16,7 @@ internal sealed partial class AppOptions : DbStoreOptions { private bool? isEmptyHistoryWishVisible; private BackdropType? backdropType; + private BackgroundImageType? backgroundImageType; private Region? region; private string? geetestCustomCompositeUrl; @@ -28,8 +30,14 @@ internal sealed partial class AppOptions : DbStoreOptions public BackdropType BackdropType { - get => GetOption(ref backdropType, SettingEntry.SystemBackdropType, v => Enum.Parse(v), BackdropType.Mica).Value; - set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, value => value.ToStringOrEmpty()); + get => GetOption(ref backdropType, SettingEntry.SystemBackdropType, EnumParse, BackdropType.Mica).Value; + set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, EnumToStringOrEmpty); + } + + public BackgroundImageType BackgroundImageType + { + get => GetOption(ref backgroundImageType, SettingEntry.BackgroundImageType, EnumParse, BackgroundImageType.HutaoOfficialLauncher).Value; + set => SetOption(ref backgroundImageType, SettingEntry.BackgroundImageType, value, EnumToStringOrEmpty); } public Lazy>> LazyRegions { get; } = new(KnownRegions.Get); @@ -45,4 +53,16 @@ internal sealed partial class AppOptions : DbStoreOptions get => GetOption(ref geetestCustomCompositeUrl, SettingEntry.GeetestCustomCompositeUrl); set => SetOption(ref geetestCustomCompositeUrl, SettingEntry.GeetestCustomCompositeUrl, value); } + + private static T? EnumParse(string input) + where T : struct, Enum + { + return Enum.Parse(input); + } + + private static string EnumToStringOrEmpty(T? input) + where T : struct, Enum + { + return input.ToStringOrEmpty(); + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs index e2e50aef..03f60135 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs @@ -13,6 +13,8 @@ namespace Snap.Hutao.Service.BackgroundImage; internal sealed class BackgroundImage { + public string Path { get; set; } = default!; + public BitmapImage ImageSource { get; set; } = default!; public Color AccentColor { get; set; } diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs index 82aa04ba..6217450a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs @@ -38,8 +38,7 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService string path = System.Random.Shared.GetItems(backgroundSet.ToArray(), 1)[0]; backgroundSet.Remove(path); - await taskContext.SwitchToMainThreadAsync(); - if (string.Equals(path, previous?.ImageSource.UriSource.ToString(), StringComparison.OrdinalIgnoreCase)) + if (string.Equals(path, previous?.Path, StringComparison.OrdinalIgnoreCase)) { return new(false, default!); } @@ -54,6 +53,7 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService BackgroundImage background = new() { + Path = path, ImageSource = new(path.ToUri()), AccentColor = accentColor, Luminance = accentColor.Luminance, @@ -65,7 +65,7 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService private async ValueTask> SkipOrInitBackgroundAsync() { - if (backgroundPathSet is null || backgroundPathSet.Count <= 0) + if (backgroundPathSet is not { Count: > 0 }) { string backgroundFolder = runtimeOptions.GetDataFolderBackgroundFolder(); Directory.CreateDirectory(backgroundFolder); diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageType.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageType.cs new file mode 100644 index 00000000..4eee1187 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageType.cs @@ -0,0 +1,13 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +namespace Snap.Hutao.Service.BackgroundImage; + +internal enum BackgroundImageType +{ + None, + LocalFolder, + HutaoBing, + HutaoDaily, + HutaoOfficialLauncher, +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Cultivation/CultivationService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Cultivation/CultivationService.cs index 7491b43d..b8d72295 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Cultivation/CultivationService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Cultivation/CultivationService.cs @@ -7,7 +7,7 @@ using Snap.Hutao.Model.Entity; using Snap.Hutao.Model.Entity.Database; using Snap.Hutao.Model.Entity.Primitive; using Snap.Hutao.Model.Metadata.Item; -using Snap.Hutao.Service.Inventroy; +using Snap.Hutao.Service.Inventory; using Snap.Hutao.Service.Metadata.ContextAbstraction; using Snap.Hutao.ViewModel.Cultivation; using System.Collections.ObjectModel; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/IInventoryDbService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/IInventoryDbService.cs similarity index 91% rename from src/Snap.Hutao/Snap.Hutao/Service/Inventroy/IInventoryDbService.cs rename to src/Snap.Hutao/Snap.Hutao/Service/Inventory/IInventoryDbService.cs index 74ed4288..14430ec9 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/IInventoryDbService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/IInventoryDbService.cs @@ -3,7 +3,7 @@ using Snap.Hutao.Model.Entity; -namespace Snap.Hutao.Service.Inventroy; +namespace Snap.Hutao.Service.Inventory; internal interface IInventoryDbService { diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/IInventoryService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/IInventoryService.cs similarity index 76% rename from src/Snap.Hutao/Snap.Hutao/Service/Inventroy/IInventoryService.cs rename to src/Snap.Hutao/Snap.Hutao/Service/Inventory/IInventoryService.cs index be6fdd89..4eb6ef0d 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/IInventoryService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/IInventoryService.cs @@ -1,7 +1,7 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. -namespace Snap.Hutao.Service.Inventroy; +namespace Snap.Hutao.Service.Inventory; internal interface IInventoryService { diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/InventoryDbService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/InventoryDbService.cs similarity index 98% rename from src/Snap.Hutao/Snap.Hutao/Service/Inventroy/InventoryDbService.cs rename to src/Snap.Hutao/Snap.Hutao/Service/Inventory/InventoryDbService.cs index 0e7b07f1..bf34de1b 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/InventoryDbService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/InventoryDbService.cs @@ -6,7 +6,7 @@ using Snap.Hutao.Core.Database; using Snap.Hutao.Model.Entity; using Snap.Hutao.Model.Entity.Database; -namespace Snap.Hutao.Service.Inventroy; +namespace Snap.Hutao.Service.Inventory; [ConstructorGenerated] [Injection(InjectAs.Singleton, typeof(IInventoryDbService))] diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/InventoryService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/InventoryService.cs similarity index 82% rename from src/Snap.Hutao/Snap.Hutao/Service/Inventroy/InventoryService.cs rename to src/Snap.Hutao/Snap.Hutao/Service/Inventory/InventoryService.cs index 7904038d..88928948 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Inventroy/InventoryService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Inventory/InventoryService.cs @@ -1,7 +1,7 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. -namespace Snap.Hutao.Service.Inventroy; +namespace Snap.Hutao.Service.Inventory; [Injection(InjectAs.Transient)] internal sealed class InventoryService : IInventoryService diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/HutaoWallpaperClient.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/HutaoWallpaperClient.cs new file mode 100644 index 00000000..fe9229c0 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/HutaoWallpaperClient.cs @@ -0,0 +1,44 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Snap.Hutao.Core.DependencyInjection.Annotation.HttpClient; +using Snap.Hutao.Web.Request.Builder; +using Snap.Hutao.Web.Request.Builder.Abstraction; +using Snap.Hutao.Web.Response; +using System.Net.Http; + +namespace Snap.Hutao.Web.Hutao.Wallpaper; + +[HttpClient(HttpClientConfiguration.Default)] +[ConstructorGenerated(ResolveHttpClient = true)] +internal sealed partial class HutaoWallpaperClient +{ + private readonly IHttpRequestMessageBuilderFactory httpRequestMessageBuilderFactory; + private readonly ILogger logger; + private readonly HttpClient httpClient; + + public ValueTask> GetBingWallpaperAsync(CancellationToken token = default) + { + return GetWallpaperAsync(HutaoEndpoints.WallpaperBing, token); + } + + public ValueTask> GetLauncherWallpaperAsync(CancellationToken token = default) + { + return GetWallpaperAsync(HutaoEndpoints.WallpaperGenshinLauncher, token); + } + + public ValueTask> GetTodayWallpaperAsync(CancellationToken token = default) + { + return GetWallpaperAsync(HutaoEndpoints.WallpaperToday, token); + } + + private async ValueTask> GetWallpaperAsync(string url, CancellationToken token = default) + { + HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create() + .SetRequestUri(url) + .Get(); + + Response? resp = await builder.TryCatchSendAsync>(httpClient, logger, token).ConfigureAwait(false); + return Web.Response.Response.DefaultIfNull(resp); + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/Wallpaper.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/Wallpaper.cs new file mode 100644 index 00000000..4ec26f2e --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/Wallpaper.cs @@ -0,0 +1,19 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +namespace Snap.Hutao.Web.Hutao.Wallpaper; + +internal sealed class Wallpaper +{ + [JsonPropertyName("url")] + public string Url { get; set; } = default!; + + [JsonPropertyName("source_url")] + public string SourceUrl { get; set; } = default!; + + [JsonPropertyName("author")] + public string Author { get; set; } = default!; + + [JsonPropertyName("uploader")] + public string Uploader { get; set; } = default!; +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Web/HutaoEndpoints.cs b/src/Snap.Hutao/Snap.Hutao/Web/HutaoEndpoints.cs index 9d707aae..2d1847b3 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/HutaoEndpoints.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/HutaoEndpoints.cs @@ -10,8 +10,9 @@ namespace Snap.Hutao.Web; /// 胡桃 API 端点 /// [HighQuality] -[SuppressMessage("", "SA1201")] [SuppressMessage("", "SA1124")] +[SuppressMessage("", "SA1201")] +[SuppressMessage("", "SA1203")] internal static class HutaoEndpoints { #region HomaAPI @@ -271,6 +272,15 @@ internal static class HutaoEndpoints } #endregion + #region Wallpaper + + public const string WallpaperBing = $"{ApiSnapGenshin}/wallpaper/bing"; + + public const string WallpaperGenshinLauncher = $"{ApiSnapGenshin}/wallpaper/genshin-launcher"; + + public const string WallpaperToday = $"{ApiSnapGenshin}/wallpaper/today"; + #endregion + #endregion private const string ApiSnapGenshin = "https://api.snapgenshin.com";