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
{
string GetApplicationDataFolder();
string GetPublisherSharedFolder();
Task MigrateAsync();
}
}

View File

@@ -1,50 +1,13 @@
using System;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage;
using Wino.Core.Domain.Interfaces;
namespace Wino.Core.UWP.Services
{
public class AppInitializerService : IAppInitializerService
{
private readonly IBackgroundTaskService _backgroundTaskService;
public const string SharedFolderName = "WinoShared";
public AppInitializerService(IBackgroundTaskService backgroundTaskService)
{
_backgroundTaskService = backgroundTaskService;
}
public string GetPublisherSharedFolder() => ApplicationData.Current.GetPublisherCacheFolder("WinoShared").Path;
public string GetPublisherSharedFolder() => ApplicationData.Current.GetPublisherCacheFolder(SharedFolderName).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 H.NotifyIcon;
using Microsoft.Extensions.DependencyInjection;
using Wino.Core;
namespace Wino.Server
{
@@ -16,11 +19,27 @@ namespace Wino.Server
private const string WinoServerAppName = "Wino.Server";
private const string WinoServerActiatedName = "Wino.Server.Activated";
public new static App Current => (App)Application.Current;
private TaskbarIcon? notifyIcon;
private static Mutex _mutex = null;
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;
@@ -36,11 +55,11 @@ namespace Wino.Server
{
if (notifyIcon == null) return;
Current.Dispatcher.BeginInvoke(() =>
Current.Dispatcher.BeginInvoke(async () =>
{
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.
thread.IsBackground = true;
thread.Start();
Services = ConfigureServices();
base.OnStartup(e);
// Create taskbar icon for the new server.
notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");
var viewModel = Services.GetRequiredService<TrayIconViewModel>();
await viewModel.Context.InitializeAsync();
notifyIcon.DataContext = viewModel;
notifyIcon.ForceCreate(enablesEfficiencyMode: true);
}
else

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
using System.Windows;
using System.Threading.Tasks;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
@@ -6,12 +7,18 @@ namespace Wino.Server
{
public partial class TrayIconViewModel : ObservableObject
{
private readonly ServerContext _context = new ServerContext();
public ServerContext Context { get; }
public TrayIconViewModel(ServerContext serverContext)
{
Context = serverContext;
}
[RelayCommand]
public void LaunchWino()
{
_context.SendTestMessageAsync();
// ServerContext.SendTestMessageAsync();
}
/// <summary>
@@ -25,6 +32,6 @@ namespace Wino.Server
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">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<OutputType>WinExe</OutputType>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
<CsWinRTComponent>true</CsWinRTComponent>
<CsWinRTWindowsMetadata>10.0.22621.0</CsWinRTWindowsMetadata>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Wino.Server.App</StartupObject>