From d5137b932cb22b16ee6ee27e2946128959172943 Mon Sep 17 00:00:00 2001 From: James Coliz Date: Mon, 16 Sep 2024 13:36:54 -0700 Subject: [PATCH] fix(ui): Add logging and hardening around slide out of range error Fixes #49 --- .../ViewModels/MainViewModel.cs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs b/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs index f18c347..ec6cf64 100644 --- a/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs +++ b/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs @@ -642,7 +642,22 @@ private void PopulateLayout() return; } - var variant = _definition.Variants.Count > 0 ? _definition.Variants[_slideNumber] : new Variant(); + var existingVariants = _definition.Variants; + var slideNumber = _slideNumber; + + // In case of no variants, we'll use a blank + if (existingVariants.Count == 0) + { + existingVariants = [new()]; + } + + if (slideNumber >= existingVariants.Count) + { + logWarningSlideOutoFoRange(slideNumber, existingVariants.Count); + slideNumber = 0; + } + + var variant = existingVariants[slideNumber]; var engine = new LayoutEngine(_definition, variant); _layout = engine.CreateSlideLayout(); @@ -736,5 +751,8 @@ private void OnPropertyChanged([CallerMemberName] string? propertyName = null) [LoggerMessage(Level = LogLevel.Error, Message = "{Location}: Slide number {Number} out of range {Count}")] public partial void logFailSlideOutoFoRange(int number, int count, [CallerMemberName] string? location = null); + [LoggerMessage(Level = LogLevel.Warning, Message = "{Location}: Slide number {Number} out of range {Count}")] + public partial void logWarningSlideOutoFoRange(int number, int count, [CallerMemberName] string? location = null); + #endregion }