Fixed culture setting handling for non-continuous indices

This commit is contained in:
Thorsten Sommer 2022-08-02 20:41:55 +02:00
parent fd3d844df4
commit fee963f8bd
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 34 additions and 30 deletions

View File

@ -288,29 +288,30 @@ public static class AppSettings
#region Number of cultures #region Number of cultures
private static int CACHE_NUMBER_OF_CULTURES = -1; private static List<int> CACHE_CULTURES_INDICES = new();
public static async Task<int> GetNumberCultures() public static async Task<List<int>> GetCultureIndices()
{ {
// When possible, use the cache: // When possible, use the cache:
if (CACHE_NUMBER_OF_CULTURES > -1) if (CACHE_CULTURES_INDICES.Count > 0)
return CACHE_NUMBER_OF_CULTURES; return CACHE_CULTURES_INDICES;
// Get the database: // Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>(); await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Count the number of cultures: // 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: // We have at least one default culture:
if(count == 0) if(list.Count == 0)
{ {
CACHE_NUMBER_OF_CULTURES = 1; // Add the default culture, which is en-US:
return 1; await AppSettings.SetCultureCode(1, "en-US");
} }
else
CACHE_NUMBER_OF_CULTURES = count; CACHE_CULTURES_INDICES = list;
return count;
return CACHE_CULTURES_INDICES;
} }
#endregion #endregion
@ -377,9 +378,8 @@ public static class AppSettings
await db.Settings.AddAsync(setting); await db.Settings.AddAsync(setting);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
// Update the number of cultures cache: // Update the list of cultures indices:
if(CACHE_NUMBER_OF_CULTURES > -1) CACHE_CULTURES_INDICES.Add(index);
CACHE_NUMBER_OF_CULTURES++;
} }
} }
@ -392,15 +392,15 @@ public static class AppSettings
public static async Task<List<CultureInfo>> GetCultureInfos() public static async Task<List<CultureInfo>> GetCultureInfos()
{ {
// Get the number of cultures: // Get the number of cultures:
var numberOfCultures = await AppSettings.GetNumberCultures(); var cultureIndices = await AppSettings.GetCultureIndices();
// Get the culture codes: // Get the culture codes:
var cultureInfos = new List<CultureInfo>(); var cultureInfos = new List<CultureInfo>();
for (var i = 1; i <= numberOfCultures; i++) foreach (var cultureIndex in cultureIndices)
cultureInfos.Add(new CultureInfo cultureInfos.Add(new CultureInfo
{ {
Code = await AppSettings.GetCultureCode(i), Code = await AppSettings.GetCultureCode(cultureIndex),
Index = i, Index = cultureIndex,
}); });
return cultureInfos; return cultureInfos;

View File

@ -153,23 +153,27 @@ public sealed partial class Setting : UserControl
private static IEnumerable<Task<Setting>> ShowCultureSettingsAsync() private static IEnumerable<Task<Setting>> ShowCultureSettingsAsync()
{ {
var isFirstCulture = true; // We need this flag to distinguish the first task from the others. 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. var cultureIndices = new List<int>(new []{ -1 });
while (numberOfCultures > 0) 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 () => yield return Task.Run(async () =>
{ {
var localCultureIndex = innerLoopIndex; var localCultureIndex = innerLoopIndex;
// Get the total number of cultures. We cannot do this in the outer loop, // 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. // because we cannot await there. The AppSettings is caching the answer, though. The list of indices is ordered
var numberCultures = await AppSettings.GetNumberCultures(); // ascending.
var localCultureIndices = await AppSettings.GetCultureIndices();
// Update the number of cultures in the outer loop for the first call: // Update the number of cultures in the outer loop for the first call:
if(isFirstCulture) if(isFirstCulture)
{ {
localCultureIndex = numberCultures; localCultureIndex = localCultureIndices.Last();
numberOfCultures = numberCultures; innerLoopIndex = localCultureIndices.Last();
cultureIndices.Clear();
cultureIndices.AddRange(localCultureIndices);
isFirstCulture = false; isFirstCulture = false;
} }
@ -196,7 +200,7 @@ public sealed partial class Setting : UserControl
}); });
}); });
numberOfCultures--; cultureIndices.Remove(innerLoopIndex);
} }
} }

View File

@ -19,11 +19,11 @@ public partial class Settings : UserControl
private async void buttonAddCulture_Click(object sender, EventArgs e) private async void buttonAddCulture_Click(object sender, EventArgs e)
{ {
// Get the current number of cultures: // Get the current indices of cultures:
var numberCultures = await AppSettings.GetNumberCultures(); var cultureIndices = await AppSettings.GetCultureIndices();
// Add a new culture: // Add a new culture:
await AppSettings.SetCultureCode(++numberCultures, string.Empty); await AppSettings.SetCultureCode(cultureIndices.Max() + 1, string.Empty);
// Reload all settings: // Reload all settings:
await this.LoadAllSettings(); await this.LoadAllSettings();