diff --git a/pdfjoiner.DesktopClient/Document/DocumentGenerator.cs b/pdfjoiner.DesktopClient/Document/DocumentGenerator.cs index 96d6e53..f2ff759 100644 --- a/pdfjoiner.DesktopClient/Document/DocumentGenerator.cs +++ b/pdfjoiner.DesktopClient/Document/DocumentGenerator.cs @@ -19,19 +19,9 @@ public class DocumentGenerator #endregion #region Public Attributes - private string _status = string.Empty; - public string Status - { - get - { - return _status; - } - private set - { - _status = value; - StatusCallback?.Invoke(_status); - } - } + + public enum StatusColourState { Green, Red, Orange}; + private Dictionary _documentItems = new Dictionary(); public Dictionary DocumentItems @@ -61,7 +51,7 @@ private set private string NextKey = "A"; private IntPtr GenerationProcessHWND = IntPtr.Zero; - public delegate void StatusUpdateCallbackHandler(string status); + public delegate void StatusUpdateCallbackHandler(string status, StatusColourState colourState); private event StatusUpdateCallbackHandler? StatusCallback = null; #endregion @@ -91,7 +81,7 @@ public DocumentGenerator() var key = GetNextKey(); DocumentItems.Add(key, newDoc); - Status = "Document successfully added."; + StatusCallback?.Invoke("Document successfully added.", StatusColourState.Green); return key; } @@ -255,7 +245,7 @@ public string Generate(string GenerationString) //GenerationProcessHWND = GenerationProcess.MainWindowHandle; GenerationWindowVisible = false; //ToggleProcessWindowVisibility(); - Status = "Generating."; + StatusCallback?.Invoke("Generating.", StatusColourState.Orange); GenerationThread = new Thread(ThreadProc); GenerationThread.Start(); @@ -284,7 +274,7 @@ private void ThreadProc() GenerationProcess.Kill(); GenerationProcess.Dispose(); GenerationProcess = null; - Status = "Cancelled."; + StatusCallback?.Invoke("Cancelled.", StatusColourState.Green); return; } @@ -307,12 +297,12 @@ private void ThreadProc() else { //not found - failed to generate. - Status = "Error - generation of PDF failed."; + StatusCallback?.Invoke("Error - generation of PDF failed.", StatusColourState.Red); return; } GenerationProcess?.Dispose(); GenerationProcess = null; - Status = "Done."; + StatusCallback?.Invoke("Done.", StatusColourState.Green); } /// diff --git a/pdfjoiner.DesktopClient/View Model/MainViewModel.cs b/pdfjoiner.DesktopClient/View Model/MainViewModel.cs index 49b4781..626c8c6 100644 --- a/pdfjoiner.DesktopClient/View Model/MainViewModel.cs +++ b/pdfjoiner.DesktopClient/View Model/MainViewModel.cs @@ -31,10 +31,9 @@ public MainViewModel() _ResetForm = new DelegateCommand(OnResetFormButton); //Create the document generator and register status callback DocGenerator = new DocumentGenerator(); - DocGenerator.SetStatusChangedCallback(StatusChanged); + DocGenerator.SetStatusChangedCallback(SetStatusTextboxContent); //Set the initial status text; - StatusText = "Welcome, please add a document to get started."; - StatusBrush = (Brush) new BrushConverter().ConvertFromString("Green"); + SetStatusTextboxContent("Welcome, please add a document to get started.", DocumentGenerator.StatusColourState.Green); } #endregion @@ -168,10 +167,21 @@ public string SelectedDocument #region Methods - - void StatusChanged(string newStatus) + private void SetStatusTextboxContent(string newStatus, DocumentGenerator.StatusColourState colourState ) { StatusText = newStatus; + switch (colourState) + { + case (DocumentGenerator.StatusColourState.Green): + StatusBrush = (Brush)new BrushConverter().ConvertFromString("Green"); + break; + case (DocumentGenerator.StatusColourState.Red): + StatusBrush = (Brush)new BrushConverter().ConvertFromString("Red"); + break; + case (DocumentGenerator.StatusColourState.Orange): + StatusBrush = (Brush)new BrushConverter().ConvertFromString("Orange"); + break; + } } /// @@ -199,22 +209,19 @@ private void OnAddDocumentButton(object commandParameter) FileDialog1.ShowDialog(); if (FileDialog1.FileName == "") { - StatusText = "No file selected."; - StatusBrush = (Brush)new BrushConverter().ConvertFromString("Orange"); + SetStatusTextboxContent("No file selected.", DocumentGenerator.StatusColourState.Orange); return; } string? key = DocGenerator.AddDocumentToList(FileDialog1.FileName); if (string.IsNullOrEmpty(key)) { - StatusText = "Document is already in the list."; - StatusBrush = (Brush)new BrushConverter().ConvertFromString("Orange"); + SetStatusTextboxContent("Document is already in the list.", DocumentGenerator.StatusColourState.Orange); return; } string listText = key + ": " + DocGenerator.GetDocument(key)?.Filename ?? "Unknown"; DocumentItemList.Add(listText); SelectedDocument = listText; - StatusText = "Document sucessfully added."; - StatusBrush = (Brush) new BrushConverter().ConvertFromString("Green"); + SetStatusTextboxContent("Document sucessfully added.", DocumentGenerator.StatusColourState.Green); } /// @@ -227,8 +234,7 @@ private void OnAddPagesButton(object commandParameter) //Validate the page string if (!ValidateAddPageString()) { - StatusText = "Invalid character entered. Valid Example: 1,2-3,5-,-6"; - StatusBrush = (Brush) new BrushConverter().ConvertFromString("Red"); + SetStatusTextboxContent("Invalid character entered. Valid Example: 1,2-3,5-,-6", DocumentGenerator.StatusColourState.Red); return; } @@ -242,8 +248,7 @@ private void OnAddPagesButton(object commandParameter) else GenerationText = $"{GenerationText},{id}{segment}"; } - StatusText = "Pages added to the generation string."; - StatusBrush = (Brush) new BrushConverter().ConvertFromString("Green"); + SetStatusTextboxContent("Pages added to the generation string.", DocumentGenerator.StatusColourState.Green); } /// @@ -268,12 +273,10 @@ private void OnCancelGenerationButton(object commandParameter) //set status text based on whether the termination was successful if (success) { - StatusText = "Document Generation stopped."; - StatusBrush = (Brush) new BrushConverter().ConvertFromString("Green"); + SetStatusTextboxContent("Document Generation stopped.", DocumentGenerator.StatusColourState.Green); return; } - StatusText = "Document Generation failed to stop."; - StatusBrush = (Brush) new BrushConverter().ConvertFromString("Red"); + SetStatusTextboxContent("Document Generation failed to stop.", DocumentGenerator.StatusColourState.Red); } @@ -293,8 +296,7 @@ private void OnResetFormButton(object commandParameter) //Reset the Generation Text GenerationText = ""; //write some status text - StatusText = "All fields reset."; - StatusBrush = (Brush) new BrushConverter().ConvertFromString("Green"); + SetStatusTextboxContent("All fields reset.", DocumentGenerator.StatusColourState.Green); } #endregion @@ -307,7 +309,7 @@ private bool ValidateAddPageString() foreach (var segment in AddPageText.Split(',')) { HashSet AllowedChars = new HashSet {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-'}; - if (!AddPageText.All(AllowedChars.Contains)) + if (!segment.All(AllowedChars.Contains)) return false; } return true;