Repository: microsoft/PowerToys
File: src/modules/imageresizer/ui/Properties/Settings.cs (C#)

Code before:
            }

            // Needs to be called on the App UI thread as the properties are bound to the UI.
            App.Current.Dispatcher.Invoke(() =>
            {
                ShrinkOnly = jsonSettings.ShrinkOnly;
                Replace = jsonSettings.Replace;
                IgnoreOrientation = jsonSettings.IgnoreOrientation;
                RemoveMetadata = jsonSettings.RemoveMetadata;
                JpegQualityLevel = jsonSettings.JpegQualityLevel;
                PngInterlaceOption = jsonSettings.PngInterlaceOption;
                TiffCompressOption = jsonSettings.TiffCompressOption;
                FileName = jsonSettings.FileName;
                KeepDateModified = jsonSettings.KeepDateModified;
                FallbackEncoder = jsonSettings.FallbackEncoder;
                CustomSize = jsonSettings.CustomSize;
                SelectedSizeIndex = jsonSettings.SelectedSizeIndex;

                if (jsonSettings.Sizes.Count > 0)
                {
                    Sizes.Clear();
                    Sizes.AddRange(jsonSettings.Sizes);

                    // Ensure Ids are unique and handle missing Ids
                    ImageResizerHelper.RecoverInvalidIds(Sizes);
                }
            });

            _jsonMutex.ReleaseMutex();
        }
    }
}


Review comment (refactor):
If the user exits before making any changes to the settings (in the Settings application or Image Resizer itself), any invalid or absent Ids in the `settings.json` file will still be unchanged, even if they've been recovered using the new helper.

I wonder whether this is an actual issue, though. The only setting which links to an actual Id value is the one which saves the last used entry in the drop-down - selectedSizeIndex, and if the user changes that, it will re-save the settings file.

I'm willing to accept your judgement here, but thought it was worth mentioning.

Code after:
            }

            // Needs to be called on the App UI thread as the properties are bound to the UI.
            App.Current.Dispatcher.Invoke(() =>
            {
                ShrinkOnly = jsonSettings.ShrinkOnly;
                Replace = jsonSettings.Replace;
                IgnoreOrientation = jsonSettings.IgnoreOrientation;
                RemoveMetadata = jsonSettings.RemoveMetadata;
                JpegQualityLevel = jsonSettings.JpegQualityLevel;
                PngInterlaceOption = jsonSettings.PngInterlaceOption;
                TiffCompressOption = jsonSettings.TiffCompressOption;
                FileName = jsonSettings.FileName;
                KeepDateModified = jsonSettings.KeepDateModified;
                FallbackEncoder = jsonSettings.FallbackEncoder;
                CustomSize = jsonSettings.CustomSize;
                SelectedSizeIndex = jsonSettings.SelectedSizeIndex;

                if (jsonSettings.Sizes.Count > 0)
                {
                    Sizes.Clear();
                    Sizes.AddRange(jsonSettings.Sizes);

                    // Ensure Ids are unique and handle missing Ids
                    IdRecoveryHelper.RecoverInvalidIds(Sizes);
                }
            });

            _jsonMutex.ReleaseMutex();
        }
    }
}