-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
25 changed files
with
4,394 additions
and
3,207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,73 @@ | ||
using barcodrod.io.Activation; | ||
using barcodrod.io.Contracts.Services; | ||
using barcodrod.io.Views; | ||
|
||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace barcodrod.io.Services; | ||
|
||
public class ActivationService : IActivationService | ||
{ | ||
private readonly ActivationHandler<LaunchActivatedEventArgs> _defaultHandler; | ||
private readonly IEnumerable<IActivationHandler> _activationHandlers; | ||
private readonly IThemeSelectorService _themeSelectorService; | ||
private UIElement? _shell = null; | ||
|
||
public ActivationService(ActivationHandler<LaunchActivatedEventArgs> defaultHandler, IEnumerable<IActivationHandler> activationHandlers, IThemeSelectorService themeSelectorService) | ||
{ | ||
_defaultHandler = defaultHandler; | ||
_activationHandlers = activationHandlers; | ||
_themeSelectorService = themeSelectorService; | ||
} | ||
|
||
public async Task ActivateAsync(object activationArgs) | ||
{ | ||
// Execute tasks before activation. | ||
await InitializeAsync(); | ||
|
||
// Set the MainWindow Content. | ||
if (App.MainWindow.Content == null) | ||
{ | ||
_shell = App.GetService<ShellPage>(); | ||
App.MainWindow.Content = _shell ?? new Frame(); | ||
} | ||
|
||
|
||
// Handle activation via ActivationHandlers. | ||
await HandleActivationAsync(activationArgs); | ||
|
||
// Activate the MainWindow. | ||
App.MainWindow.Activate(); | ||
|
||
// Execute tasks after activation. | ||
await StartupAsync(); | ||
} | ||
|
||
private async Task HandleActivationAsync(object activationArgs) | ||
{ | ||
var activationHandler = _activationHandlers.FirstOrDefault(h => h.CanHandle(activationArgs)); | ||
|
||
if (activationHandler != null) | ||
{ | ||
await activationHandler.HandleAsync(activationArgs); | ||
} | ||
|
||
if (_defaultHandler.CanHandle(activationArgs)) | ||
{ | ||
await _defaultHandler.HandleAsync(activationArgs); | ||
} | ||
} | ||
|
||
private async Task InitializeAsync() | ||
{ | ||
await _themeSelectorService.InitializeAsync().ConfigureAwait(false); | ||
await Task.CompletedTask; | ||
} | ||
|
||
private async Task StartupAsync() | ||
{ | ||
await _themeSelectorService.SetRequestedThemeAsync(); | ||
await Task.CompletedTask; | ||
} | ||
} | ||
using barcodrod.io.Activation; | ||
using barcodrod.io.Contracts.Services; | ||
using barcodrod.io.Views; | ||
|
||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace barcodrod.io.Services; | ||
|
||
public class ActivationService : IActivationService | ||
{ | ||
private readonly ActivationHandler<LaunchActivatedEventArgs> _defaultHandler; | ||
private readonly IEnumerable<IActivationHandler> _activationHandlers; | ||
private readonly IThemeSelectorService _themeSelectorService; | ||
private UIElement? _shell = null; | ||
|
||
public ActivationService(ActivationHandler<LaunchActivatedEventArgs> defaultHandler, IEnumerable<IActivationHandler> activationHandlers, IThemeSelectorService themeSelectorService) | ||
{ | ||
_defaultHandler = defaultHandler; | ||
_activationHandlers = activationHandlers; | ||
_themeSelectorService = themeSelectorService; | ||
} | ||
|
||
public async Task ActivateAsync(object activationArgs) | ||
{ | ||
// Execute tasks before activation. | ||
await InitializeAsync(); | ||
|
||
// Set the MainWindow Content. | ||
if (App.MainWindow.Content == null) | ||
{ | ||
_shell = App.GetService<ShellPage>(); | ||
App.MainWindow.Content = _shell ?? new Frame(); | ||
} | ||
|
||
|
||
// Handle activation via ActivationHandlers. | ||
await HandleActivationAsync(activationArgs); | ||
|
||
// Activate the MainWindow. | ||
App.MainWindow.Activate(); | ||
|
||
// Execute tasks after activation. | ||
await StartupAsync(); | ||
} | ||
|
||
private async Task HandleActivationAsync(object activationArgs) | ||
{ | ||
var activationHandler = _activationHandlers.FirstOrDefault(h => h.CanHandle(activationArgs)); | ||
|
||
if (activationHandler != null) | ||
{ | ||
await activationHandler.HandleAsync(activationArgs); | ||
} | ||
|
||
if (_defaultHandler.CanHandle(activationArgs)) | ||
{ | ||
await _defaultHandler.HandleAsync(activationArgs); | ||
} | ||
} | ||
|
||
private async Task InitializeAsync() | ||
{ | ||
await _themeSelectorService.InitializeAsync().ConfigureAwait(false); | ||
await Task.CompletedTask; | ||
} | ||
|
||
private async Task StartupAsync() | ||
{ | ||
await _themeSelectorService.SetRequestedThemeAsync(); | ||
await Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,86 @@ | ||
using barcodrod.io.Contracts.Services; | ||
using barcodrod.io.Core.Contracts.Services; | ||
using barcodrod.io.Core.Helpers; | ||
using barcodrod.io.Helpers; | ||
using barcodrod.io.Models; | ||
|
||
using Microsoft.Extensions.Options; | ||
using Windows.Storage; | ||
|
||
namespace barcodrod.io.Services; | ||
|
||
public class LocalSettingsService : ILocalSettingsService | ||
{ | ||
private const string _defaultApplicationDataFolder = "barcodrod.io/ApplicationData"; | ||
private const string _defaultLocalSettingsFile = "LocalSettings.json"; | ||
|
||
private readonly IFileService _fileService; | ||
private readonly LocalSettingsOptions _options; | ||
|
||
private readonly string _localApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); | ||
private readonly string _applicationDataFolder; | ||
private readonly string _localsettingsFile; | ||
|
||
private IDictionary<string, object> _settings; | ||
|
||
private bool _isInitialized; | ||
|
||
public LocalSettingsService(IFileService fileService, IOptions<LocalSettingsOptions> options) | ||
{ | ||
_fileService = fileService; | ||
_options = options.Value; | ||
|
||
_applicationDataFolder = Path.Combine(_localApplicationData, _options.ApplicationDataFolder ?? _defaultApplicationDataFolder); | ||
_localsettingsFile = _options.LocalSettingsFile ?? _defaultLocalSettingsFile; | ||
|
||
_settings = new Dictionary<string, object>(); | ||
} | ||
|
||
private async Task InitializeAsync() | ||
{ | ||
if (!_isInitialized) | ||
{ | ||
_settings = await Task.Run(() => _fileService.Read<IDictionary<string, object>>(_applicationDataFolder, _localsettingsFile)) ?? new Dictionary<string, object>(); | ||
|
||
_isInitialized = true; | ||
} | ||
} | ||
|
||
public async Task<T?> ReadSettingAsync<T>(string key) | ||
{ | ||
if (RuntimeHelper.IsMSIX) | ||
{ | ||
if (ApplicationData.Current.LocalSettings.Values.TryGetValue(key, out var obj)) | ||
{ | ||
return await Json.ToObjectAsync<T>((string)obj); | ||
} | ||
} | ||
else | ||
{ | ||
await InitializeAsync(); | ||
|
||
if (_settings != null && _settings.TryGetValue(key, out var obj)) | ||
{ | ||
return await Json.ToObjectAsync<T>((string)obj); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public async Task SaveSettingAsync<T>(string key, T value) | ||
{ | ||
if (RuntimeHelper.IsMSIX) | ||
{ | ||
ApplicationData.Current.LocalSettings.Values[key] = await Json.StringifyAsync(value); | ||
} | ||
else | ||
{ | ||
await InitializeAsync(); | ||
|
||
_settings[key] = await Json.StringifyAsync(value); | ||
|
||
await Task.Run(() => _fileService.Save(_applicationDataFolder, _localsettingsFile, _settings)); | ||
} | ||
} | ||
} | ||
using barcodrod.io.Contracts.Services; | ||
using barcodrod.io.Core.Contracts.Services; | ||
using barcodrod.io.Core.Helpers; | ||
using barcodrod.io.Helpers; | ||
using barcodrod.io.Models; | ||
|
||
using Microsoft.Extensions.Options; | ||
using Windows.Storage; | ||
|
||
namespace barcodrod.io.Services; | ||
|
||
public class LocalSettingsService : ILocalSettingsService | ||
{ | ||
private const string _defaultApplicationDataFolder = "barcodrod.io/ApplicationData"; | ||
private const string _defaultLocalSettingsFile = "LocalSettings.json"; | ||
|
||
private readonly IFileService _fileService; | ||
private readonly LocalSettingsOptions _options; | ||
|
||
private readonly string _localApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); | ||
private readonly string _applicationDataFolder; | ||
private readonly string _localsettingsFile; | ||
|
||
private IDictionary<string, object> _settings; | ||
|
||
private bool _isInitialized; | ||
|
||
public LocalSettingsService(IFileService fileService, IOptions<LocalSettingsOptions> options) | ||
{ | ||
_fileService = fileService; | ||
_options = options.Value; | ||
|
||
_applicationDataFolder = Path.Combine(_localApplicationData, _options.ApplicationDataFolder ?? _defaultApplicationDataFolder); | ||
_localsettingsFile = _options.LocalSettingsFile ?? _defaultLocalSettingsFile; | ||
|
||
_settings = new Dictionary<string, object>(); | ||
} | ||
|
||
private async Task InitializeAsync() | ||
{ | ||
if (!_isInitialized) | ||
{ | ||
_settings = await Task.Run(() => _fileService.Read<IDictionary<string, object>>(_applicationDataFolder, _localsettingsFile)) ?? new Dictionary<string, object>(); | ||
|
||
_isInitialized = true; | ||
} | ||
} | ||
|
||
public async Task<T?> ReadSettingAsync<T>(string key) | ||
{ | ||
if (RuntimeHelper.IsMSIX) | ||
{ | ||
if (ApplicationData.Current.LocalSettings.Values.TryGetValue(key, out var obj)) | ||
{ | ||
return await Json.ToObjectAsync<T>((string)obj); | ||
} | ||
} | ||
else | ||
{ | ||
await InitializeAsync(); | ||
|
||
if (_settings != null && _settings.TryGetValue(key, out var obj)) | ||
{ | ||
return await Json.ToObjectAsync<T>((string)obj); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public async Task SaveSettingAsync<T>(string key, T value) | ||
{ | ||
if (RuntimeHelper.IsMSIX) | ||
{ | ||
ApplicationData.Current.LocalSettings.Values[key] = await Json.StringifyAsync(value); | ||
} | ||
else | ||
{ | ||
await InitializeAsync(); | ||
|
||
_settings[key] = await Json.StringifyAsync(value); | ||
|
||
await Task.Run(() => _fileService.Save(_applicationDataFolder, _localsettingsFile, _settings)); | ||
} | ||
} | ||
} |
Oops, something went wrong.