mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
add wallpaper api
This commit is contained in:
@@ -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";
|
||||
|
||||
|
||||
@@ -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<BackdropType>(v), BackdropType.Mica).Value;
|
||||
set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, value => value.ToStringOrEmpty());
|
||||
get => GetOption(ref backdropType, SettingEntry.SystemBackdropType, EnumParse<BackdropType>, BackdropType.Mica).Value;
|
||||
set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, EnumToStringOrEmpty);
|
||||
}
|
||||
|
||||
public BackgroundImageType BackgroundImageType
|
||||
{
|
||||
get => GetOption(ref backgroundImageType, SettingEntry.BackgroundImageType, EnumParse<BackgroundImageType>, BackgroundImageType.HutaoOfficialLauncher).Value;
|
||||
set => SetOption(ref backgroundImageType, SettingEntry.BackgroundImageType, value, EnumToStringOrEmpty);
|
||||
}
|
||||
|
||||
public Lazy<List<NameValue<Region>>> 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<T>(string input)
|
||||
where T : struct, Enum
|
||||
{
|
||||
return Enum.Parse<T>(input);
|
||||
}
|
||||
|
||||
private static string EnumToStringOrEmpty<T>(T? input)
|
||||
where T : struct, Enum
|
||||
{
|
||||
return input.ToStringOrEmpty();
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
|
||||
@@ -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<HashSet<string>> SkipOrInitBackgroundAsync()
|
||||
{
|
||||
if (backgroundPathSet is null || backgroundPathSet.Count <= 0)
|
||||
if (backgroundPathSet is not { Count: > 0 })
|
||||
{
|
||||
string backgroundFolder = runtimeOptions.GetDataFolderBackgroundFolder();
|
||||
Directory.CreateDirectory(backgroundFolder);
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
using Snap.Hutao.Model.Entity;
|
||||
|
||||
namespace Snap.Hutao.Service.Inventroy;
|
||||
namespace Snap.Hutao.Service.Inventory;
|
||||
|
||||
internal interface IInventoryDbService
|
||||
{
|
||||
@@ -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
|
||||
{
|
||||
@@ -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))]
|
||||
@@ -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
|
||||
@@ -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<HutaoWallpaperClient> logger;
|
||||
private readonly HttpClient httpClient;
|
||||
|
||||
public ValueTask<Response<Wallpaper>> GetBingWallpaperAsync(CancellationToken token = default)
|
||||
{
|
||||
return GetWallpaperAsync(HutaoEndpoints.WallpaperBing, token);
|
||||
}
|
||||
|
||||
public ValueTask<Response<Wallpaper>> GetLauncherWallpaperAsync(CancellationToken token = default)
|
||||
{
|
||||
return GetWallpaperAsync(HutaoEndpoints.WallpaperGenshinLauncher, token);
|
||||
}
|
||||
|
||||
public ValueTask<Response<Wallpaper>> GetTodayWallpaperAsync(CancellationToken token = default)
|
||||
{
|
||||
return GetWallpaperAsync(HutaoEndpoints.WallpaperToday, token);
|
||||
}
|
||||
|
||||
private async ValueTask<Response<Wallpaper>> GetWallpaperAsync(string url, CancellationToken token = default)
|
||||
{
|
||||
HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create()
|
||||
.SetRequestUri(url)
|
||||
.Get();
|
||||
|
||||
Response<Wallpaper>? resp = await builder.TryCatchSendAsync<Response<Wallpaper>>(httpClient, logger, token).ConfigureAwait(false);
|
||||
return Web.Response.Response.DefaultIfNull(resp);
|
||||
}
|
||||
}
|
||||
19
src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/Wallpaper.cs
Normal file
19
src/Snap.Hutao/Snap.Hutao/Web/Hutao/Wallpaper/Wallpaper.cs
Normal file
@@ -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!;
|
||||
}
|
||||
@@ -10,8 +10,9 @@ namespace Snap.Hutao.Web;
|
||||
/// 胡桃 API 端点
|
||||
/// </summary>
|
||||
[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";
|
||||
|
||||
Reference in New Issue
Block a user