Skip to content

Commit

Permalink
Allowed the document generator class to set status text colour. Fixed…
Browse files Browse the repository at this point in the history
… Add pages not recognising commas
  • Loading branch information
harrystb committed Feb 4, 2020
1 parent 5028941 commit 3f0ec0a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
28 changes: 9 additions & 19 deletions pdfjoiner.DesktopClient/Document/DocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, DocumentItem> _documentItems = new Dictionary<string, DocumentItem>();
public Dictionary<string, DocumentItem> DocumentItems
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -284,7 +274,7 @@ private void ThreadProc()
GenerationProcess.Kill();
GenerationProcess.Dispose();
GenerationProcess = null;
Status = "Cancelled.";
StatusCallback?.Invoke("Cancelled.", StatusColourState.Green);
return;
}

Expand All @@ -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);
}

/// <summary>
Expand Down
46 changes: 24 additions & 22 deletions pdfjoiner.DesktopClient/View Model/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
}

/// <summary>
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand All @@ -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;
}

Expand All @@ -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);
}

/// <summary>
Expand All @@ -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);

}

Expand All @@ -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
Expand All @@ -307,7 +309,7 @@ private bool ValidateAddPageString()
foreach (var segment in AddPageText.Split(','))
{
HashSet<char> AllowedChars = new HashSet<char> {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-'};
if (!AddPageText.All(AllowedChars.Contains))
if (!segment.All(AllowedChars.Contains))
return false;
}
return true;
Expand Down

0 comments on commit 3f0ec0a

Please sign in to comment.