From fee963f8bd6c70f090e43aec5842dab3032192ff Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 2 Aug 2022 20:41:55 +0200 Subject: [PATCH] Fixed culture setting handling for non-continuous indices --- I18N Commander/Processor/AppSettings.cs | 36 +++++++++---------- .../UI WinForms/Components/Setting.cs | 22 +++++++----- .../UI WinForms/Components/Settings.cs | 6 ++-- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs index 047897f..edd48cb 100644 --- a/I18N Commander/Processor/AppSettings.cs +++ b/I18N Commander/Processor/AppSettings.cs @@ -288,29 +288,30 @@ public static class AppSettings #region Number of cultures - private static int CACHE_NUMBER_OF_CULTURES = -1; + private static List CACHE_CULTURES_INDICES = new(); - public static async Task GetNumberCultures() + public static async Task> GetCultureIndices() { // When possible, use the cache: - if (CACHE_NUMBER_OF_CULTURES > -1) - return CACHE_NUMBER_OF_CULTURES; + if (CACHE_CULTURES_INDICES.Count > 0) + return CACHE_CULTURES_INDICES; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Count the number of cultures: - var count = await db.Settings.CountAsync(n => n.Code.StartsWith(SettingNames.CULTURE)); + var list = await db.Settings.Where(n => n.Code.StartsWith(SettingNames.CULTURE)).OrderBy(n => n.IntegerValue).Select(n => n.IntegerValue).ToListAsync(); // We have at least one default culture: - if(count == 0) + if(list.Count == 0) { - CACHE_NUMBER_OF_CULTURES = 1; - return 1; + // Add the default culture, which is en-US: + await AppSettings.SetCultureCode(1, "en-US"); } - - CACHE_NUMBER_OF_CULTURES = count; - return count; + else + CACHE_CULTURES_INDICES = list; + + return CACHE_CULTURES_INDICES; } #endregion @@ -377,9 +378,8 @@ public static class AppSettings await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); - // Update the number of cultures cache: - if(CACHE_NUMBER_OF_CULTURES > -1) - CACHE_NUMBER_OF_CULTURES++; + // Update the list of cultures indices: + CACHE_CULTURES_INDICES.Add(index); } } @@ -392,15 +392,15 @@ public static class AppSettings public static async Task> GetCultureInfos() { // Get the number of cultures: - var numberOfCultures = await AppSettings.GetNumberCultures(); + var cultureIndices = await AppSettings.GetCultureIndices(); // Get the culture codes: var cultureInfos = new List(); - for (var i = 1; i <= numberOfCultures; i++) + foreach (var cultureIndex in cultureIndices) cultureInfos.Add(new CultureInfo { - Code = await AppSettings.GetCultureCode(i), - Index = i, + Code = await AppSettings.GetCultureCode(cultureIndex), + Index = cultureIndex, }); return cultureInfos; diff --git a/I18N Commander/UI WinForms/Components/Setting.cs b/I18N Commander/UI WinForms/Components/Setting.cs index 87dc1a4..0d08121 100644 --- a/I18N Commander/UI WinForms/Components/Setting.cs +++ b/I18N Commander/UI WinForms/Components/Setting.cs @@ -153,23 +153,27 @@ public sealed partial class Setting : UserControl private static IEnumerable> ShowCultureSettingsAsync() { var isFirstCulture = true; // We need this flag to distinguish the first task from the others. - var numberOfCultures = int.MaxValue; // There is always at least one culture, which is the default culture. We update this value later, out of the tasks. - while (numberOfCultures > 0) + var cultureIndices = new List(new []{ -1 }); + while (cultureIndices.Count > 0) { - var innerLoopIndex = numberOfCultures; // needed to avoid closure issues. + var innerLoopIndex = cultureIndices.Last(); // needed to avoid closure issues. yield return Task.Run(async () => { var localCultureIndex = innerLoopIndex; - // Get the total number of cultures. We cannot do this in the outer loop, - // because we cannot await there. The AppSettings is caching the answer, though. - var numberCultures = await AppSettings.GetNumberCultures(); + // Get a list of culture indices. Thus, we know the number of cultures. We cannot do this in the outer loop, + // because we cannot await there. The AppSettings is caching the answer, though. The list of indices is ordered + // ascending. + var localCultureIndices = await AppSettings.GetCultureIndices(); // Update the number of cultures in the outer loop for the first call: if(isFirstCulture) { - localCultureIndex = numberCultures; - numberOfCultures = numberCultures; + localCultureIndex = localCultureIndices.Last(); + innerLoopIndex = localCultureIndices.Last(); + + cultureIndices.Clear(); + cultureIndices.AddRange(localCultureIndices); isFirstCulture = false; } @@ -196,7 +200,7 @@ public sealed partial class Setting : UserControl }); }); - numberOfCultures--; + cultureIndices.Remove(innerLoopIndex); } } diff --git a/I18N Commander/UI WinForms/Components/Settings.cs b/I18N Commander/UI WinForms/Components/Settings.cs index cb6f551..ba36e48 100644 --- a/I18N Commander/UI WinForms/Components/Settings.cs +++ b/I18N Commander/UI WinForms/Components/Settings.cs @@ -19,11 +19,11 @@ public partial class Settings : UserControl private async void buttonAddCulture_Click(object sender, EventArgs e) { - // Get the current number of cultures: - var numberCultures = await AppSettings.GetNumberCultures(); + // Get the current indices of cultures: + var cultureIndices = await AppSettings.GetCultureIndices(); // Add a new culture: - await AppSettings.SetCultureCode(++numberCultures, string.Empty); + await AppSettings.SetCultureCode(cultureIndices.Max() + 1, string.Empty); // Reload all settings: await this.LoadAllSettings();