Skip to content

Commit 0f34a51

Browse files
committed
feat: use avalonia toast
1 parent 0a955aa commit 0f34a51

14 files changed

+13
-407
lines changed

Hollow/App.axaml

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<ResourceInclude Source="Views/Controls/HollowHost.axaml" />
2222
<ResourceInclude Source="Views/Controls/Coverages/ProhibitedCoverage.axaml" />
2323
<ResourceInclude Source="Views/Controls/Coverages/LoadingCoverage.axaml" />
24-
<ResourceInclude Source="Views/Controls/Toast/Toast.axaml" />
2524
<ResourceInclude Source="Views/Controls/SignalSearch/SignalSearchOverviewCard.axaml" />
2625
</ResourceDictionary.MergedDictionaries>
2726
</ResourceDictionary>

Hollow/Hollow.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,5 @@
123123
<AdditionalFiles Include="Views\Controls\Home\StartGameNoticeButton.axaml" />
124124
<AdditionalFiles Include="Views\Controls\Screenshots\ScreenshotImage.axaml" />
125125
<AdditionalFiles Include="Views\Controls\SignalSearch\SignalSearchOverviewCard.axaml" />
126-
<AdditionalFiles Include="Views\Controls\Toast\Toast.axaml" />
127126
</ItemGroup>
128127
</Project>

Hollow/ViewModels/MainWindowViewModel.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using CommunityToolkit.Mvvm.ComponentModel;
77
using CommunityToolkit.Mvvm.Input;
88
using Hollow.Abstractions.Models;
9-
using Hollow.Enums;
109
using Hollow.Helpers;
1110
using Hollow.Languages;
1211
using Hollow.Services.MetadataService;

Hollow/ViewModels/Pages/GameSettingsViewModel.cs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using CommunityToolkit.Mvvm.ComponentModel;
55
using CommunityToolkit.Mvvm.Input;
66
using Hollow.Abstractions.Models;
7-
using Hollow.Enums;
87
using Hollow.Languages;
98
using Hollow.Services.ConfigurationService;
109
using Hollow.Services.GameService;

Hollow/ViewModels/Pages/HomeViewModel.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Collections.ObjectModel;
1+
using System.Collections.ObjectModel;
32
using System.Net.Http;
43
using System.Threading.Tasks;
5-
using Avalonia.Controls.Notifications;
64
using CommunityToolkit.Mvvm.ComponentModel;
75
using CommunityToolkit.Mvvm.Input;
86
using Hollow.Abstractions.Models.HttpContrasts.MiHoYoLauncher;
@@ -12,7 +10,6 @@
1210
using Hollow.Services.GameService;
1311
using Hollow.Services.MiHoYoLauncherService;
1412
using Hollow.Services.NavigationService;
15-
using Hollow.Views.Controls;
1613
using Hollow.Views.Pages;
1714
using Serilog;
1815

Hollow/ViewModels/Pages/SignalSearchViewModel.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Hollow.Abstractions.Models;
1414
using Hollow.Abstractions.Models.HttpContrasts.Gacha;
1515
using Hollow.Abstractions.Models.HttpContrasts.Gacha.Uigf;
16-
using Hollow.Enums;
1716
using Hollow.Helpers;
1817
using Hollow.Languages;
1918
using Hollow.Models.SignalSearch;
@@ -185,7 +184,7 @@ private async Task UpdateByImport()
185184
{
186185
if(_metadataService.ItemsMetadata is null)
187186
{
188-
await HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_MetadataNotFound_Message, NotificationType.Error);
187+
HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_MetadataNotFound_Message, NotificationType.Error);
189188
}
190189

191190
var files = await App.MainWindowInstance.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
@@ -202,20 +201,20 @@ private async Task UpdateByImport()
202201
var fileContent = await streamReader.ReadToEndAsync();
203202
if (!UigfSchemaValidator.Validate(fileContent))
204203
{
205-
await HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_InvalidUigfFile_Message, NotificationType.Error);
204+
HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_InvalidUigfFile_Message, NotificationType.Error);
206205
return;
207206
}
208207

209208
var fileJson = JsonSerializer.Deserialize<GachaRecords>(fileContent, HollowJsonSerializer.Options);
210209
if (fileJson is null)
211210
{
212-
await HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_InvalidUigfFile_Message, NotificationType.Error);
211+
HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_InvalidUigfFile_Message, NotificationType.Error);
213212
return;
214213
}
215214

216215
if(fileJson.Profiles.Count == 0)
217216
{
218-
await HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_EmptyUigfNapRecords_Message, NotificationType.Error);
217+
HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_EmptyUigfNapRecords_Message, NotificationType.Error);
219218
return;
220219
}
221220

@@ -228,7 +227,7 @@ async void SelectedImportItemsCallback(ImportItem[] selectedImportItems)
228227
var gachaRecords = _gachaService.MergeGachaRecordsFromImport(fileJson, selectedImportItems, _metadataService.ItemsMetadata!);
229228
await File.WriteAllTextAsync(AppInfo.GachaRecordPath, JsonSerializer.Serialize(gachaRecords, HollowJsonSerializer.Options));
230229

231-
await HollowHost.ShowToast(Lang.Toast_Common_Success_Title, string.Format(Lang.Toast_ImportSuccess_Message, fileJson.Info.ExportApp, selectedImportItems.Length), NotificationType.Success);
230+
HollowHost.ShowToast(Lang.Toast_Common_Success_Title, string.Format(Lang.Toast_ImportSuccess_Message, fileJson.Info.ExportApp, selectedImportItems.Length), NotificationType.Success);
232231
await LoadGachaRecords();
233232
}
234233
}
@@ -247,7 +246,7 @@ async void UrlCallback(string url)
247246

248247
if (!authKey.IsSuccess)
249248
{
250-
await HollowHost.ShowToast(Lang.SignalSearch_Update_GetRecordsFailed, authKey.Message, NotificationType.Error);
249+
HollowHost.ShowToast(Lang.SignalSearch_Update_GetRecordsFailed, authKey.Message, NotificationType.Error);
251250
return;
252251
}
253252
await UpdateRecords(authKey.Data);
@@ -260,7 +259,7 @@ private async Task UpdateByWebCaches()
260259
var authKey = _gachaService.GetGachaUrlData();
261260
if (!authKey.IsSuccess)
262261
{
263-
await HollowHost.ShowToast(Lang.SignalSearch_Update_GetRecordsFailed, authKey.Message, NotificationType.Error);
262+
HollowHost.ShowToast(Lang.SignalSearch_Update_GetRecordsFailed, authKey.Message, NotificationType.Error);
264263
return;
265264
}
266265
await UpdateRecords(authKey.Data);
@@ -270,7 +269,7 @@ private async Task UpdateRecords(GachaUrlData gachaUrlData)
270269
{
271270
if (!await _gachaService.IsAuthKeyValid(gachaUrlData.AuthKey))
272271
{
273-
await HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_InvalidAuthKey_Message, NotificationType.Error);
272+
HollowHost.ShowToast(Lang.Toast_Common_Error_Title, Lang.Toast_InvalidAuthKey_Message, NotificationType.Error);
274273
return;
275274
}
276275

Hollow/ViewModels/Pages/WikiViewModel.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Hollow.Abstractions.Enums.Hakush;
1010
using Hollow.Abstractions.JsonConverters.Serializers;
1111
using Hollow.Abstractions.Models.HttpContrasts.Hakush;
12-
using Hollow.Enums;
1312
using Hollow.Helpers;
1413
using Hollow.Languages;
1514
using Hollow.Models.Wiki;
@@ -61,7 +60,7 @@ partial void OnSelectedCharacterItemChanged(WikiCharacterItemModel? value)
6160
SelectedCharacterDetailItem = await LoadCharacterInfo(value.Id);
6261
if (SelectedCharacterDetailItem is null)
6362
{
64-
await HollowHost.ShowToast(Lang.Toast_Common_Error_Title, string.Format(Lang.Toast_WikiItemLoadFailed_Message, value.Name), NotificationType.Error);
63+
HollowHost.ShowToast(Lang.Toast_Common_Error_Title, string.Format(Lang.Toast_WikiItemLoadFailed_Message, value.Name), NotificationType.Error);
6564
}
6665
while (IsCharacterItemLoading) await Task.Delay(200);
6766
HideCoverage();

Hollow/Views/Controls/HollowHost.axaml.cs

+2-105
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Threading.Tasks;
54
using Avalonia;
6-
using Avalonia.Collections;
75
using Avalonia.Controls;
86
using Avalonia.Controls.Notifications;
97
using Avalonia.Controls.Primitives;
108
using Avalonia.Controls.Templates;
119
using Avalonia.LogicalTree;
1210
using Avalonia.Rendering.Composition;
13-
using Avalonia.Threading;
1411
using Hollow.Helpers;
15-
using Hollow.Views.Controls.Toast;
1612

1713
namespace Hollow.Views.Controls;
1814

@@ -81,27 +77,6 @@ public static void CloseDialog()
8177
{
8278
if (_mainWindow != null) CloseDialog(_mainWindow);
8379
}
84-
85-
86-
// Toasts
87-
public static readonly AttachedProperty<int> ToastLimitProperty =
88-
AvaloniaProperty.RegisterAttached<HollowHost, Window, int>("ToastLimit", defaultValue: 5);
89-
90-
public static int GetToastLimit(Control element) => element.GetValue(ToastLimitProperty);
91-
92-
public static void SetToastLimit(Control element, int value) =>
93-
element.SetValue(ToastLimitProperty, value);
94-
95-
public static readonly StyledProperty<AvaloniaList<Toast.Toast>?> ToastsCollectionProperty =
96-
AvaloniaProperty.Register<HollowHost, AvaloniaList<Toast.Toast>?>(nameof(ToastsCollection));
97-
98-
public AvaloniaList<Toast.Toast>? ToastsCollection
99-
{
100-
get => GetValue(ToastsCollectionProperty);
101-
set => SetValue(ToastsCollectionProperty, value);
102-
}
103-
104-
private int _maxToasts;
10580

10681
// Common
10782
private static Window? _mainWindow;
@@ -118,99 +93,21 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
11893
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
11994
{
12095
base.OnApplyTemplate(e);
121-
if (VisualRoot is not Window window)
96+
if (VisualRoot is not Window)
12297
throw new InvalidOperationException("HollowHost must be hosted inside a Window");
123-
ToastsCollection ??= [];
124-
_maxToasts = GetToastLimit(window);
12598

12699
e.NameScope.Get<Border>("PART_DialogBackground").PointerPressed += (_, _) => BackgroundRequestClose(this);
127100

128101
var b = e.NameScope.Get<Border>("PART_DialogBackground");
129102
b.Loaded += (_, _) => ControlAnimationHelper.MakeOpacityAnimate(ElementComposition.GetElementVisual(b)!, 400);
130103
}
131-
132-
public static async Task ShowToast(Window window, ToastModel model)
133-
{
134-
try
135-
{
136-
if (!Instances.TryGetValue(window, out var host))
137-
throw new InvalidOperationException("No HollowHost present in this window");
138-
139-
var toast = ToastPool.Get();
140-
toast.Initialize(model, host);
141-
if (host.ToastsCollection!.Count >= host._maxToasts)
142-
await ClearToast(host.ToastsCollection.First());
143-
Dispatcher.UIThread.Invoke(() =>
144-
{
145-
host.ToastsCollection.Add(toast);
146-
toast.Animate(OpacityProperty, 0d, 1d, TimeSpan.FromMilliseconds(500));
147-
toast.Animate(MarginProperty, new Thickness(0, 10, 0, -10), new Thickness(),
148-
TimeSpan.FromMilliseconds(500));
149-
});
150-
}
151-
catch
152-
{
153-
// ignored
154-
}
155-
}
156-
public static Task ShowToast(ToastModel model) =>
157-
ShowToast(_mainWindow!, model);
158-
159-
public static Task ShowToast(string title, string? content, NotificationType? type, TimeSpan? duration = null, Action? onClicked = null) =>
160-
ShowToast(new ToastModel(
161-
title,
162-
content ?? "",
163-
type ?? NotificationType.Information,
164-
duration ?? TimeSpan.FromSeconds(4),
165-
onClicked));
166-
167-
public static Task ShowToast(Window window, string title, string? content, NotificationType? type, TimeSpan? duration = null,
168-
Action? onClicked = null) =>
169-
ShowToast(window, new ToastModel(
170-
title,
171-
content ?? "",
172-
type ?? NotificationType.Information,
173-
duration ?? TimeSpan.FromSeconds(4),
174-
onClicked));
175-
176-
public static async Task ClearToast(Toast.Toast toast)
177-
{
178-
var wasRemoved = await Task.Run(async () =>
179-
{
180-
Dispatcher.UIThread.Invoke(() =>
181-
{
182-
toast.Animate(OpacityProperty, 1d, 0d, TimeSpan.FromMilliseconds(300));
183-
toast.Animate(MarginProperty, new Thickness(), new Thickness(0, 20, 0, -20),
184-
TimeSpan.FromMilliseconds(300));
185-
});
186-
await Task.Delay(300);
187-
return Dispatcher.UIThread.Invoke(() => toast.Host.ToastsCollection!.Remove(toast));
188-
});
189-
190-
if (!wasRemoved) return;
191-
ToastPool.Return(toast);
192-
}
193-
194-
public static void ClearAllToasts(Window window)
195-
{
196-
if (!Instances.TryGetValue(window, out var host))
197-
throw new InvalidOperationException("No HollowHost present in this window");
198-
ToastPool.Return(host.ToastsCollection!);
199-
Dispatcher.UIThread.Invoke(() => host.ToastsCollection!.Clear());
200-
}
201-
202-
public static void ClearAllToasts() => ClearAllToasts(_mainWindow!);
203-
204-
#region New Toasts
205104

206105
public static WindowNotificationManager NotificationManager { get; set; } = null!;
207-
public static void ShowAvaloniaToast(string title, string message, NotificationType notificationType, TimeSpan? timeSpan = null, Action? onClick = null, Action? onClose = null)
106+
public static void ShowToast(string title, string message, NotificationType notificationType, TimeSpan? timeSpan = null, Action? onClick = null, Action? onClose = null)
208107
{
209108
NotificationManager.Show(new Notification(title, message, notificationType, timeSpan, onClick, onClose));
210109
}
211110

212-
#endregion
213-
214111
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
215112
{
216113
base.OnDetachedFromLogicalTree(e);

Hollow/Views/Controls/Toast/Toast.axaml

-94
This file was deleted.

0 commit comments

Comments
 (0)