Skip to content

Commit

Permalink
Adjust so that changing the partition option or mode causes a retally…
Browse files Browse the repository at this point in the history
…, if

the currently stored and displayed tally results are for the same quest as the
one currently selected in the UI.
  • Loading branch information
Kinematics committed Mar 17, 2015
1 parent 123bcf8 commit aea6110
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
4 changes: 2 additions & 2 deletions NetTally/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
IsEnabled="{Binding ElementName=tallyButton, Path=IsEnabled, Converter={StaticResource BoolConverter}, ConverterParameter=Normal, Mode=OneWay}"/>
<CheckBox x:Name="partitionedVotes" Content="Partition Votes" Margin="318,45,0,0" VerticalAlignment="Top"
IsChecked="{Binding Path=CurrentItem.UseVotePartitions}" HorizontalAlignment="Left" Width="95"
IsEnabled="{Binding ElementName=tallyButton, Path=IsEnabled, Converter={StaticResource BoolConverter}, ConverterParameter=Normal, Mode=OneWay}"/>
IsEnabled="{Binding ElementName=tallyButton, Path=IsEnabled, Converter={StaticResource BoolConverter}, ConverterParameter=Normal, Mode=OneWay}" Checked="partitionedVotes_CheckedChanged" Unchecked="partitionedVotes_CheckedChanged"/>
<StackPanel HorizontalAlignment="Left" Height="39" Margin="418,45,0,0" VerticalAlignment="Top" Width="87"
IsEnabled="{Binding ElementName=tallyButton, Path=IsEnabled, Converter={StaticResource BoolConverter}, ConverterParameter=Normal, Mode=OneWay}">
<RadioButton x:Name="partitionByLine" Content="By Line" HorizontalAlignment="Left" VerticalAlignment="Top"
GroupName="PartitionType" IsChecked="{Binding Path=CurrentItem.PartitionByLine, ConverterParameter=Normal, Converter={StaticResource BoolConverter}}"
IsEnabled="{Binding IsChecked, ElementName=partitionedVotes}"/>
IsEnabled="{Binding IsChecked, ElementName=partitionedVotes}" Checked="partitionByLine_CheckedChanged" Unchecked="partitionByLine_CheckedChanged"/>
<RadioButton x:Name="partitionByBlock" Content="By Block" HorizontalAlignment="Left" VerticalAlignment="Top"
GroupName="PartitionType" IsChecked="{Binding ElementName=partitionByLine, Path=IsChecked, ConverterParameter=Invert, Converter={StaticResource BoolConverter}, Mode=OneWay}"
IsEnabled="{Binding IsChecked, ElementName=partitionedVotes}"/>
Expand Down
33 changes: 30 additions & 3 deletions NetTally/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public MainWindow()
private void Window_Closing(object sender, CancelEventArgs e)
{
string selectedQuest = "";
IQuest currentQuest = QuestCollectionView.CurrentItem as IQuest;
if (currentQuest != null)

if (CurrentlySelectedQuest() != null)
{
selectedQuest = currentQuest.ThreadName;
selectedQuest = CurrentlySelectedQuest().ThreadName;
}

QuestCollectionWrapper qcw = new QuestCollectionWrapper(questCollection, selectedQuest);
NetTallyConfig.Save(tally, qcw);

Expand All @@ -82,6 +83,11 @@ private void Window_Closing(object sender, CancelEventArgs e)
}
#endregion

private IQuest CurrentlySelectedQuest()
{
return QuestCollectionView.CurrentItem as IQuest;
}

#region User action events
/// <summary>
/// Start running the tally on the currently selected quest and post range.
Expand Down Expand Up @@ -299,6 +305,27 @@ private void editQuestThread_KeyUp(object sender, KeyEventArgs e)
}
}


/// <summary>
/// If the option to partition votes is changed, retally the results.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void partitionedVotes_CheckedChanged(object sender, RoutedEventArgs e)
{
tally.TallyMethodChanged(CurrentlySelectedQuest());
}

/// <summary>
/// If the partition type is changed, retally the results.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void partitionByLine_CheckedChanged(object sender, RoutedEventArgs e)
{
tally.TallyMethodChanged(CurrentlySelectedQuest());
}

#endregion


Expand Down
50 changes: 35 additions & 15 deletions TallyCore/Tally.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace NetTally
{
Expand Down Expand Up @@ -73,18 +75,10 @@ public bool UseSpoilerForVoters
set
{
useSpoilerForVoters = value;
UpdateResults();
ChangeSpoilers();
}
}


IQuest lastTallyQuest = null;
private void UpdateResults()
{
if (lastTallyQuest != null)
ConstructResults(lastTallyQuest);
}

#endregion

#region Interface functions
Expand All @@ -100,21 +94,24 @@ public async Task Run(IQuest quest, CancellationToken token)
try
{
TallyResults = string.Empty;
lastTallyQuest = quest;

await quest.GetForumAdapter(token);

// Load pages from the website
var pages = await pageProvider.LoadPages(quest, token).ConfigureAwait(false);
loadedPages = await pageProvider.LoadPages(quest, token).ConfigureAwait(false);

// Tally the votes from the loaded pages.
voteCounter.TallyVotes(quest, pages);
voteCounter.TallyVotes(quest, loadedPages);

// Compose the final result string from the compiled votes.
ConstructResults(quest);

}
catch (NotImplementedException)
catch (Exception)
{
lastTallyQuest = null;
loadedPages.Clear();
throw;
}
finally
Expand All @@ -140,8 +137,6 @@ public void ClearPageCache()
/// </summary>
private void ConstructResults(IQuest quest)
{
lastTallyQuest = quest;

StringBuilder sb = new StringBuilder();

var assembly = Assembly.GetExecutingAssembly();
Expand Down Expand Up @@ -199,5 +194,30 @@ 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

}
}

0 comments on commit aea6110

Please sign in to comment.