Removal of background task service from core.

This commit is contained in:
Burak Kaan Köse
2024-07-18 15:07:17 +02:00
parent a9a9907bc6
commit 7ef045a0ad
7 changed files with 63 additions and 73 deletions

View File

@@ -1,12 +1,8 @@
using System.Threading.Tasks; namespace Wino.Core.Domain.Interfaces
namespace Wino.Core.Domain.Interfaces
{ {
public interface IAppInitializerService public interface IAppInitializerService
{ {
string GetApplicationDataFolder(); string GetApplicationDataFolder();
string GetPublisherSharedFolder(); string GetPublisherSharedFolder();
Task MigrateAsync();
} }
} }

View File

@@ -1,50 +1,13 @@
using System; using Windows.Storage;
using System.Threading.Tasks;
using Windows.Storage;
using Wino.Core.Domain.Interfaces; using Wino.Core.Domain.Interfaces;
namespace Wino.Core.UWP.Services namespace Wino.Core.UWP.Services
{ {
public class AppInitializerService : IAppInitializerService public class AppInitializerService : IAppInitializerService
{ {
private readonly IBackgroundTaskService _backgroundTaskService; public const string SharedFolderName = "WinoShared";
public AppInitializerService(IBackgroundTaskService backgroundTaskService) public string GetPublisherSharedFolder() => ApplicationData.Current.GetPublisherCacheFolder(SharedFolderName).Path;
{
_backgroundTaskService = backgroundTaskService;
}
public string GetPublisherSharedFolder() => ApplicationData.Current.GetPublisherCacheFolder("WinoShared").Path;
public string GetApplicationDataFolder() => ApplicationData.Current.LocalFolder.Path; public string GetApplicationDataFolder() => ApplicationData.Current.LocalFolder.Path;
public Task MigrateAsync()
{
UnregisterAllBackgroundTasks();
return Task.CompletedTask;
}
#region 1.6.8 -> 1.6.9
private void UnregisterAllBackgroundTasks()
{
_backgroundTaskService.UnregisterAllBackgroundTask();
}
#endregion
#region 1.7.0
/// <summary>
/// We decided to use publisher cache folder as a database going forward.
/// This migration will move the file from application local folder and delete it.
/// Going forward database will be initialized from publisher cache folder.
/// </summary>
private async Task MoveExistingDatabaseToSharedCacheFolderAsync()
{
throw new NotImplementedException();
}
#endregion
} }
} }

View File

@@ -1,6 +1,9 @@
using System.Threading; using System;
using System.Threading;
using System.Windows; using System.Windows;
using H.NotifyIcon; using H.NotifyIcon;
using Microsoft.Extensions.DependencyInjection;
using Wino.Core;
namespace Wino.Server namespace Wino.Server
{ {
@@ -16,11 +19,27 @@ namespace Wino.Server
private const string WinoServerAppName = "Wino.Server"; private const string WinoServerAppName = "Wino.Server";
private const string WinoServerActiatedName = "Wino.Server.Activated"; private const string WinoServerActiatedName = "Wino.Server.Activated";
public new static App Current => (App)Application.Current;
private TaskbarIcon? notifyIcon; private TaskbarIcon? notifyIcon;
private static Mutex _mutex = null; private static Mutex _mutex = null;
private EventWaitHandle _eventWaitHandle; private EventWaitHandle _eventWaitHandle;
protected override void OnStartup(StartupEventArgs e) public IServiceProvider Services { get; private set; }
private IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddTransient<ServerContext>();
services.AddTransient<TrayIconViewModel>();
services.RegisterCoreServices();
return services.BuildServiceProvider();
}
protected override async void OnStartup(StartupEventArgs e)
{ {
bool isCreatedNew; bool isCreatedNew;
@@ -36,11 +55,11 @@ namespace Wino.Server
{ {
if (notifyIcon == null) return; if (notifyIcon == null) return;
Current.Dispatcher.BeginInvoke(() => Current.Dispatcher.BeginInvoke(async () =>
{ {
if (notifyIcon.DataContext is TrayIconViewModel trayIconViewModel) if (notifyIcon.DataContext is TrayIconViewModel trayIconViewModel)
{ {
trayIconViewModel.Reconnect(); await trayIconViewModel.ReconnectAsync();
} }
}); });
} }
@@ -48,13 +67,19 @@ namespace Wino.Server
// It is important mark it as background otherwise it will prevent app from exiting. // It is important mark it as background otherwise it will prevent app from exiting.
thread.IsBackground = true; thread.IsBackground = true;
thread.Start(); thread.Start();
Services = ConfigureServices();
base.OnStartup(e); base.OnStartup(e);
// Create taskbar icon for the new server. // Create taskbar icon for the new server.
notifyIcon = (TaskbarIcon)FindResource("NotifyIcon"); notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");
var viewModel = Services.GetRequiredService<TrayIconViewModel>();
await viewModel.Context.InitializeAsync();
notifyIcon.DataContext = viewModel;
notifyIcon.ForceCreate(enablesEfficiencyMode: true); notifyIcon.ForceCreate(enablesEfficiencyMode: true);
} }
else else

View File

@@ -4,23 +4,23 @@ using System.Threading.Tasks;
using Windows.ApplicationModel; using Windows.ApplicationModel;
using Windows.ApplicationModel.AppService; using Windows.ApplicationModel.AppService;
using Windows.Foundation.Collections; using Windows.Foundation.Collections;
using Wino.Core.Domain.Entities;
using Wino.Core.Domain.Interfaces; using Wino.Core.Domain.Interfaces;
using Wino.Core.Services;
using Wino.Messaging; using Wino.Messaging;
using Wino.Messaging.Enums; using Wino.Messaging.Enums;
using Wino.Messaging.Server;
namespace Wino.Server namespace Wino.Server
{ {
public class ServerContext public class ServerContext : IInitializeAsync
{ {
private static object connectionLock = new object(); private static object connectionLock = new object();
private AppServiceConnection connection = null; private AppServiceConnection connection = null;
private readonly IDatabaseService _databaseService;
public ServerContext() public ServerContext(IDatabaseService databaseService)
{ {
InitializeAppServiceConnection(); _databaseService = databaseService;
} }
private string GetAppPackagFamilyName() private string GetAppPackagFamilyName()
@@ -42,7 +42,7 @@ namespace Wino.Server
/// <summary> /// <summary>
/// Open connection to UWP app service /// Open connection to UWP app service
/// </summary> /// </summary>
public async void InitializeAppServiceConnection() public async Task InitializeAppServiceConnectionAsync()
{ {
if (connection != null) DisposeConnection(); if (connection != null) DisposeConnection();
@@ -65,12 +65,6 @@ namespace Wino.Server
} }
} }
public Task SendTestMessageAsync()
{
var message = new MailAddedMessage(new MailCopy());
return SendMessageAsync(MessageType.UIMessage, message);
}
private async Task SendMessageAsync(MessageType messageType, object message) private async Task SendMessageAsync(MessageType messageType, object message)
{ {
if (connection == null) return; if (connection == null) return;
@@ -119,5 +113,11 @@ namespace Wino.Server
connection = null; connection = null;
} }
} }
public async Task InitializeAsync()
{
await InitializeAppServiceConnectionAsync();
await _databaseService.InitializeAsync();
}
} }
} }

View File

@@ -16,10 +16,6 @@
ToolTipText="Wino Mail" ToolTipText="Wino Mail"
LeftClickCommand="{Binding LaunchWinoCommand}" LeftClickCommand="{Binding LaunchWinoCommand}"
NoLeftClickDelay="True" NoLeftClickDelay="True"
ContextMenu="{StaticResource SysTrayMenu}"> ContextMenu="{StaticResource SysTrayMenu}" />
<tb:TaskbarIcon.DataContext>
<server:TrayIconViewModel />
</tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -1,4 +1,5 @@
using System.Windows; using System.Threading.Tasks;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
@@ -6,12 +7,18 @@ namespace Wino.Server
{ {
public partial class TrayIconViewModel : ObservableObject public partial class TrayIconViewModel : ObservableObject
{ {
private readonly ServerContext _context = new ServerContext(); public ServerContext Context { get; }
public TrayIconViewModel(ServerContext serverContext)
{
Context = serverContext;
}
[RelayCommand] [RelayCommand]
public void LaunchWino() public void LaunchWino()
{ {
_context.SendTestMessageAsync();
// ServerContext.SendTestMessageAsync();
} }
/// <summary> /// <summary>
@@ -25,6 +32,6 @@ namespace Wino.Server
Application.Current.Shutdown(); Application.Current.Shutdown();
} }
public void Reconnect() => _context.InitializeAppServiceConnection(); public async Task ReconnectAsync() => await Context.InitializeAppServiceConnectionAsync();
} }
} }

View File

@@ -1,11 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework> <TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo> <GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets> <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
<CsWinRTComponent>true</CsWinRTComponent>
<CsWinRTWindowsMetadata>10.0.22621.0</CsWinRTWindowsMetadata>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<StartupObject>Wino.Server.App</StartupObject> <StartupObject>Wino.Server.App</StartupObject>