-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathBackgroundActivityManager.cs
More file actions
280 lines (244 loc) · 13 KB
/
BackgroundActivityManager.cs
File metadata and controls
280 lines (244 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using CollapseLauncher.Dialogs;
using CollapseLauncher.Extension;
using CollapseLauncher.Helper;
using CollapseLauncher.Helper.Metadata;
using CollapseLauncher.Interfaces;
using CollapseLauncher.Pages;
using CollapseLauncher.Plugins;
using Hi3Helper;
using Hi3Helper.Data;
using Hi3Helper.Shared.Region;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
// ReSharper disable StringLiteralTypo
// ReSharper disable ClassNeverInstantiated.Global
#pragma warning disable IDE0130
namespace CollapseLauncher
{
internal class BackgroundActivityManager
{
private static readonly ThemeShadow InfoBarShadow = new();
private static readonly Dictionary<PresetConfig, IBackgroundActivity> BackgroundActivities = new();
public static void Attach(PresetConfig presetConfig, IBackgroundActivity activity, string activityTitle, string activitySubtitle)
{
if (!BackgroundActivities.TryAdd(presetConfig, activity))
{
return;
}
DispatcherQueueExtensions.TryEnqueue(() => AttachEventToNotification(presetConfig, activity, activityTitle, activitySubtitle));
#if DEBUG
Logger.LogWriteLine($"Background activity for {presetConfig} has been attached", LogType.Debug, true);
#endif
}
public static void Detach(PresetConfig presetConfig)
{
if (BackgroundActivities.Remove(presetConfig))
{
NotificationSender.RemoveCustomNotification(presetConfig.GetHashCode());
#if DEBUG
Logger.LogWriteLine($"Background activity for {presetConfig} has been detached", LogType.Debug, true);
return;
#endif
}
#if DEBUG
Logger.LogWriteLine($"Cannot detach background activity for {presetConfig} because it doesn't attached", LogType.Debug, true);
#endif
}
private static IconElement GetGamePresetIcon(PresetConfig presetConfig)
{
string uri = presetConfig.GameType switch
{
GameNameType.Honkai => "ms-appx:///Assets/Images/GameLogo/honkai-logo.png",
GameNameType.Genshin => "ms-appx:///Assets/Images/GameLogo/genshin-logo.png",
GameNameType.StarRail => "ms-appx:///Assets/Images/GameLogo/starrail-logo.png",
GameNameType.Zenless => "ms-appx:///Assets/Images/GameLogo/zenless-logo.png",
_ => "ms-appx:///Assets/Images/GameMascot/PaimonWhat.png"
};
if (presetConfig is not PluginPresetConfigWrapper pluginPresetConfig)
{
return Create(uri);
}
PluginInfo pluginInfo = pluginPresetConfig.PluginInfo;
GamePluginIconConverter converter = StaticConverter<GamePluginIconConverter>.Shared;
if (converter.Convert(pluginInfo, null!, null!, "") is not IconElement iconElement)
{
return Create(uri);
}
return iconElement;
static BitmapIcon Create(string uri)
=> new()
{
UriSource = new Uri(uri),
ShowAsMonochrome = false
};
}
private static void AttachEventToNotification(PresetConfig presetConfig, IBackgroundActivity activity, string activityTitle, string activitySubtitle)
{
Thickness containerNotClosableMargin = new(-28, -8, 24, 20);
Thickness containerClosableMargin = new(-28, -8, -28, 20);
Brush brush = UIElementExtensions.GetApplicationResource<Brush>("InfoBarAnnouncementBrush");
InfoBar parentNotificationUI = new InfoBar
{
Tag = presetConfig.GetHashCode(),
Severity = InfoBarSeverity.Informational,
Background = brush,
IsOpen = true,
IsClosable = false,
Shadow = InfoBarShadow,
Title = activityTitle,
Message = activitySubtitle
}
.WithMargin(4d, 4d, 4d, 0)
.WithCornerRadius(8);
parentNotificationUI.Translation = new Vector3(0,0,32);
StackPanel parentContainer = UIElementExtensions.CreateStackPanel()
.WithMargin(parentNotificationUI.IsClosable ? containerClosableMargin : containerNotClosableMargin);
parentNotificationUI.Content = parentContainer;
Grid parentGrid = parentContainer.AddElementToStackPanel(
UIElementExtensions.CreateGrid()
.WithColumns(new GridLength(72), new GridLength(1, GridUnitType.Star))
);
StackPanel progressLogoContainer = parentGrid.AddElementToGridColumn(
UIElementExtensions.CreateStackPanel()
.WithWidthAndHeight(64d)
.WithMargin(0d, 4d, 8d, 4d)
.WithCornerRadius(8),
0
);
_ = progressLogoContainer.AddElementToStackPanel(GetGamePresetIcon(presetConfig).WithWidthAndHeight(64));
StackPanel progressStatusContainer = parentGrid.AddElementToGridColumn(
UIElementExtensions.CreateStackPanel()
.WithVerticalAlignment(VerticalAlignment.Center)
.WithMargin(8d, -4d, 0, 0),
1
);
Grid progressStatusGrid = progressStatusContainer.AddElementToStackPanel(
UIElementExtensions.CreateGrid()
.WithColumns(new GridLength(1, GridUnitType.Star), new GridLength(1, GridUnitType.Star))
.WithRows(new GridLength(1, GridUnitType.Star), new GridLength(1, GridUnitType.Star))
.WithMargin(0d, 0d, 0d, 16d)
);
TextBlock progressLeftTitle = progressStatusGrid.AddElementToGridRowColumn(new TextBlock
{
Style = UIElementExtensions.GetApplicationResource<Style>("BodyStrongTextBlockStyle"),
Text = Locale.Current.Lang?._BackgroundNotification?.LoadingTitle
});
TextBlock progressLeftSubtitle = progressStatusGrid.AddElementToGridRowColumn(new TextBlock
{
Style = UIElementExtensions.GetApplicationResource<Style>("CaptionTextBlockStyle"),
Text = Locale.Current.Lang?._BackgroundNotification?.Placeholder
}, 1);
TextBlock progressRightTitle = progressStatusGrid.AddElementToGridRowColumn(new TextBlock
{
Style = UIElementExtensions.GetApplicationResource<Style>("BodyStrongTextBlockStyle"),
Text = Locale.Current.Lang?._BackgroundNotification?.Placeholder
}.WithHorizontalAlignment(HorizontalAlignment.Right), 0, 1);
TextBlock progressRightSubtitle = progressStatusGrid.AddElementToGridRowColumn(new TextBlock
{
Style = UIElementExtensions.GetApplicationResource<Style>("CaptionTextBlockStyle"),
Text = Locale.Current.Lang?._BackgroundNotification?.Placeholder
}.WithHorizontalAlignment(HorizontalAlignment.Right), 1, 1);
ProgressBar progressBar = progressStatusContainer.AddElementToStackPanel(
new ProgressBar { Minimum = 0, Maximum = 100, Value = 0, IsIndeterminate = true });
Button cancelButton =
UIElementExtensions.CreateButtonWithIcon<Button>(Locale.Current.Lang?._HomePage?.PauseCancelDownloadBtn,
"",
"FontAwesomeSolid",
"AccentButtonStyle"
)
.WithHorizontalAlignment(HorizontalAlignment.Right)
.WithMargin(0d, 4d, 0d, 0d);
cancelButton.Click += (_, _) =>
{
cancelButton.IsEnabled = false;
activity!.CancelRoutine();
parentNotificationUI.IsOpen = false;
};
bool isGameInstaller = activity is IGameInstallManager;
Button settingsButton =
UIElementExtensions.CreateButtonWithIcon<Button>(
Locale.Current.Lang?._Dialogs?.DownloadSettingsTitle,
"\uf013",
"FontAwesomeSolid",
"AccentButtonStyle"
)
.WithHorizontalAlignment(HorizontalAlignment.Right)
.WithMargin(0d, 4d, 8d, 0d);
if (isGameInstaller)
{
settingsButton.Click += async (_, _) => await SimpleDialogs.Dialog_DownloadSettings((activity as IGameInstallManager)!);
}
else
{
settingsButton.Visibility = Visibility.Collapsed;
}
StackPanel controlButtons = parentContainer.AddElementToStackPanel(
UIElementExtensions.CreateStackPanel(Orientation.Horizontal)
.WithHorizontalAlignment(HorizontalAlignment.Right)
);
controlButtons.AddElementToStackPanel(settingsButton, cancelButton);
EventHandler<TotalPerFileProgress> progressChangedEventHandler = (_, args) => activity?.Dispatch(() =>
{
progressBar.Value = args!.ProgressAllPercentage;
progressLeftSubtitle.Text = string.Format(Locale.Current.Lang?._Misc?.Speed ?? "", ConverterTool.SummarizeSizeSimple(args.ProgressAllSpeed));
progressRightTitle.Text = string.Format(Locale.Current.Lang?._Misc?.TimeRemainHMSFormat ?? "", args.ProgressAllTimeLeft);
progressRightSubtitle.Text = string.Format(Locale.Current.Lang?._UpdatePage?.UpdateHeader1 + " {0}%", args.ProgressAllPercentage);
});
EventHandler<TotalPerFileStatus> statusChangedEventHandler = (_, args) => activity?.Dispatch(() =>
{
progressBar.IsIndeterminate = args!.IsProgressAllIndetermined;
progressLeftTitle.Text = args.ActivityStatus;
if (args.IsCanceled)
{
cancelButton.IsEnabled = false;
settingsButton.IsEnabled = false;
controlButtons.Visibility = Visibility.Collapsed;
parentNotificationUI.Severity = InfoBarSeverity.Error;
parentNotificationUI.Title = string.Format(Locale.Current.Lang?._BackgroundNotification?.NotifBadge_Error!, activityTitle);
parentNotificationUI.IsClosable = true;
parentContainer.Margin = containerClosableMargin;
}
if (args.IsCompleted)
{
cancelButton.IsEnabled = false;
settingsButton.IsEnabled = false;
controlButtons.Visibility = Visibility.Collapsed;
parentNotificationUI.Severity = InfoBarSeverity.Success;
parentNotificationUI.Title = string.Format(Locale.Current.Lang?._BackgroundNotification?.NotifBadge_Completed!, activityTitle);
parentNotificationUI.IsClosable = true;
parentContainer.Margin = containerClosableMargin;
}
if (!args.IsRunning)
{
return;
}
cancelButton.IsEnabled = true;
settingsButton.IsEnabled = true;
controlButtons.Visibility = Visibility.Visible;
parentNotificationUI.Severity = InfoBarSeverity.Informational;
parentNotificationUI.Title = activityTitle;
parentNotificationUI.IsClosable = false;
parentContainer.Margin = containerNotClosableMargin;
});
activity!.ProgressChanged += progressChangedEventHandler;
activity!.StatusChanged += statusChangedEventHandler;
activity.FlushingTrigger += (_, _) =>
{
activity.ProgressChanged -= progressChangedEventHandler;
activity.StatusChanged -= statusChangedEventHandler;
};
parentNotificationUI.Closing += (_, _) =>
{
activity.ProgressChanged -= progressChangedEventHandler;
activity.StatusChanged -= statusChangedEventHandler;
Detach(presetConfig);
};
NotificationSender.SendCustomNotification(presetConfig.GetHashCode(), parentNotificationUI);
}
}
}