瀏覽代碼

Added retry loop for missing settings

Kenric Nugteren 1 年之前
父節點
當前提交
fd92ad031d
共有 1 個文件被更改,包括 87 次插入73 次删除
  1. 87 73
      prs.desktop/Utils/PostUtils.cs

+ 87 - 73
prs.desktop/Utils/PostUtils.cs

@@ -24,110 +24,124 @@ public static class PostUtils
     public static void PostEntities<T>(IDataModel<T> model, Action refresh, Action? configurePost = null)
         where T : Entity, IPostable, IRemotable, IPersistent, new()
     {
-        try
+        bool retry;
+        do
         {
-            var result = PosterUtils.Process(model);
-            if(result is null)
+            retry = false;
+            try
             {
-                MessageWindow.ShowMessage($"Processing failed", "Processing failed");
-                refresh();
-            }
-            else
-            {
-                var failedMessages = new List<string>();
-                var successCount = 0;
-                foreach(var entity in result.PostedEntities)
+                var result = PosterUtils.Process(model);
+                if (result is null)
+                {
+                    MessageWindow.ShowMessage($"Processing failed", "Processing failed");
+                    refresh();
+                }
+                else
                 {
-                    if(entity.PostedStatus == PostedStatus.PostFailed)
+                    var failedMessages = new List<string>();
+                    var successCount = 0;
+                    foreach (var entity in result.PostedEntities)
+                    {
+                        if (entity.PostedStatus == PostedStatus.PostFailed)
+                        {
+                            failedMessages.Add(entity.PostedNote);
+                        }
+                        else
+                        {
+                            successCount++;
+                        }
+                    }
+                    if (successCount == 0)
                     {
-                        failedMessages.Add(entity.PostedNote);
+                        MessageWindow.ShowMessage($"Processing failed:\n - {string.Join("\n - ", failedMessages)}", "Processing failed.");
+                    }
+                    else if (failedMessages.Count == 0)
+                    {
+                        MessageWindow.ShowMessage($"Processing successful; {successCount} items processed", "Processing successful.");
                     }
                     else
                     {
-                        successCount++;
+                        MessageWindow.ShowMessage($"{successCount} items succeeded, but {failedMessages.Count} failed:\n - {string.Join("\n - ", failedMessages)}", "Partial success");
                     }
+                    refresh();
                 }
-                if(successCount == 0)
-                {
-                    MessageWindow.ShowMessage($"Processing failed:\n - {string.Join("\n - ", failedMessages)}", "Processing failed.");
-                }
-                else if(failedMessages.Count == 0)
-                {
-                    MessageWindow.ShowMessage($"Processing successful; {successCount} items processed", "Processing successful.");
-                }
-                else
-                {
-                    MessageWindow.ShowMessage($"{successCount} items succeeded, but {failedMessages.Count} failed:\n - {string.Join("\n - ", failedMessages)}", "Partial success");
-                }
-                refresh();
             }
-        }
-        catch (EmptyPostException)
-        {
-            MessageWindow.ShowMessage($"Please select at least one {typeof(T).Name}.", "Select items");
-        }
-        catch (PostFailedMessageException e)
-        {
-            MessageWindow.ShowMessage(e.Message, "Post failed");
-        }
-        catch (RepostedException)
-        {
-            MessageWindow.ShowMessage("At least one of the items you selected has already been processed. Processing cancelled.", "Already processed");
-        }
-        catch (PostCancelledException)
-        {
-            MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
-        }
-        catch(MissingSettingException e)
-        {
-            if (configurePost is not null && Security.CanConfigurePost<T>())
+            catch (EmptyPostException)
+            {
+                MessageWindow.ShowMessage($"Please select at least one {typeof(T).Name}.", "Select items");
+            }
+            catch (PostFailedMessageException e)
+            {
+                MessageWindow.ShowMessage(e.Message, "Post failed");
+            }
+            catch (RepostedException)
+            {
+                MessageWindow.ShowMessage("At least one of the items you selected has already been processed. Processing cancelled.", "Already processed");
+            }
+            catch (PostCancelledException)
+            {
+                MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
+            }
+            catch (MissingSettingException e)
             {
-                if (MessageWindow.ShowYesNo($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
-                    "Configure Processing?"))
+                if (configurePost is not null && Security.CanConfigurePost<T>())
                 {
-                    if (e.SettingsType.IsAssignableTo(typeof(IGlobalPosterSettings)))
+                    if (MessageWindow.ShowYesNo($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
+                        "Configure Processing?"))
                     {
-                        PostableSettingsGrid.ConfigureGlobalPosterSettings(e.SettingsType);
+                        bool success = false;
+                        if (e.SettingsType.IsAssignableTo(typeof(IGlobalPosterSettings)))
+                        {
+                            success = PostableSettingsGrid.ConfigureGlobalPosterSettings(e.SettingsType);
+                        }
+                        else
+                        {
+                            success = PostableSettingsGrid.ConfigurePosterSettings<T>(e.SettingsType);
+                        }
+                        if (success && MessageWindow.ShowYesNo("Settings updated; Would you like to retry the post?", "Retry?"))
+                        {
+                            retry = true;
+                        }
+                        else
+                        {
+                            MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
+                        }
                     }
                     else
                     {
-                        PostableSettingsGrid.ConfigurePosterSettings<T>(e.SettingsType);
+                        MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
                     }
                 }
                 else
                 {
-                    MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
+                    MessageWindow.ShowMessage($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}", "Unconfigured");
                 }
             }
-            else
-            {
-                MessageWindow.ShowMessage($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}", "Unconfigured");
-            }
-        }
-        catch (MissingSettingsException)
-        {
-            if (configurePost is not null && Security.CanConfigurePost<T>())
+            catch (MissingSettingsException)
             {
-                if (MessageWindow.ShowYesNo($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
-                    "Configure Processing?"))
+                if (configurePost is not null && Security.CanConfigurePost<T>())
                 {
-                    configurePost();
+                    if (MessageWindow.ShowYesNo($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
+                        "Configure Processing?"))
+                    {
+                        configurePost();
+                    }
+                    else
+                    {
+                        MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
+                    }
                 }
                 else
                 {
-                    MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
+                    MessageWindow.ShowMessage($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!", "Unconfigured");
                 }
             }
-            else
+            catch (Exception e)
             {
-                MessageWindow.ShowMessage($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!", "Unconfigured");
+                MessageWindow.ShowError("Processing failed.", e);
+                refresh();
             }
-        }
-        catch (Exception e)
-        {
-            MessageWindow.ShowError("Processing failed.", e);
-            refresh();
-        }
+        } while (retry);
     }
 
     public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, Action? configurePost = null)