Skip to content

Commit

Permalink
Load user settings from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Apr 24, 2024
1 parent 9885b00 commit fcac992
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public override async void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
var initializerManager = _serviceProvider.GetRequiredService<InitializerManager>();
var userManager = _serviceProvider.GetRequiredService<UserManager>();
await userManager.LoadAsync();
initializerManager.InitializeLocale();

var userManager = _serviceProvider.GetRequiredService<UserManager>();
var localeManager = _serviceProvider.GetRequiredService<ILocaleManager>();
var locale = userManager.UserSettings?.Locale?.Key!;

Expand Down
74 changes: 45 additions & 29 deletions src/ImeSense.Launchers.Belarus.Core/Manager/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,20 @@

namespace ImeSense.Launchers.Belarus.Core.Manager;

public class UserManager : ReactiveObject
public class UserManager(ILogger<UserManager>? logger,
IAuthenticationValidator authenticationValidator,
IStartGameValidator startGameValidator,
ILauncherStorage launcherStorage) : ReactiveObject
{
private readonly ILogger<UserManager>? _logger;
private readonly IAuthenticationValidator _authenticationValidator;
private readonly IStartGameValidator _startGameValidator;
private readonly ILauncherStorage _launcherStorage;
private readonly ILogger<UserManager>? _logger = logger;
private readonly IAuthenticationValidator _authenticationValidator = authenticationValidator;
private readonly IStartGameValidator _startGameValidator = startGameValidator;
private readonly ILauncherStorage _launcherStorage = launcherStorage;

[Reactive]
public UserSettings? UserSettings { get; set; }

public UserManager(ILogger<UserManager>? logger, IAuthenticationValidator authenticationValidator, IStartGameValidator startGameValidator, ILauncherStorage launcherStorage)
{
_logger = logger;
_authenticationValidator = authenticationValidator;
_startGameValidator = startGameValidator;
_launcherStorage = launcherStorage;
Load();
}

private UserSettings CreateDefaultUserSettings() {
var userSettings = new UserSettings();
var systemCulture = CultureInfo.CurrentCulture;
if (systemCulture.ThreeLetterISOLanguageName.Equals(_launcherStorage.Locales[0].Key)) {
userSettings.Locale = _launcherStorage.Locales[0];
} else {
userSettings.Locale = _launcherStorage.Locales[1];
}
_logger?.LogInformation("Set locale: {locale}", userSettings.Locale.Title);

return userSettings;
}

private void Load()
public async Task LoadAsync()
{
if (!File.Exists(PathStorage.LauncherSetting)) {
UserSettings = CreateDefaultUserSettings();
Expand All @@ -53,7 +34,8 @@ private void Load()

try {
using var json = File.OpenRead(PathStorage.LauncherSetting);
var user = JsonSerializer.Deserialize(json, SourceGenerationContext.Default.UserSettings)!;
var user = await JsonSerializer.DeserializeAsync(json, SourceGenerationContext.Default.UserSettings);
user ??= CreateDefaultUserSettings();

if (!_startGameValidator.IsValidIpAddressOrUrl(user.IpAddress)) {
user.IpAddress = string.Empty;
Expand Down Expand Up @@ -91,4 +73,38 @@ public void Save()
var json = JsonSerializer.Serialize(UserSettings, typeof(UserSettings), SourceGenerationContext.Default);
writer.Write(json);
}

public async Task SaveAsync()
{
if (UserSettings is null) {
throw new ArgumentNullException(nameof(UserSettings));
}
if (string.IsNullOrEmpty(UserSettings.Username)) {
throw new Exception("Username not specified");
}

if (!Directory.Exists(DirectoryStorage.User)) {
Directory.CreateDirectory(DirectoryStorage.User);
}

using var fileStream = new FileStream(PathStorage.LauncherSetting,
FileMode.Create);
using var writer = new StreamWriter(fileStream);

await JsonSerializer.SerializeAsync(fileStream, UserSettings, typeof(UserSettings), SourceGenerationContext.Default);
}

private UserSettings CreateDefaultUserSettings()
{
var userSettings = new UserSettings();
var systemCulture = CultureInfo.CurrentCulture;
if (systemCulture.ThreeLetterISOLanguageName.Equals(_launcherStorage.Locales[0].Key)) {
userSettings.Locale = _launcherStorage.Locales[0];
} else {
userSettings.Locale = _launcherStorage.Locales[1];
}
_logger?.LogInformation("Set locale: {locale}", userSettings.Locale.Title);

return userSettings;
}
}

0 comments on commit fcac992

Please sign in to comment.