diff --git a/NetTally/MainWindow.xaml.cs b/NetTally/MainWindow.xaml.cs index 7919067e..0f815809 100644 --- a/NetTally/MainWindow.xaml.cs +++ b/NetTally/MainWindow.xaml.cs @@ -313,7 +313,7 @@ private void editQuestThread_KeyUp(object sender, KeyEventArgs e) /// private void partitionedVotes_CheckedChanged(object sender, RoutedEventArgs e) { - tally.TallyMethodChanged(CurrentlySelectedQuest()); + tally.UpdateTally(CurrentlySelectedQuest()); } /// @@ -323,7 +323,7 @@ private void partitionedVotes_CheckedChanged(object sender, RoutedEventArgs e) /// private void partitionByLine_CheckedChanged(object sender, RoutedEventArgs e) { - tally.TallyMethodChanged(CurrentlySelectedQuest()); + tally.UpdateTally(CurrentlySelectedQuest()); } #endregion diff --git a/TallyCore/Tally.cs b/TallyCore/Tally.cs index 93bbcd69..6bcc50e9 100644 --- a/TallyCore/Tally.cs +++ b/TallyCore/Tally.cs @@ -16,6 +16,12 @@ public class Tally : INotifyPropertyChanged IPageProvider pageProvider; IVoteCounter voteCounter; + string results = string.Empty; + bool useSpoilerForVoters = false; + + IQuest lastTallyQuest = null; + List loadedPages = null; + public Tally() { pageProvider = new WebPageProvider(); @@ -24,7 +30,6 @@ public Tally() pageProvider.StatusChanged += PageProvider_StatusChanged; } - #region Event handling /// /// Keep watch for any status messasges from the page provider, and add them @@ -54,9 +59,9 @@ protected void OnPropertyChanged([CallerMemberName] string propertyName = null) #endregion #region Behavior properties - string results = string.Empty; /// - /// Property for the string containing the current tally progress or results. + /// The string containing the current tally progress or results. + /// Creates a notification event if the contents change. /// public string TallyResults { @@ -68,14 +73,18 @@ public string TallyResults } } - bool useSpoilerForVoters = false; + /// + /// Flag for whether to use spoiler blocks for voter lists in + /// the output display. + /// Recalculates the display if changed. + /// public bool UseSpoilerForVoters { get { return useSpoilerForVoters; } set { useSpoilerForVoters = value; - ChangeSpoilers(); + ConstructResults(lastTallyQuest); } } @@ -101,17 +110,13 @@ public async Task Run(IQuest quest, CancellationToken token) // Load pages from the website loadedPages = await pageProvider.LoadPages(quest, token).ConfigureAwait(false); - // Tally the votes from the loaded pages. - voteCounter.TallyVotes(quest, loadedPages); - - // Compose the final result string from the compiled votes. - ConstructResults(quest); - + UpdateTally(quest); } catch (Exception) { lastTallyQuest = null; loadedPages.Clear(); + loadedPages = null; throw; } finally @@ -120,6 +125,21 @@ public async Task Run(IQuest quest, CancellationToken token) } } + public void UpdateTally(IQuest changedQuest) + { + if (lastTallyQuest != null && changedQuest == lastTallyQuest) + { + if (loadedPages != null && loadedPages.Count > 0) + { + // Tally the votes from the loaded pages. + voteCounter.TallyVotes(lastTallyQuest, loadedPages); + + // Compose the final result string from the compiled votes. + ConstructResults(lastTallyQuest); + } + } + } + /// /// Allow manual clearing of the page cache. /// @@ -130,13 +150,16 @@ public void ClearPageCache() #endregion - #region Local class functions + #region Functions for constructing the tally results output. /// /// Compose the tallied results into a string to put in the TallyResults property, /// for display in the UI. /// private void ConstructResults(IQuest quest) { + if (quest == null) + return; + StringBuilder sb = new StringBuilder(); var assembly = Assembly.GetExecutingAssembly(); @@ -193,30 +216,7 @@ private string GenerateSupporterUrl(IQuest quest, string supporter) return sb.ToString(); } - #endregion - - #region Live Updates - - IQuest lastTallyQuest = null; - List loadedPages = null; - private void ChangeSpoilers() - { - if (lastTallyQuest != null) - ConstructResults(lastTallyQuest); - } - - public void TallyMethodChanged(IQuest changedQuest) - { - if (lastTallyQuest != null && changedQuest == lastTallyQuest) - { - if (loadedPages != null && loadedPages.Count > 0) - { - voteCounter.TallyVotes(lastTallyQuest, loadedPages); - ConstructResults(lastTallyQuest); - } - } - } #endregion }