Skip to content

Commit c515d00

Browse files
committed
added notifications helper
1 parent 07847af commit c515d00

File tree

7 files changed

+75
-74
lines changed

7 files changed

+75
-74
lines changed

src/Avalonia.Desktop/App.axaml.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Avalonia.Controls;
22
using Avalonia.Controls.ApplicationLifetimes;
3-
using Avalonia.Controls.Notifications;
43
using Avalonia.Desktop.DI;
5-
using Avalonia.Desktop.Helpers;
64
using Avalonia.Desktop.Misc;
75
using Avalonia.Desktop.ViewModels;
86
using Avalonia.Markup.Xaml;
@@ -19,9 +17,6 @@ namespace Avalonia.Desktop;
1917

2018
public sealed class App : Application
2119
{
22-
public static WindowNotificationManager NotificationManager { get; private set; } = null!;
23-
public static Random Random { get; private set; } = null!;
24-
2520
private static App _app = null!;
2621

2722
public override void Initialize()
@@ -55,8 +50,6 @@ public static int Run(AppBuilder builder)
5550
lifetime.MainWindow = new MainWindow();
5651
lifetime.MainWindow.DataContext = vmFactory.GetMainWindowViewModel();
5752

58-
InitializeStatics();
59-
6053
if (ClientProperties.IsDeveloperMode)
6154
{
6255
logger.LogInformation("Starting in developer mode");
@@ -94,18 +87,6 @@ public static int Run(AppBuilder builder)
9487
return code;
9588
}
9689

97-
private static void InitializeStatics()
98-
{
99-
Random = new();
100-
101-
NotificationManager = new(AvaloniaProperties.TopLevel)
102-
{
103-
MaxItems = 3,
104-
Position = NotificationPosition.TopRight,
105-
Margin = new(0, 50, 10, 0)
106-
};
107-
}
108-
10990
/// <summary>
11091
/// Load DI bindings
11192
/// </summary>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Avalonia.Controls.Notifications;
2+
using Avalonia.Desktop.Helpers;
3+
4+
namespace Avalonia.Desktop.Misc;
5+
6+
/// <summary>
7+
/// Helper that fixes crash when multiple notifications with the same text are shown.
8+
/// </summary>
9+
public static class NotificationsHelper
10+
{
11+
public static WindowNotificationManager NotificationManager { get; }
12+
13+
private static Random Random { get; }
14+
15+
static NotificationsHelper()
16+
{
17+
Random = new();
18+
19+
NotificationManager = new(AvaloniaProperties.TopLevel)
20+
{
21+
MaxItems = 3,
22+
Position = NotificationPosition.TopRight,
23+
Margin = new(0, 50, 10, 0)
24+
};
25+
}
26+
27+
[Obsolete("Remove when https://github.com/AvaloniaUI/Avalonia/issues/15766 is fixed.")]
28+
public static void Show(
29+
object content,
30+
NotificationType type,
31+
TimeSpan? expiration = null,
32+
Action? onClick = null,
33+
Action? onClose = null,
34+
string[]? classes = null)
35+
{
36+
var length = Random.Next(1, 200);
37+
var repeatedString = new string('\u200B', length);
38+
39+
NotificationManager.Show(
40+
content + repeatedString,
41+
type,
42+
expiration,
43+
onClick,
44+
onClose,
45+
classes
46+
);
47+
}
48+
}

src/Avalonia.Desktop/ViewModels/CampaignsViewModel.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Addons.Providers;
22
using Avalonia.Controls.Notifications;
3+
using Avalonia.Desktop.Misc;
34
using Common.Client.Interfaces;
45
using Common.Client.Providers;
56
using Common.Enums;
@@ -162,11 +163,8 @@ private async Task StartCampaignAsync(object? command)
162163
}
163164
catch (Exception ex)
164165
{
165-
var length = App.Random.Next(1, 100);
166-
var repeatedString = new string('\u200B', length);
167-
168-
App.NotificationManager.Show(
169-
"Critical error! Exception is written to the log." + repeatedString,
166+
NotificationsHelper.Show(
167+
"Critical error! Exception is written to the log.",
170168
NotificationType.Error
171169
);
172170

src/Avalonia.Desktop/ViewModels/DownloadsViewModel.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Addons.Providers;
22
using Avalonia.Controls.Notifications;
3+
using Avalonia.Desktop.Misc;
34
using Avalonia.Threading;
45
using Common.Entities;
56
using Common.Enums;
@@ -184,11 +185,8 @@ private async Task UpdateAsync(bool? createNew)
184185

185186
if (!result)
186187
{
187-
var length = App.Random.Next(1, 100);
188-
var repeatedString = new string('\u200B', length);
189-
190-
App.NotificationManager.Show(
191-
$"Error while getting downloadable addons for{Environment.NewLine}{Game.FullName}" + repeatedString,
188+
NotificationsHelper.Show(
189+
$"Error while getting downloadable addons for{Environment.NewLine}{Game.FullName}",
192190
NotificationType.Error
193191
);
194192
}
@@ -257,11 +255,8 @@ private async Task DownloadAddon()
257255
}
258256
catch (Exception ex)
259257
{
260-
var length = App.Random.Next(1, 100);
261-
var repeatedString = new string('\u200B', length);
262-
263-
App.NotificationManager.Show(
264-
"Critical error! Exception is written to the log." + repeatedString,
258+
NotificationsHelper.Show(
259+
"Critical error! Exception is written to the log.",
265260
NotificationType.Error
266261
);
267262

src/Avalonia.Desktop/ViewModels/MapsViewModel.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Addons.Providers;
22
using Avalonia.Controls.Notifications;
3+
using Avalonia.Desktop.Misc;
34
using Common.Client.Interfaces;
45
using Common.Client.Providers;
56
using Common.Enums;
@@ -154,11 +155,8 @@ private async Task StartMapAsync(object? command)
154155
}
155156
catch (Exception ex)
156157
{
157-
var length = App.Random.Next(1, 100);
158-
var repeatedString = new string('\u200B', length);
159-
160-
App.NotificationManager.Show(
161-
"Critical error! Exception is written to the log." + repeatedString,
158+
NotificationsHelper.Show(
159+
"Critical error! Exception is written to the log.",
162160
NotificationType.Error
163161
);
164162

src/Avalonia.Desktop/ViewModels/PortViewModel.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Avalonia.Controls.Notifications;
2+
using Avalonia.Desktop.Misc;
23
using Common.Entities;
34
using Common.Enums;
45
using Common.Helpers;
@@ -248,11 +249,8 @@ private async Task InstallAsync()
248249
}
249250
catch (Exception ex)
250251
{
251-
var length = App.Random.Next(1, 100);
252-
var repeatedString = new string('\u200B', length);
253-
254-
App.NotificationManager.Show(
255-
"Critical error! Exception is written to the log." + repeatedString,
252+
NotificationsHelper.Show(
253+
"Critical error! Exception is written to the log.",
256254
NotificationType.Error
257255
);
258256

@@ -285,11 +283,8 @@ private void Uninstall()
285283
}
286284
catch (Exception ex)
287285
{
288-
var length = App.Random.Next(1, 100);
289-
var repeatedString = new string('\u200B', length);
290-
291-
App.NotificationManager.Show(
292-
"Critical error! Exception is written to the log." + repeatedString,
286+
NotificationsHelper.Show(
287+
"Critical error! Exception is written to the log.",
293288
NotificationType.Error
294289
);
295290

src/Avalonia.Desktop/ViewModels/PortsViewModel.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Avalonia.Controls.Notifications;
22
using Avalonia.Desktop.Helpers;
3+
using Avalonia.Desktop.Misc;
34
using Avalonia.Platform.Storage;
45
using Common.Enums;
56
using CommunityToolkit.Diagnostics;
@@ -93,11 +94,8 @@ private async Task AddCustomPortAsync()
9394
}
9495
catch (Exception ex)
9596
{
96-
var length = App.Random.Next(1, 100);
97-
var repeatedString = new string('\u200B', length);
98-
99-
App.NotificationManager.Show(
100-
"Critical error! Exception is written to the log." + repeatedString,
97+
NotificationsHelper.Show(
98+
"Critical error! Exception is written to the log.",
10199
NotificationType.Error
102100
);
103101

@@ -125,11 +123,8 @@ private async Task EditCustomPortAsync()
125123
}
126124
catch (Exception ex)
127125
{
128-
var length = App.Random.Next(1, 100);
129-
var repeatedString = new string('\u200B', length);
130-
131-
App.NotificationManager.Show(
132-
"Critical error! Exception is written to the log." + repeatedString,
126+
NotificationsHelper.Show(
127+
"Critical error! Exception is written to the log.",
133128
NotificationType.Error
134129
);
135130

@@ -152,11 +147,8 @@ private void DeleteCustomPort()
152147
}
153148
catch (Exception ex)
154149
{
155-
var length = App.Random.Next(1, 100);
156-
var repeatedString = new string('\u200B', length);
157-
158-
App.NotificationManager.Show(
159-
"Critical error! Exception is written to the log." + repeatedString,
150+
NotificationsHelper.Show(
151+
"Critical error! Exception is written to the log.",
160152
NotificationType.Error
161153
);
162154

@@ -213,11 +205,8 @@ private void SaveCustomPort()
213205
}
214206
catch (Exception ex)
215207
{
216-
var length = App.Random.Next(1, 100);
217-
var repeatedString = new string('\u200B', length);
218-
219-
App.NotificationManager.Show(
220-
"Critical error! Exception is written to the log." + repeatedString,
208+
NotificationsHelper.Show(
209+
"Critical error! Exception is written to the log.",
221210
NotificationType.Error
222211
);
223212

@@ -261,11 +250,8 @@ private async Task SelectPortExeAsync()
261250
}
262251
catch (Exception ex)
263252
{
264-
var length = App.Random.Next(1, 100);
265-
var repeatedString = new string('\u200B', length);
266-
267-
App.NotificationManager.Show(
268-
"Critical error! Exception is written to the log." + repeatedString,
253+
NotificationsHelper.Show(
254+
"Critical error! Exception is written to the log.",
269255
NotificationType.Error
270256
);
271257

0 commit comments

Comments
 (0)