Skip to content

Commit

Permalink
Fix excessive memory allocation in AxamlLocaleManager class
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Apr 24, 2024
1 parent 7105689 commit 5cab761
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml.Styling;

using ImeSense.Launchers.Belarus.Core.Manager;

using Microsoft.Extensions.Logging;

using ReactiveUI;
using ReactiveUI.Fody.Helpers;

namespace ImeSense.Launchers.Belarus.Avalonia.Manager;

public class AxamlLocaleManager : ReactiveObject, IApplicationLocaleManager
public class AxamlLocaleManager(ILogger<AxamlLocaleManager> logger) : ReactiveObject, IApplicationLocaleManager
{
[Reactive]
public string Locale { get; private set; } = string.Empty;
private readonly ILogger<AxamlLocaleManager> _logger = logger;
private ResourceInclude? _resources;

[Reactive] public string Locale { get; private set; } = string.Empty;

public void SetLocale(string locale)
{
Locale = locale;

App.Current?.Resources.Clear();
var resource = new ResourceInclude(new Uri("avares://SBLauncher/Assets/Locales/")) {
Source = new Uri($"avares://SBLauncher/Assets/Locales/{Locale}.axaml"),
};
App.Current?.Resources.MergedDictionaries.Add(resource);
LoadLocalizedResources();
}

private void LoadLocalizedResources()
{
try {
_resources = new ResourceInclude(new Uri("avares://SBLauncher/Assets/Locales/")) {

Check warning on line 30 in src/ImeSense.Launchers.Belarus.Avalonia/Manager/AxamlLocaleManager.cs

View workflow job for this annotation

GitHub Actions / Publish launcher (windows-2022, Release, win-x64, net8.0)

Using member 'Avalonia.Markup.Xaml.Styling.ResourceInclude.ResourceInclude(Uri)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. StyleInclude and ResourceInclude use AvaloniaXamlLoader.Load which dynamically loads referenced assembly with Avalonia resources. Note, StyleInclude and ResourceInclude defined in XAML are resolved compile time and are safe with trimming and AOT.
Source = new Uri($"avares://SBLauncher/Assets/Locales/{Locale}.axaml")
};
App.Current?.Resources.MergedDictionaries.Add(_resources);
} catch (Exception ex) {
_logger.LogError(ex, "Failed to load localized resources for locale: {Locale}", Locale);
throw;
}
}

public string GetStringByKey(string key)
{
var resources = new ResourceInclude(new Uri("avares://SBLauncher/Assets/Locales/")) {
Source = new Uri($"avares://SBLauncher/Assets/Locales/{Locale}.axaml"),
};
var control = new Control {
Resources = resources.Loaded,
};
if (control.TryFindResource(key, out var value)) {
if (_resources is null) {
_logger.LogError("Resource include is not initialized");
return string.Empty;
}

var resources = _resources.Loaded;
if (resources.TryGetValue(key, out var value)) {
return (string) value!;
} else {
_logger.LogError("Resource with key '{Key}' not found for locale '{Locale}'", key, Locale);
return string.Empty;
}
return string.Empty;
}
}
2 changes: 1 addition & 1 deletion src/ImeSense.Launchers.Belarus.Core/Manager/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UserManager(ILogger<UserManager>? logger,
private readonly IStartGameValidator _startGameValidator = startGameValidator;
private readonly ILauncherStorage _launcherStorage = launcherStorage;

public UserSettings? UserSettings { get; set; }
public UserSettings? UserSettings { get; private set; }

public async Task LoadAsync()
{
Expand Down

0 comments on commit 5cab761

Please sign in to comment.