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
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:
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<DataContext>();
// 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<List<CultureInfo>> GetCultureInfos()
{
// Get the number of cultures:
var numberOfCultures = await AppSettings.GetNumberCultures();
var cultureIndices = await AppSettings.GetCultureIndices();
// Get the culture codes:
var cultureInfos = new List<CultureInfo>();
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;

View File

@ -153,23 +153,27 @@ public sealed partial class Setting : UserControl
private static IEnumerable<Task<Setting>> 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<int>(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);
}
}

View File

@ -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();