Sfoglia il codice sorgente

Added exception handling to Post Map Configuration

Kenric Nugteren 1 settimana fa
parent
commit
fdc983a75e
1 ha cambiato i file con 61 aggiunte e 54 eliminazioni
  1. 61 54
      prs.desktop/Utils/PostUtils.cs

+ 61 - 54
prs.desktop/Utils/PostUtils.cs

@@ -745,70 +745,77 @@ public static class PostUtils
 
     private static void ConfigureMaps(PanelActionEntry<Type> entry) // entry.Data is a definition of IPosterMapConfigurer
     {
-        var configurerType = entry.Data!;
-        var configurer = (Activator.CreateInstance(configurerType) as IPosterMapConfigurer)!;
-
-        var dispatcher = new PosterProgressDispatcher("Loading Maps");
-        var task = Task.Run(() => configurer.GetMaps(dispatcher));
-        dispatcher.WaitTask(task);
+        try
+        {
+            var configurerType = entry.Data!;
+            var configurer = (Activator.CreateInstance(configurerType) as IPosterMapConfigurer)!;
 
-        var allItems =
-            CoreUtils.One(new ConfigureItem(null, "(Blank)", ""))
-            .Concat(task.Result.OfType<object?>().Select(x => new ConfigureItem(x, configurer.MapDescriptor(x), configurer.Reference(x))))
-            .ToArray();
+            var dispatcher = new PosterProgressDispatcher("Loading Maps");
+            var task = Task.Run(() => configurer.GetMaps(dispatcher));
+            dispatcher.WaitTask(task);
 
-        var tConfigure = configurerType.GetInterfaceDefinition(typeof(IPosterMapConfigurer<>))!.GenericTypeArguments[0];
-        var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicItemsListGrid<>), tConfigure) as IDynamicItemsListGrid)!;
-        var window = new DynamicContentDialog((grid as FrameworkElement)!)
-        {
-            Title = $"Configure {tConfigure.Name} maps"
-        };
+            var allItems =
+                CoreUtils.One(new ConfigureItem(null, "(Blank)", ""))
+                .Concat(task.Result.OfType<object?>().Select(x => new ConfigureItem(x, configurer.MapDescriptor(x), configurer.Reference(x))))
+                .ToArray();
 
-        grid.AddHiddenColumn(CoreUtils.GetFullPropertyName<IPostableFragment, string>(x => x.PostedReference));
-        grid.Reconfigure(options =>
-        {
-            options.Clear();
-        });
-        grid.ActionColumns.Add(new DynamicTemplateColumn(row =>
-        {
-            var item = (grid.LoadItem(row) as IPostableFragment);
-            var value = item!.PostedReference;
-            var comboBox = new ComboBox
-            {
-                ItemsSource = allItems,
-                SelectedValuePath = "Reference",
-                DisplayMemberPath = "Display",
-                VerticalContentAlignment = VerticalAlignment.Center
-            };
-            comboBox.SelectedValue = value;
-            comboBox.SelectionChanged += (o, e) =>
+            var tConfigure = configurerType.GetInterfaceDefinition(typeof(IPosterMapConfigurer<>))!.GenericTypeArguments[0];
+            var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicItemsListGrid<>), tConfigure) as IDynamicItemsListGrid)!;
+            var window = new DynamicContentDialog((grid as FrameworkElement)!)
             {
-                window.CanSave = true;
-                item.PostedReference = (comboBox.SelectedValue as string)!;
+                Title = $"Configure {tConfigure.Name} maps"
             };
-            return comboBox;
-        })
-        {
-            HeaderText = "Maps To..."
-        });
 
-        grid.Refresh(true, false);
+            grid.AddHiddenColumn(CoreUtils.GetFullPropertyName<IPostableFragment, string>(x => x.PostedReference));
+            grid.Reconfigure(options =>
+            {
+                options.Clear();
+            });
+            grid.ActionColumns.Add(new DynamicTemplateColumn(row =>
+            {
+                var item = (grid.LoadItem(row) as IPostableFragment);
+                var value = item!.PostedReference;
+                var comboBox = new ComboBox
+                {
+                    ItemsSource = allItems,
+                    SelectedValuePath = "Reference",
+                    DisplayMemberPath = "Display",
+                    VerticalContentAlignment = VerticalAlignment.Center
+                };
+                comboBox.SelectedValue = value;
+                comboBox.SelectionChanged += (o, e) =>
+                {
+                    window.CanSave = true;
+                    item.PostedReference = (comboBox.SelectedValue as string)!;
+                };
+                return comboBox;
+            })
+            {
+                HeaderText = "Maps To..."
+            });
+
+            grid.Refresh(true, false);
 
-        var items = Client.Create(tConfigure)
-            .Query(
-                Filter.Create(tConfigure).All(),
-                grid.DataColumns())
-            .ToObjects(tConfigure);
-        foreach(var item in items)
-        {
-            grid.Items.Add(item);
-        }
+            var items = Client.Create(tConfigure)
+                .Query(
+                    Filter.Create(tConfigure).All(),
+                    grid.DataColumns())
+                .ToObjects(tConfigure);
+            foreach(var item in items)
+            {
+                grid.Items.Add(item);
+            }
 
-        grid.Refresh(false, true);
+            grid.Refresh(false, true);
 
-        if(window.ShowDialog() == true)
+            if(window.ShowDialog() == true)
+            {
+                Client.Create(tConfigure).Save(grid.Items.OfType<Entity>().Where(x => x.IsChanged()), "Updated poster mapping");
+            }
+        }
+        catch(Exception e)
         {
-            Client.Create(tConfigure).Save(grid.Items.OfType<Entity>().Where(x => x.IsChanged()), "Updated poster mapping");
+            MessageWindow.ShowError("Configuration failed", e);
         }
     }