notifications fix

This commit is contained in:
2024-03-10 23:04:39 +01:00
Unverified
parent 1951db857a
commit c4387213a9
15 changed files with 166 additions and 53 deletions

View File

@@ -10,8 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
</ItemGroup>
<ItemGroup>

View File

@@ -21,6 +21,19 @@ namespace VDownload.Core.Tasks
{
public partial class DownloadTask : ObservableObject
{
#region ENUMS
private enum TaskResult
{
Success,
Cancellation,
Error
}
#endregion
#region SERVICES
protected readonly IConfigurationService _configurationService;
@@ -156,6 +169,8 @@ namespace VDownload.Core.Tasks
$"{_stringResourcesService.NotificationsResources.Get("Author")}: {Video.Author}"
};
string errorMessage = null;
TaskResult? endingStatus = null;
try
{
IProgress<double> onProgressDownloading = new Progress<double>((value) =>
@@ -194,16 +209,12 @@ namespace VDownload.Core.Tasks
await Finalizing(outputFile);
UpdateStatusWithDispatcher(DownloadTaskStatus.EndedSuccessfully);
if (_settingsService.Data.Common.Notifications.OnSuccessful)
{
string title = _stringResourcesService.NotificationsResources.Get("OnSuccessfulTitle");
_notificationsService.SendNotification(title, content);
}
endingStatus = TaskResult.Success;
}
catch (OperationCanceledException)
{
UpdateStatusWithDispatcher(DownloadTaskStatus.EndedCancelled);
endingStatus = TaskResult.Cancellation;
}
catch (Exception ex)
{
@@ -228,16 +239,30 @@ namespace VDownload.Core.Tasks
UpdateErrorWithDispatcher(message);
UpdateStatusWithDispatcher(DownloadTaskStatus.EndedUnsuccessfully);
if (_settingsService.Data.Common.Notifications.OnUnsuccessful)
{
string title = _stringResourcesService.NotificationsResources.Get("OnSuccessfulTitle");
content.Add($"{_stringResourcesService.NotificationsResources.Get("Message")}: {ex.Message}");
_notificationsService.SendNotification(title, content);
}
endingStatus = TaskResult.Error;
errorMessage = message;
}
finally
{
switch (endingStatus)
{
case TaskResult.Error:
if (_settingsService.Data.Common.Notifications.OnUnsuccessful)
{
string title = _stringResourcesService.NotificationsResources.Get("OnUnsuccessfulTitle");
content.Add($"{_stringResourcesService.NotificationsResources.Get("Message")}: {errorMessage}");
_notificationsService.SendNotification(title, content);
}
break;
case TaskResult.Success:
if (_settingsService.Data.Common.Notifications.OnSuccessful)
{
string title = _stringResourcesService.NotificationsResources.Get("OnSuccessfulTitle");
_notificationsService.SendNotification(title, content);
}
break;
}
if (Status != DownloadTaskStatus.EndedUnsuccessfully || _settingsService.Data.Common.Temp.DeleteOnError)
{
Directory.Delete(tempDirectory, true);

View File

@@ -11,8 +11,8 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.WinUI.Helpers" Version="8.0.240109" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="SimpleToolkit.MVVM" Version="1.7.4" />
<PackageReference Include="SimpleToolkit.UI.Models" Version="1.7.4" />

View File

@@ -20,8 +20,8 @@
<PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.0.240109" />
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.0.240109" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls" Version="7.1.2" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
<PackageReference Include="SimpleToolkit.UI.WinUI.Behaviors" Version="1.7.4" />
<PackageReference Include="SimpleToolkit.UI.WinUI.Controls" Version="1.7.4" />

View File

@@ -10,8 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
</ItemGroup>
<ItemGroup>

View File

@@ -10,8 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,4 +1,5 @@
using Microsoft.Windows.AppNotifications;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
using System;
using System.Collections.Generic;
@@ -10,6 +11,7 @@ namespace VDownload.Services.UI.Notifications
{
public interface INotificationsService
{
void Initialize(Action notificationInvoked);
void SendNotification(string title, IEnumerable<string> message);
}
@@ -17,8 +19,26 @@ namespace VDownload.Services.UI.Notifications
public class NotificationsService : INotificationsService
{
#region CONSTRCUTORS
~NotificationsService()
{
AppNotificationManager.Default.Unregister();
}
#endregion
#region PUBLIC METHODS
public void Initialize(Action notificationInvoked)
{
AppNotificationManager.Default.NotificationInvoked += (obj, args) => notificationInvoked.Invoke();
AppNotificationManager.Default.Register();
}
public void SendNotification(string title, IEnumerable<string> message)
{
AppNotificationBuilder builder = new AppNotificationBuilder();
@@ -27,7 +47,9 @@ namespace VDownload.Services.UI.Notifications
{
builder.AddText(text);
}
AppNotification notification = builder.BuildNotification();
AppNotificationManager.Default.Show(notification);
}

View File

@@ -10,7 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
</ItemGroup>
</Project>

View File

@@ -10,7 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
</ItemGroup>
</Project>

View File

@@ -10,8 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
</ItemGroup>
<ItemGroup>

View File

@@ -13,8 +13,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,7 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppNotifications;
using System;
using System.Net.Http;
using System.Threading.Tasks;
@@ -39,6 +41,7 @@ using VDownload.Sources.Twitch;
using VDownload.Sources.Twitch.Api;
using VDownload.Sources.Twitch.Authentication;
using Windows.Graphics.Printing;
using Windows.UI.Notifications;
namespace VDownload
{
@@ -175,32 +178,34 @@ namespace VDownload
services.AddTransient<BaseWindow>();
}
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
await InitData();
_window = _serviceProvider.GetService<BaseWindow>();
_window.RootLoaded += Window_RootLoaded;
_window.Activate();
AssignStaticProperties();
}
protected async Task InitData()
protected async Task InitializeServices()
{
IApplicationDataService applicationDataService = _serviceProvider.GetService<IApplicationDataService>();
ISettingsService settingsService = _serviceProvider.GetService<ISettingsService>();
IAuthenticationDataService authenticationDataService = _serviceProvider.GetService<IAuthenticationDataService>();
ISubscriptionsDataService subscriptionsDataService = _serviceProvider.GetService<ISubscriptionsDataService>();
await Task.WhenAll(applicationDataService.Load(), settingsService.Load(), authenticationDataService.Load(), subscriptionsDataService.Load());
Task initViewModelToViewConverterTask = Task.Run(() => ViewModelToViewConverter.ServiceProvider = _serviceProvider);
Task initStoragePickerServiceTask = Task.Run(() => _serviceProvider.GetService<IStoragePickerService>().DefaultRoot = _window);
Task initNotificationsServiceTask = Task.Run(() => _serviceProvider.GetService<INotificationsService>().Initialize(() => WindowHelper.ShowWindow(_window)));
await Task.WhenAll(
applicationDataService.Load(),
settingsService.Load(),
authenticationDataService.Load(),
subscriptionsDataService.Load(),
initStoragePickerServiceTask,
initViewModelToViewConverterTask,
initNotificationsServiceTask
);
}
protected void AssignStaticProperties()
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
IStoragePickerService storagePickerService = _serviceProvider.GetService<IStoragePickerService>();
storagePickerService.DefaultRoot = _window;
_window = _serviceProvider.GetService<BaseWindow>();
_window.RootLoaded += Window_RootLoaded;
_window.Activate();
ViewModelToViewConverter.ServiceProvider = _serviceProvider;
await InitializeServices();
}
protected void Window_RootLoaded(object sender, EventArgs e)

View File

@@ -5,7 +5,9 @@
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap rescap com desktop">
<Identity
Name="VDownload"
@@ -42,6 +44,19 @@
<uap:DefaultTile Wide310x150Logo="Assets\Logo\Wide310x150Logo.png" Square71x71Logo="Assets\Logo\SmallTile.png" Square310x310Logo="Assets\Logo\LargeTile.png"/>
<uap:SplashScreen Image="Assets\Logo\SplashScreen.png" />
</uap:VisualElements>
<Extensions>
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="1e95e58c-ce3f-421a-8cee-cd3332a87109" />
</desktop:Extension>
<com:Extension Category="windows.comServer">
<com:ComServer>
<com:ExeServer Executable="App1.exe" Arguments="----AppNotificationActivated:" DisplayName="Toast activator">
<com:Class Id="1e95e58c-ce3f-421a-8cee-cd3332a87109" DisplayName="Toast activator"/>
</com:ExeServer>
</com:ComServer>
</com:Extension>
</Extensions>
</Application>
</Applications>

View File

@@ -18,6 +18,7 @@
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Version>0.0.0</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Content Remove="Assets\BaseView\AuthenticationDark.png" />
@@ -167,8 +168,9 @@
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.0.240109" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
<PackageReference Include="SimpleToolkit.UI.WinUI.Converters" Version="1.7.4" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

43
VDownload/WindowHelper.cs Normal file
View File

@@ -0,0 +1,43 @@
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace VDownload
{
public static partial class WindowHelper
{
#region PUBLIC METHODS
public static void ShowWindow(Window window)
{
// Bring the window to the foreground... first get the window handle...
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
// Restore window if minimized... requires DLL import above
ShowWindow(hwnd, 0x00000009);
// And call SetForegroundWindow... requires DLL import above
SetForegroundWindow(hwnd);
}
#endregion
#region PRIVATE METHODS
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetForegroundWindow(IntPtr hWnd);
#endregion
}
}