mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
refactor infobarservice
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder;
|
||||
namespace Snap.Hutao.Core.Abstraction.Extension;
|
||||
|
||||
internal static class BuilderExtension
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
namespace Snap.Hutao.Core.Abstraction;
|
||||
|
||||
internal interface IBuilder;
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Service.Notification;
|
||||
|
||||
internal interface IInfoBarOptionsBuilder : IBuilder
|
||||
{
|
||||
public InfoBarOptions Options { get; }
|
||||
}
|
||||
@@ -11,5 +11,5 @@ internal interface IInfoBarService
|
||||
{
|
||||
ObservableCollection<InfoBar> Collection { get; }
|
||||
|
||||
void PrepareInfoBarAndShow(InfoBarSeverity severity, string? title, string? message, int delay);
|
||||
void PrepareInfoBarAndShow(Action<IInfoBarOptionsBuilder> configure);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media.Animation;
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
using System.Collections.ObjectModel;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Snap.Hutao.Service.Notification;
|
||||
|
||||
internal sealed class InfoBarOptions
|
||||
{
|
||||
public InfoBarSeverity Severity { get; set; }
|
||||
|
||||
public string? Title { get; set; }
|
||||
|
||||
public string? Message { get; set; }
|
||||
|
||||
public object? Content { get; set; }
|
||||
|
||||
public int MilliSecondsDelay { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media.Animation;
|
||||
using System.Collections.ObjectModel;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Snap.Hutao.Service.Notification;
|
||||
|
||||
internal sealed class InfoBarOptionsBuilder : IInfoBarOptionsBuilder
|
||||
{
|
||||
public InfoBarOptions Options { get; } = new();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
|
||||
namespace Snap.Hutao.Service.Notification;
|
||||
|
||||
internal static class InfoBarOptionsBuilderExtension
|
||||
{
|
||||
public static IInfoBarOptionsBuilder SetSeverity(this IInfoBarOptionsBuilder builder, InfoBarSeverity severity)
|
||||
{
|
||||
builder.Configure(builder => builder.Options.Severity = severity);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IInfoBarOptionsBuilder SetTitle(this IInfoBarOptionsBuilder builder, string? title)
|
||||
{
|
||||
builder.Configure(builder => builder.Options.Title = title);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IInfoBarOptionsBuilder SetMessage(this IInfoBarOptionsBuilder builder, string? message)
|
||||
{
|
||||
builder.Configure(builder => builder.Options.Message = message);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IInfoBarOptionsBuilder SetContent(this IInfoBarOptionsBuilder builder, object? content)
|
||||
{
|
||||
builder.Configure(builder => builder.Options.Content = content);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IInfoBarOptionsBuilder SetDelay(this IInfoBarOptionsBuilder builder, int milliSeconds)
|
||||
{
|
||||
builder.Configure(builder => builder.Options.MilliSecondsDelay = milliSeconds);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media.Animation;
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
using System.Collections.ObjectModel;
|
||||
using Windows.Foundation;
|
||||
|
||||
@@ -34,25 +35,28 @@ internal sealed class InfoBarService : IInfoBarService
|
||||
get => collection ??= [];
|
||||
}
|
||||
|
||||
public void PrepareInfoBarAndShow(InfoBarSeverity severity, string? title, string? message, int delay)
|
||||
public void PrepareInfoBarAndShow(Action<IInfoBarOptionsBuilder> configure)
|
||||
{
|
||||
if (collection is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PrepareInfoBarAndShowCoreAsync(severity, title, message, delay).SafeForget(logger);
|
||||
PrepareInfoBarAndShowCoreAsync(configure).SafeForget(logger);
|
||||
}
|
||||
|
||||
private async ValueTask PrepareInfoBarAndShowCoreAsync(InfoBarSeverity severity, string? title, string? message, int delay)
|
||||
private async ValueTask PrepareInfoBarAndShowCoreAsync(Action<IInfoBarOptionsBuilder> configure)
|
||||
{
|
||||
IInfoBarOptionsBuilder builder = new InfoBarOptionsBuilder().Configure(configure);
|
||||
|
||||
await taskContext.SwitchToMainThreadAsync();
|
||||
|
||||
InfoBar infoBar = new()
|
||||
{
|
||||
Severity = severity,
|
||||
Title = title,
|
||||
Message = message,
|
||||
Severity = builder.Options.Severity,
|
||||
Title = builder.Options.Title,
|
||||
Message = builder.Options.Message,
|
||||
Content = builder.Options.Content,
|
||||
IsOpen = true,
|
||||
Transitions = [new AddDeleteThemeTransition()],
|
||||
};
|
||||
@@ -61,9 +65,9 @@ internal sealed class InfoBarService : IInfoBarService
|
||||
ArgumentNullException.ThrowIfNull(collection);
|
||||
collection.Add(infoBar);
|
||||
|
||||
if (delay > 0)
|
||||
if (builder.Options.MilliSecondsDelay > 0)
|
||||
{
|
||||
await Delay.FromMilliSeconds(delay).ConfigureAwait(true);
|
||||
await Delay.FromMilliSeconds(builder.Options.MilliSecondsDelay).ConfigureAwait(true);
|
||||
collection.Remove(infoBar);
|
||||
infoBar.IsOpen = false;
|
||||
}
|
||||
|
||||
@@ -2,58 +2,79 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
|
||||
namespace Snap.Hutao.Service.Notification;
|
||||
|
||||
internal static class InfoBarServiceExtension
|
||||
{
|
||||
public static void Information(this IInfoBarService infoBarService, string message, int delay = 5000)
|
||||
public static void Information(this IInfoBarService infoBarService, string message, int milliSeconds = 5000)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Informational, null, message, delay);
|
||||
infoBarService.Information(builder => builder.SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Information(this IInfoBarService infoBarService, string title, string message, int delay = 5000)
|
||||
public static void Information(this IInfoBarService infoBarService, string title, string message, int milliSeconds = 5000)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Informational, title, message, delay);
|
||||
infoBarService.Information(builder => builder.SetTitle(title).SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Success(this IInfoBarService infoBarService, string message, int delay = 5000)
|
||||
public static void Information(this IInfoBarService infoBarService, Action<IInfoBarOptionsBuilder> configure)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Success, null, message, delay);
|
||||
infoBarService.PrepareInfoBarAndShow(builder => builder.SetSeverity(InfoBarSeverity.Informational).Configure(configure));
|
||||
}
|
||||
|
||||
public static void Success(this IInfoBarService infoBarService, string title, string message, int delay = 5000)
|
||||
public static void Success(this IInfoBarService infoBarService, string message, int milliSeconds = 5000)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Success, title, message, delay);
|
||||
infoBarService.Success(builder => builder.SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Warning(this IInfoBarService infoBarService, string message, int delay = 30000)
|
||||
public static void Success(this IInfoBarService infoBarService, string title, string message, int milliSeconds = 5000)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Warning, null, message, delay);
|
||||
infoBarService.Success(builder => builder.SetTitle(title).SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Warning(this IInfoBarService infoBarService, string title, string message, int delay = 30000)
|
||||
public static void Success(this IInfoBarService infoBarService, Action<IInfoBarOptionsBuilder> configure)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Warning, title, message, delay);
|
||||
infoBarService.PrepareInfoBarAndShow(builder => builder.SetSeverity(InfoBarSeverity.Success).Configure(configure));
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, string message, int delay = 0)
|
||||
public static void Warning(this IInfoBarService infoBarService, string message, int milliSeconds = 30000)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Error, null, message, delay);
|
||||
infoBarService.Warning(builder => builder.SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, string title, string message, int delay = 0)
|
||||
public static void Warning(this IInfoBarService infoBarService, string title, string message, int milliSeconds = 30000)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Error, title, message, delay);
|
||||
infoBarService.Warning(builder => builder.SetTitle(title).SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, Exception ex, int delay = 0)
|
||||
public static void Warning(this IInfoBarService infoBarService, Action<IInfoBarOptionsBuilder> configure)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Error, ex.GetType().Name, ex.Message, delay);
|
||||
infoBarService.PrepareInfoBarAndShow(builder => builder.SetSeverity(InfoBarSeverity.Warning).Configure(configure));
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, Exception ex, string title, int delay = 0)
|
||||
public static void Error(this IInfoBarService infoBarService, string message, int milliSeconds = 0)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(InfoBarSeverity.Error, ex.GetType().Name, $"{title}\n{ex.Message}", delay);
|
||||
infoBarService.Error(builder => builder.SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, string title, string message, int milliSeconds = 0)
|
||||
{
|
||||
infoBarService.Error(builder => builder.SetTitle(title).SetMessage(message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, Exception ex, int milliSeconds = 0)
|
||||
{
|
||||
infoBarService.Error(builder => builder.SetTitle(ex.GetType().Name).SetMessage(ex.Message).SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, Exception ex, string subtitle, int milliSeconds = 0)
|
||||
{
|
||||
infoBarService.Error(builder => builder.SetTitle(ex.GetType().Name).SetMessage($"{subtitle}\n{ex.Message}").SetDelay(milliSeconds));
|
||||
}
|
||||
|
||||
public static void Error(this IInfoBarService infoBarService, Action<IInfoBarOptionsBuilder> configure)
|
||||
{
|
||||
infoBarService.PrepareInfoBarAndShow(builder => builder.SetSeverity(InfoBarSeverity.Error).Configure(configure));
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using System.Net.Http;
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using System.Net.Http.Headers;
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using System.Net.Http;
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
|
||||
internal interface IHttpProtocolVersionBuilder : IBuilder
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using System.Net.Http;
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using System.Net.Http;
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
|
||||
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
|
||||
internal interface IRequestUriBuilder : IBuilder
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
using Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
using Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
using Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction;
|
||||
using Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Core.Abstraction.Extension;
|
||||
using Snap.Hutao.Web.Request.Builder.Abstraction;
|
||||
using System.Diagnostics;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user