Skip to content

Commit

Permalink
Reorganize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinematics committed Mar 17, 2015
1 parent 55cc0bf commit b1a33a1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions NetTally/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void editQuestThread_KeyUp(object sender, KeyEventArgs e)
/// <param name="e"></param>
private void partitionedVotes_CheckedChanged(object sender, RoutedEventArgs e)
{
tally.TallyMethodChanged(CurrentlySelectedQuest());
tally.UpdateTally(CurrentlySelectedQuest());
}

/// <summary>
Expand All @@ -323,7 +323,7 @@ private void partitionedVotes_CheckedChanged(object sender, RoutedEventArgs e)
/// <param name="e"></param>
private void partitionByLine_CheckedChanged(object sender, RoutedEventArgs e)
{
tally.TallyMethodChanged(CurrentlySelectedQuest());
tally.UpdateTally(CurrentlySelectedQuest());
}

#endregion
Expand Down
70 changes: 35 additions & 35 deletions TallyCore/Tally.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class Tally : INotifyPropertyChanged
IPageProvider pageProvider;
IVoteCounter voteCounter;

string results = string.Empty;
bool useSpoilerForVoters = false;

IQuest lastTallyQuest = null;
List<HtmlDocument> loadedPages = null;

public Tally()
{
pageProvider = new WebPageProvider();
Expand All @@ -24,7 +30,6 @@ public Tally()
pageProvider.StatusChanged += PageProvider_StatusChanged;
}


#region Event handling
/// <summary>
/// Keep watch for any status messasges from the page provider, and add them
Expand Down Expand Up @@ -54,9 +59,9 @@ protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
#endregion

#region Behavior properties
string results = string.Empty;
/// <summary>
/// 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.
/// </summary>
public string TallyResults
{
Expand All @@ -68,14 +73,18 @@ public string TallyResults
}
}

bool useSpoilerForVoters = false;
/// <summary>
/// Flag for whether to use spoiler blocks for voter lists in
/// the output display.
/// Recalculates the display if changed.
/// </summary>
public bool UseSpoilerForVoters
{
get { return useSpoilerForVoters; }
set
{
useSpoilerForVoters = value;
ChangeSpoilers();
ConstructResults(lastTallyQuest);
}
}

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

/// <summary>
/// Allow manual clearing of the page cache.
/// </summary>
Expand All @@ -130,13 +150,16 @@ public void ClearPageCache()

#endregion

#region Local class functions
#region Functions for constructing the tally results output.
/// <summary>
/// Compose the tallied results into a string to put in the TallyResults property,
/// for display in the UI.
/// </summary>
private void ConstructResults(IQuest quest)
{
if (quest == null)
return;

StringBuilder sb = new StringBuilder();

var assembly = Assembly.GetExecutingAssembly();
Expand Down Expand Up @@ -193,30 +216,7 @@ private string GenerateSupporterUrl(IQuest quest, string supporter)

return sb.ToString();
}
#endregion

#region Live Updates


IQuest lastTallyQuest = null;
List<HtmlDocument> 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

}
Expand Down

0 comments on commit b1a33a1

Please sign in to comment.