From 38f44a8b39804592c8efa7ddae798711018f267d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Thu, 18 Jul 2024 20:03:58 +0200 Subject: [PATCH] Delegating changes to UI and triggering new background synchronization. --- .../Interfaces/IAppInitializerService.cs | 8 - .../Interfaces/IApplicationConfiguration.cs | 21 +++ .../Interfaces/INativeAppService.cs | 1 + Wino.Core.UWP/CoreUWPContainerSetup.cs | 2 +- .../Services/AppInitializerService.cs | 13 -- Wino.Core.UWP/Services/NativeAppService.cs | 26 ++- .../Services/PreferencesService.cs | 2 +- .../Services/StatePersistenceService.cs | 14 +- .../Services/WinoServerConnectionManager.cs | 3 + Wino.Core.UWP/Wino.Core.UWP.csproj | 3 +- .../Authenticators/OutlookAuthenticator.cs | 15 +- Wino.Core/CoreContainerSetup.cs | 6 + .../Services/ApplicationConfiguration.cs | 13 ++ Wino.Core/Services/DatabaseService.cs | 13 +- Wino.Core/Services/ImapTestService.cs | 6 +- Wino.Mail.ViewModels/AboutPageViewModel.cs | 6 +- Wino.Mail.ViewModels/AppShellViewModel.cs | 4 + Wino.Mail/App.xaml.cs | 11 +- Wino.Mail/AppShell.xaml | 2 +- Wino.Mail/Wino.Mail.csproj | 2 - Wino.Packaging/Wino.Packaging.wapproj | 39 +++++ Wino.Server/App.xaml.cs | 53 ++++-- Wino.Server/ServerContext.cs | 152 ++++++++++++++---- ...rayIconViewModel.cs => ServerViewModel.cs} | 11 +- Wino.Server/Wino.Server.csproj | 9 ++ 25 files changed, 331 insertions(+), 104 deletions(-) delete mode 100644 Wino.Core.Domain/Interfaces/IAppInitializerService.cs create mode 100644 Wino.Core.Domain/Interfaces/IApplicationConfiguration.cs delete mode 100644 Wino.Core.UWP/Services/AppInitializerService.cs rename {Wino.Mail => Wino.Core.UWP}/Services/PreferencesService.cs (99%) rename {Wino.Mail => Wino.Core.UWP}/Services/StatePersistenceService.cs (90%) create mode 100644 Wino.Core/Services/ApplicationConfiguration.cs rename Wino.Server/{TrayIconViewModel.cs => ServerViewModel.cs} (68%) diff --git a/Wino.Core.Domain/Interfaces/IAppInitializerService.cs b/Wino.Core.Domain/Interfaces/IAppInitializerService.cs deleted file mode 100644 index a5d9d280..00000000 --- a/Wino.Core.Domain/Interfaces/IAppInitializerService.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Wino.Core.Domain.Interfaces -{ - public interface IAppInitializerService - { - string GetApplicationDataFolder(); - string GetPublisherSharedFolder(); - } -} diff --git a/Wino.Core.Domain/Interfaces/IApplicationConfiguration.cs b/Wino.Core.Domain/Interfaces/IApplicationConfiguration.cs new file mode 100644 index 00000000..4a9aca60 --- /dev/null +++ b/Wino.Core.Domain/Interfaces/IApplicationConfiguration.cs @@ -0,0 +1,21 @@ +namespace Wino.Core.Domain.Interfaces +{ + /// + /// Singleton object that holds the application data folder path and the publisher shared folder path. + /// Load the values before calling any service. + /// App data folder is used for storing files. + /// Pubhlisher cache folder is only used for database file so other apps can access it in the same package by same publisher. + /// + public interface IApplicationConfiguration + { + /// + /// Application data folder. + /// + string ApplicationDataFolderPath { get; set; } + + /// + /// Publisher shared folder path. + /// + string PublisherSharedFolderPath { get; set; } + } +} diff --git a/Wino.Core.Domain/Interfaces/INativeAppService.cs b/Wino.Core.Domain/Interfaces/INativeAppService.cs index b6d14249..93162f73 100644 --- a/Wino.Core.Domain/Interfaces/INativeAppService.cs +++ b/Wino.Core.Domain/Interfaces/INativeAppService.cs @@ -11,6 +11,7 @@ namespace Wino.Core.Domain.Interfaces Task GetEditorBundlePathAsync(); Task LaunchFileAsync(string filePath); Task LaunchUriAsync(Uri uri); + bool IsAppRunning(); string GetFullAppVersion(); diff --git a/Wino.Core.UWP/CoreUWPContainerSetup.cs b/Wino.Core.UWP/CoreUWPContainerSetup.cs index cde45f8e..b2c4f89c 100644 --- a/Wino.Core.UWP/CoreUWPContainerSetup.cs +++ b/Wino.Core.UWP/CoreUWPContainerSetup.cs @@ -20,7 +20,7 @@ namespace Wino.Core.UWP services.AddSingleton(); services.AddSingleton(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/Wino.Core.UWP/Services/AppInitializerService.cs b/Wino.Core.UWP/Services/AppInitializerService.cs deleted file mode 100644 index dab80991..00000000 --- a/Wino.Core.UWP/Services/AppInitializerService.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Windows.Storage; -using Wino.Core.Domain.Interfaces; - -namespace Wino.Core.UWP.Services -{ - public class AppInitializerService : IAppInitializerService - { - public const string SharedFolderName = "WinoShared"; - - public string GetPublisherSharedFolder() => ApplicationData.Current.GetPublisherCacheFolder(SharedFolderName).Path; - public string GetApplicationDataFolder() => ApplicationData.Current.LocalFolder.Path; - } -} diff --git a/Wino.Core.UWP/Services/NativeAppService.cs b/Wino.Core.UWP/Services/NativeAppService.cs index 27d4b3cc..9c1521b1 100644 --- a/Wino.Core.UWP/Services/NativeAppService.cs +++ b/Wino.Core.UWP/Services/NativeAppService.cs @@ -9,11 +9,14 @@ using Windows.Storage; using Windows.Storage.Streams; using Windows.System; using Windows.UI.Shell; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; using Wino.Core.Domain.Interfaces; using Wino.Core.Domain.Models.Authorization; +#if WINDOWS_UWP +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +#endif + namespace Wino.Services { public class NativeAppService : INativeAppService @@ -21,7 +24,14 @@ namespace Wino.Services private string _mimeMessagesFolder; private string _editorBundlePath; - public string GetWebAuthenticationBrokerUri() => WebAuthenticationBroker.GetCurrentApplicationCallbackUri().AbsoluteUri; + public string GetWebAuthenticationBrokerUri() + { +#if WINDOWS_UWP + return WebAuthenticationBroker.GetCurrentApplicationCallbackUri().AbsoluteUri; +#endif + + return string.Empty; + } public async Task GetMimeMessageStoragePath() { @@ -91,7 +101,15 @@ namespace Wino.Services return _editorBundlePath; } - public bool IsAppRunning() => (Window.Current?.Content as Frame)?.Content != null; + public bool IsAppRunning() + { +#if WINDOWS_UWP + return (Window.Current?.Content as Frame)?.Content != null; +#endif + + return true; + } + public async Task LaunchFileAsync(string filePath) { diff --git a/Wino.Mail/Services/PreferencesService.cs b/Wino.Core.UWP/Services/PreferencesService.cs similarity index 99% rename from Wino.Mail/Services/PreferencesService.cs rename to Wino.Core.UWP/Services/PreferencesService.cs index 7b2fe0fa..173d7f22 100644 --- a/Wino.Mail/Services/PreferencesService.cs +++ b/Wino.Core.UWP/Services/PreferencesService.cs @@ -7,7 +7,7 @@ using Wino.Core.Domain.Interfaces; using Wino.Core.Domain.Models.Reader; using Wino.Core.Services; -namespace Wino.Services +namespace Wino.Core.UWP.Services { public class PreferencesService : ObservableObject, IPreferencesService { diff --git a/Wino.Mail/Services/StatePersistenceService.cs b/Wino.Core.UWP/Services/StatePersistenceService.cs similarity index 90% rename from Wino.Mail/Services/StatePersistenceService.cs rename to Wino.Core.UWP/Services/StatePersistenceService.cs index 4c995ef9..e94067ed 100644 --- a/Wino.Mail/Services/StatePersistenceService.cs +++ b/Wino.Core.UWP/Services/StatePersistenceService.cs @@ -2,7 +2,6 @@ using System.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; -using Microsoft.AppCenter.Crashes; using Wino.Core.Domain.Interfaces; using Wino.Core.Messages.Shell; @@ -116,17 +115,10 @@ namespace Wino.Services private void UpdateAppCoreWindowTitle() { - try - { - var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView(); + var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView(); - if (appView != null) - appView.Title = CoreWindowTitle; - } - catch (System.Exception ex) - { - Crashes.TrackError(ex); - } + if (appView != null) + appView.Title = CoreWindowTitle; } } } diff --git a/Wino.Core.UWP/Services/WinoServerConnectionManager.cs b/Wino.Core.UWP/Services/WinoServerConnectionManager.cs index 80a21ee3..2d382d2c 100644 --- a/Wino.Core.UWP/Services/WinoServerConnectionManager.cs +++ b/Wino.Core.UWP/Services/WinoServerConnectionManager.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Text.Json; using System.Threading.Tasks; using CommunityToolkit.Mvvm.Messaging; @@ -140,6 +141,8 @@ namespace Wino.Core.UWP.Services /// Message data in json format. private void HandleUIMessage(string messageJson, string typeName) { + Debug.WriteLine($"C: UImessage ({typeName})"); + switch (typeName) { case nameof(MailAddedMessage): diff --git a/Wino.Core.UWP/Wino.Core.UWP.csproj b/Wino.Core.UWP/Wino.Core.UWP.csproj index f90c481e..04ef24b8 100644 --- a/Wino.Core.UWP/Wino.Core.UWP.csproj +++ b/Wino.Core.UWP/Wino.Core.UWP.csproj @@ -128,7 +128,8 @@ - + + diff --git a/Wino.Core/Authenticators/OutlookAuthenticator.cs b/Wino.Core/Authenticators/OutlookAuthenticator.cs index 878d1c91..72e5d847 100644 --- a/Wino.Core/Authenticators/OutlookAuthenticator.cs +++ b/Wino.Core/Authenticators/OutlookAuthenticator.cs @@ -29,10 +29,17 @@ namespace Wino.Core.Authenticators { var authenticationRedirectUri = nativeAppService.GetWebAuthenticationBrokerUri(); - _publicClientApplication = PublicClientApplicationBuilder.Create(ClientId) - .WithAuthority(Authority) - .WithRedirectUri(authenticationRedirectUri) - .Build(); + var outlookAppBuilder = PublicClientApplicationBuilder.Create(ClientId) + .WithAuthority(Authority); + +#if WINDOWS_UWP + outlookAppBuilder.WithRedirectUri(authenticationRedirectUri); +#else + outlookAppBuilder.WithDefaultRedirectUri(); +#endif + _publicClientApplication = outlookAppBuilder.Build(); + + } #pragma warning disable S1133 // Deprecated code should be removed diff --git a/Wino.Core/CoreContainerSetup.cs b/Wino.Core/CoreContainerSetup.cs index 0a89f203..6047a089 100644 --- a/Wino.Core/CoreContainerSetup.cs +++ b/Wino.Core/CoreContainerSetup.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using Serilog.Core; +using Wino.Core.Authenticators; using Wino.Core.Domain.Interfaces; using Wino.Core.Integration.Processors; using Wino.Core.Integration.Threading; @@ -16,6 +17,7 @@ namespace Wino.Core services.AddSingleton(loggerLevelSwitcher); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -41,6 +43,10 @@ namespace Wino.Core services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/Wino.Core/Services/ApplicationConfiguration.cs b/Wino.Core/Services/ApplicationConfiguration.cs new file mode 100644 index 00000000..e920a12e --- /dev/null +++ b/Wino.Core/Services/ApplicationConfiguration.cs @@ -0,0 +1,13 @@ +using Wino.Core.Domain.Interfaces; + +namespace Wino.Core.Services +{ + public class ApplicationConfiguration : IApplicationConfiguration + { + public const string SharedFolderName = "WinoShared"; + + public string ApplicationDataFolderPath { get; set; } + + public string PublisherSharedFolderPath { get; set; } + } +} diff --git a/Wino.Core/Services/DatabaseService.cs b/Wino.Core/Services/DatabaseService.cs index 8abac819..246f9a63 100644 --- a/Wino.Core/Services/DatabaseService.cs +++ b/Wino.Core/Services/DatabaseService.cs @@ -14,16 +14,16 @@ namespace Wino.Core.Services public class DatabaseService : IDatabaseService { - private string DatabaseName => "Wino172.db"; + private const string DatabaseName = "Wino172.db"; private bool _isInitialized = false; - private readonly IAppInitializerService _appInitializerService; + private readonly IApplicationConfiguration _folderConfiguration; public SQLiteAsyncConnection Connection { get; private set; } - public DatabaseService(IAppInitializerService appInitializerService) + public DatabaseService(IApplicationConfiguration folderConfiguration) { - _appInitializerService = appInitializerService; + _folderConfiguration = folderConfiguration; } public async Task InitializeAsync() @@ -31,8 +31,8 @@ namespace Wino.Core.Services if (_isInitialized) return; - var applicationData = _appInitializerService.GetPublisherSharedFolder(); - var databaseFileName = Path.Combine(applicationData, DatabaseName); + var publisherCacheFolder = _folderConfiguration.PublisherSharedFolderPath; + var databaseFileName = Path.Combine(publisherCacheFolder, DatabaseName); Connection = new SQLiteAsyncConnection(databaseFileName) { @@ -45,7 +45,6 @@ namespace Wino.Core.Services }) }; - await CreateTablesAsync(); _isInitialized = true; diff --git a/Wino.Core/Services/ImapTestService.cs b/Wino.Core/Services/ImapTestService.cs index b9cc54bf..ae7f92c3 100644 --- a/Wino.Core/Services/ImapTestService.cs +++ b/Wino.Core/Services/ImapTestService.cs @@ -11,11 +11,11 @@ namespace Wino.Core.Services public const string ProtocolLogFileName = "ImapProtocolLog.log"; private readonly IPreferencesService _preferencesService; - private readonly IAppInitializerService _appInitializerService; + private readonly IApplicationConfiguration _appInitializerService; private Stream _protocolLogStream; - public ImapTestService(IPreferencesService preferencesService, IAppInitializerService appInitializerService) + public ImapTestService(IPreferencesService preferencesService, IApplicationConfiguration appInitializerService) { _preferencesService = preferencesService; _appInitializerService = appInitializerService; @@ -24,7 +24,7 @@ namespace Wino.Core.Services private void EnsureProtocolLogFileExists() { // Create new file for protocol logger. - var localAppFolderPath = _appInitializerService.GetApplicationDataFolder(); + var localAppFolderPath = _appInitializerService.ApplicationDataFolderPath; var logFile = Path.Combine(localAppFolderPath, ProtocolLogFileName); diff --git a/Wino.Mail.ViewModels/AboutPageViewModel.cs b/Wino.Mail.ViewModels/AboutPageViewModel.cs index 3b363854..a8403a1a 100644 --- a/Wino.Mail.ViewModels/AboutPageViewModel.cs +++ b/Wino.Mail.ViewModels/AboutPageViewModel.cs @@ -12,7 +12,7 @@ namespace Wino.Mail.ViewModels { private readonly IStoreRatingService _storeRatingService; private readonly INativeAppService _nativeAppService; - private readonly IAppInitializerService _appInitializerService; + private readonly IApplicationConfiguration _appInitializerService; private readonly IFileService _fileService; private readonly ILogInitializer _logInitializer; @@ -31,7 +31,7 @@ namespace Wino.Mail.ViewModels IDialogService dialogService, INativeAppService nativeAppService, IPreferencesService preferencesService, - IAppInitializerService appInitializerService, + IApplicationConfiguration appInitializerService, IFileService fileService, ILogInitializer logInitializer) : base(dialogService) { @@ -77,7 +77,7 @@ namespace Wino.Mail.ViewModels private async Task SaveLogInternalAsync(string sourceFileName) { - var appDataFolder = _appInitializerService.GetApplicationDataFolder(); + var appDataFolder = _appInitializerService.ApplicationDataFolderPath; var logFile = Path.Combine(appDataFolder, sourceFileName); diff --git a/Wino.Mail.ViewModels/AppShellViewModel.cs b/Wino.Mail.ViewModels/AppShellViewModel.cs index 96226233..b5c9d16d 100644 --- a/Wino.Mail.ViewModels/AppShellViewModel.cs +++ b/Wino.Mail.ViewModels/AppShellViewModel.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using Microsoft.AppCenter.Crashes; using MoreLinq; @@ -129,6 +130,9 @@ namespace Wino.Mail.ViewModels _winoRequestDelegator = winoRequestDelegator; } + [RelayCommand] + private Task ReconnectServerAsync() => ServerConnectionManager.ConnectAsync(); + protected override void OnDispatcherAssigned() { base.OnDispatcherAssigned(); diff --git a/Wino.Mail/App.xaml.cs b/Wino.Mail/App.xaml.cs index 02de9c11..b6243f88 100644 --- a/Wino.Mail/App.xaml.cs +++ b/Wino.Mail/App.xaml.cs @@ -45,8 +45,9 @@ namespace Wino private readonly ILogInitializer _logInitializer; private readonly IThemeService _themeService; private readonly IDatabaseService _databaseService; - private readonly IAppInitializerService _appInitializerService; + private readonly IApplicationConfiguration _appInitializerService; private readonly ITranslationService _translationService; + private readonly IApplicationConfiguration _applicationFolderConfiguration; // Order matters. private List initializeServices => new List() @@ -77,10 +78,16 @@ namespace Wino ConfigurePrelaunch(); ConfigureXbox(); + _applicationFolderConfiguration = Services.GetService(); + + // Make sure the paths are setup on app start. + _applicationFolderConfiguration.ApplicationDataFolderPath = ApplicationData.Current.LocalFolder.Path; + _applicationFolderConfiguration.PublisherSharedFolderPath = ApplicationData.Current.GetPublisherCacheFolder(ApplicationConfiguration.SharedFolderName).Path; + _appServiceConnectionManager = Services.GetService>(); _themeService = Services.GetService(); _databaseService = Services.GetService(); - _appInitializerService = Services.GetService(); + _appInitializerService = Services.GetService(); _translationService = Services.GetService(); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); diff --git a/Wino.Mail/AppShell.xaml b/Wino.Mail/AppShell.xaml index 031e964c..cfb36585 100644 --- a/Wino.Mail/AppShell.xaml +++ b/Wino.Mail/AppShell.xaml @@ -521,7 +521,7 @@ Padding="14" HorizontalAlignment="Right" VerticalAlignment="Bottom"> - +