Skip to content

Commit

Permalink
Convert function to run tally on votes to async to avoid UI jank.
Browse files Browse the repository at this point in the history
Update all references.
  • Loading branch information
Kinematics committed Jan 11, 2017
1 parent 6be7ed3 commit 413a019
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
8 changes: 4 additions & 4 deletions TallyCore/Tally.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ private void Options_PropertyChanged(object sender, PropertyChangedEventArgs e)
/// </summary>
/// <param name="sender">The quest that sent the notification.</param>
/// <param name="e">Info about a property of the quest that changed.</param>
private void Quest_PropertyChanged(object sender, PropertyChangedEventArgs e)
private async void Quest_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
IQuest quest = sender as IQuest;
if (quest != null && quest == VoteCounter.Instance.Quest)
{
if (e.PropertyName == "PartitionMode")
UpdateTally();
await UpdateTally();
}
}
#endregion
Expand Down Expand Up @@ -253,12 +253,12 @@ public void ClearPageCache()
/// <summary>
/// Process the results of the tally through the vote counter, and update the output.
/// </summary>
private void UpdateTally()
private async Task UpdateTally()
{
if (VoteCounter.Instance.Quest != null)
{
// Tally the votes from the loaded pages.
VoteCounter.Instance.TallyPosts();
await VoteCounter.Instance.TallyPosts().ConfigureAwait(false);

// Compose the final result string from the compiled votes.
UpdateResults();
Expand Down
4 changes: 2 additions & 2 deletions TallyCore/Votes/IVoteCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public interface IVoteCounter : INotifyPropertyChanged
IQuest Quest { get; set; }

Task<bool> TallyVotes(IQuest quest, ThreadRangeInfo startInfo, List<Task<HtmlDocument>> pages);
void TallyPosts();
void TallyPosts(IQuest quest);
Task TallyPosts();
Task TallyPosts(IQuest quest);
List<PostComponents> PostsList { get; }
void Reset();

Expand Down
26 changes: 22 additions & 4 deletions TallyCore/Votes/VoteCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public async Task<bool> TallyVotes(IQuest quest, ThreadRangeInfo startInfo, List

PostsList = PostsList.Distinct().OrderBy(p => p.Number).ToList();

TallyPosts();
await TallyPosts().ConfigureAwait(false);

return true;
}
Expand All @@ -187,22 +187,33 @@ public async Task<bool> TallyVotes(IQuest quest, ThreadRangeInfo startInfo, List
/// Run TallyPosts using the provided quest.
/// </summary>
/// <param name="quest">The quest that will be used for tallying parameters.</param>
public void TallyPosts(IQuest quest)
public async Task TallyPosts(IQuest quest)
{
Quest = quest;
TallyPosts();
await TallyPosts().ConfigureAwait(false);
}

/// <summary>
/// Construct the tally results based on the stored list of posts.
/// Run async so that it doesn't cause UI jank.
/// </summary>
public void TallyPosts()
public async Task TallyPosts()
{
Reset();

if (PostsList == null || PostsList.Count == 0)
return;

await Task.Run(() => PreprocessPlans());
await Task.Run(() => ProcessPosts());
}

/// <summary>
/// The first half of tallying posts involves doing the preprocessing
/// work on the plans in the post list.
/// </summary>
private void PreprocessPlans()
{
// Preprocessing Phase 1 (Only plans with contents are counted as plans.)
foreach (var post in PostsList)
{
Expand Down Expand Up @@ -230,7 +241,14 @@ public void TallyPosts()
{
post.SetWorkingVote(p => VoteConstructor.GetWorkingVote(p));
}
}

/// <summary>
/// The second half of tallying the posts involves cycling through for
/// as long as future references need to be handled.
/// </summary>
private void ProcessPosts()
{
var unprocessed = PostsList;

// Loop as long as there are any more to process.
Expand Down
25 changes: 13 additions & 12 deletions TallyUnitTest/VoteConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NetTally.Utility;

namespace NetTally.Tests
Expand Down Expand Up @@ -136,7 +137,7 @@ [x] Head over to Oriko's.


[TestMethod]
public void ProcessPostContentsWholeWithReferralTest1()
public async Task ProcessPostContentsWholeWithReferralTest1()
{
sampleQuest.PartitionMode = PartitionMode.None;

Expand Down Expand Up @@ -164,7 +165,7 @@ [x] Head over to Oriko's.
VoteCounter.Instance.PostsList.Add(p1);
VoteCounter.Instance.PostsList.Add(p2);

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

var votes = VoteCounter.Instance.GetVotesCollection(VoteType.Vote);
var voters = VoteCounter.Instance.GetVotersCollection(VoteType.Vote);
Expand All @@ -174,7 +175,7 @@ [x] Head over to Oriko's.
}

[TestMethod]
public void ProcessPostContentsBlockWithReferralTest1()
public async Task ProcessPostContentsBlockWithReferralTest1()
{
sampleQuest.PartitionMode = PartitionMode.ByBlock;

Expand All @@ -200,7 +201,7 @@ [x] Head over to Oriko's.
VoteCounter.Instance.PostsList.Add(p1);
VoteCounter.Instance.PostsList.Add(p2);

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

var votes = VoteCounter.Instance.GetVotesCollection(VoteType.Vote);
var voters = VoteCounter.Instance.GetVotersCollection(VoteType.Vote);
Expand All @@ -210,7 +211,7 @@ [x] Head over to Oriko's.
}

[TestMethod]
public void ProcessPostContentsLineWithReferralTest1()
public async Task ProcessPostContentsLineWithReferralTest1()
{
sampleQuest.PartitionMode = PartitionMode.ByLine;

Expand All @@ -236,7 +237,7 @@ [x] Head over to Oriko's.
VoteCounter.Instance.PostsList.Add(p1);
VoteCounter.Instance.PostsList.Add(p2);

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

var votes = VoteCounter.Instance.GetVotesCollection(VoteType.Vote);
var voters = VoteCounter.Instance.GetVotersCollection(VoteType.Vote);
Expand All @@ -247,7 +248,7 @@ [x] Head over to Oriko's.


[TestMethod]
public void ProcessPostContentsWholeWithReferralTest2()
public async Task ProcessPostContentsWholeWithReferralTest2()
{
sampleQuest.PartitionMode = PartitionMode.None;

Expand All @@ -273,7 +274,7 @@ [x] Head over to Oriko's.
VoteCounter.Instance.PostsList.Add(p1);
VoteCounter.Instance.PostsList.Add(p2);

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

var votes = VoteCounter.Instance.GetVotesCollection(VoteType.Vote);
var voters = VoteCounter.Instance.GetVotersCollection(VoteType.Vote);
Expand All @@ -283,7 +284,7 @@ [x] Head over to Oriko's.
}

[TestMethod]
public void ProcessPostContentsBlockWithReferralTest2()
public async Task ProcessPostContentsBlockWithReferralTest2()
{
sampleQuest.PartitionMode = PartitionMode.ByBlock;

Expand All @@ -310,7 +311,7 @@ [x] Head over to Oriko's.
VoteCounter.Instance.PostsList.Add(p1);
VoteCounter.Instance.PostsList.Add(p2);

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

var votes = VoteCounter.Instance.GetVotesCollection(VoteType.Vote);
var voters = VoteCounter.Instance.GetVotersCollection(VoteType.Vote);
Expand All @@ -321,7 +322,7 @@ [x] Head over to Oriko's.
}

[TestMethod]
public void ProcessPostContentsLineWithReferralTest2()
public async Task ProcessPostContentsLineWithReferralTest2()
{
sampleQuest.PartitionMode = PartitionMode.ByLine;

Expand All @@ -348,7 +349,7 @@ [x] Head over to Oriko's.
VoteCounter.Instance.PostsList.Add(p1);
VoteCounter.Instance.PostsList.Add(p2);

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

var votes = VoteCounter.Instance.GetVotesCollection(VoteType.Vote);
var voters = VoteCounter.Instance.GetVotersCollection(VoteType.Vote);
Expand Down
6 changes: 4 additions & 2 deletions TallyUnitTest/VoteCounterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NetTally.Utility;

namespace NetTally.Tests
{
Expand Down Expand Up @@ -507,14 +509,14 @@ public void TallyVotesTest()
}

[TestMethod]
public void NameReferenceTest()
public async Task NameReferenceTest()
{
// Check for non-case sensitivity in referencing other voters.
PostComponents p1 = new PostComponents("Beyogi", "12345", "[x] Vote for something");
PostComponents p2 = new PostComponents("Mini", "12345", "[x] beyogi");
VoteCounter.Instance.PostsList.Add(p1);
VoteCounter.Instance.PostsList.Add(p2);
VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

Assert.AreEqual(2, VoteCounter.Instance.GetVotersCollection(VoteType.Vote).Count);
Assert.AreEqual(1, VoteCounter.Instance.GetVotesCollection(VoteType.Vote).Count);
Expand Down
8 changes: 4 additions & 4 deletions TallyUnitTest/VoteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Initialize()
#endregion

#region Test Harness
public void TestSinglePostParsing(string vote, List<string> results)
public async Task TestSinglePostParsing(string vote, List<string> results)
{
string author = "User1";
string postId = "123456";
Expand All @@ -38,7 +38,7 @@ public void TestSinglePostParsing(string vote, List<string> results)
PostComponents post = new PostComponents(author, postId, vote, postNumber);
VoteCounter.Instance.PostsList.Add(post);

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

var votes = GetVotesBy(author, VoteType.Vote);

Expand All @@ -51,7 +51,7 @@ public List<string> GetVotesBy(string author, VoteType voteType)
return votes.Where(v => v.Value.Contains(author)).Select(v => v.Key).ToList();
}

public void TestReferencePostParsing(List<string> votes, List<string> authors, List<List<string>> results)
public async Task TestReferencePostParsing(List<string> votes, List<string> authors, List<List<string>> results)
{
for (int i = 0; i < votes.Count; i++)
{
Expand All @@ -62,7 +62,7 @@ public void TestReferencePostParsing(List<string> votes, List<string> authors, L
VoteCounter.Instance.PostsList.Add(post);
}

VoteCounter.Instance.TallyPosts(sampleQuest);
await VoteCounter.Instance.TallyPosts(sampleQuest);

for (int i = 0; i < results.Count; i++)
{
Expand Down

0 comments on commit 413a019

Please sign in to comment.