Fixed culture setting handling for non-continuous indices

This commit is contained in:
tsommer committed 2022-08-02 20:43:12 +02:00
1 parent fd3d844df4
commit fee963f8bd
3 files changed
+34 -30

No files matched your search

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