Refactoring results update

This commit is contained in:
bao-qian 2016-05-07 20:08:27 +01:00
parent 02d44a6cca
commit 192e4b8877
2 changed files with 57 additions and 67 deletions

View File

@ -629,7 +629,6 @@ namespace Wox.ViewModel
{ {
ResultListBoxVisibility = Visibility.Visible; ResultListBoxVisibility = Visibility.Visible;
} }
} }
#endregion #endregion

View File

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Data; using System.Windows.Data;
@ -140,49 +138,10 @@ namespace Wox.ViewModel
{ {
lock (_addResultsLock) lock (_addResultsLock)
{ {
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList(); var newResults = NewResults(newRawResults, resultId);
// todo use async to do new result calculation
var resultsCopy = Results.ToList();
var oldResults = resultsCopy.Where(r => r.PluginID == resultId).ToList();
// intersection of A (old results) and B (new newResults)
var intersection = oldResults.Intersect(newResults).ToList();
// remove result of relative complement of B in A
foreach (var result in oldResults.Except(intersection))
{
resultsCopy.Remove(result);
}
// update index for result in intersection of A and B
foreach (var commonResult in intersection)
{
int oldIndex = resultsCopy.IndexOf(commonResult);
int oldScore = resultsCopy[oldIndex].Score;
var newResult = newResults[newResults.IndexOf(commonResult)];
int newScore = newResult.Score;
if (newScore != oldScore)
{
var oldResult = resultsCopy[oldIndex];
oldResult.Score = newScore;
oldResult.OriginQuery = newResult.OriginQuery;
resultsCopy.RemoveAt(oldIndex);
int newIndex = InsertIndexOf(newScore, resultsCopy);
resultsCopy.Insert(newIndex, oldResult);
}
}
// insert result in relative complement of A in B
foreach (var result in newResults.Except(intersection))
{
int newIndex = InsertIndexOf(result.Score, resultsCopy);
resultsCopy.Insert(newIndex, result);
}
// update UI in one run, so it can avoid UI flickering // update UI in one run, so it can avoid UI flickering
Results.Update(resultsCopy); Results.Update(newResults);
if (Results.Count > 0) if (Results.Count > 0)
{ {
@ -196,6 +155,51 @@ namespace Wox.ViewModel
} }
} }
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId)
{
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
var results = Results.ToList();
var oldResults = results.Where(r => r.PluginID == resultId).ToList();
// intersection of A (old results) and B (new newResults)
var intersection = oldResults.Intersect(newResults).ToList();
// remove result of relative complement of B in A
foreach (var result in oldResults.Except(intersection))
{
results.Remove(result);
}
// update index for result in intersection of A and B
foreach (var commonResult in intersection)
{
int oldIndex = results.IndexOf(commonResult);
int oldScore = results[oldIndex].Score;
var newResult = newResults[newResults.IndexOf(commonResult)];
int newScore = newResult.Score;
if (newScore != oldScore)
{
var oldResult = results[oldIndex];
oldResult.Score = newScore;
oldResult.OriginQuery = newResult.OriginQuery;
results.RemoveAt(oldIndex);
int newIndex = InsertIndexOf(newScore, results);
results.Insert(newIndex, oldResult);
}
}
// insert result in relative complement of A in B
foreach (var result in newResults.Except(intersection))
{
int newIndex = InsertIndexOf(result.Score, results);
results.Insert(newIndex, result);
}
return results;
}
#endregion #endregion
@ -206,21 +210,13 @@ namespace Wox.ViewModel
{ {
CheckReentrancy(); CheckReentrancy();
List<ResultViewModel> itemsToRemove = Items.Where(x => predicate(x)).ToList(); for (int i = Count - 1; i >= 0; i--)
if (itemsToRemove.Count > 0)
{ {
itemsToRemove.ForEach(item => { Items.Remove(item); }); if (predicate(this[i]))
{
OnPropertyChanged(new PropertyChangedEventArgs("Count")); RemoveAt(i);
OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); }
// fuck ms
// http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx
// http://geekswithblogs.net/NewThingsILearned/archive/2008/01/16/listcollectionviewcollectionview-doesnt-support-notifycollectionchanged-with-multiple-items.aspx
// PS: don't use Reset for other data updates, it will cause UI flickering
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
} }
} }
public void Update(List<ResultViewModel> newItems) public void Update(List<ResultViewModel> newItems)
@ -228,13 +224,13 @@ namespace Wox.ViewModel
int newCount = newItems.Count; int newCount = newItems.Count;
int oldCount = Items.Count; int oldCount = Items.Count;
int location = newCount > oldCount ? oldCount : newCount; int location = newCount > oldCount ? oldCount : newCount;
for (int i = 0; i < location; i++) for (int i = 0; i < location; i++)
{ {
ResultViewModel oldResult = Items[i]; ResultViewModel oldResult = this[i];
ResultViewModel newResult = newItems[i]; ResultViewModel newResult = newItems[i];
if (!oldResult.Equals(newResult)) if (!oldResult.Equals(newResult))
{ {
this[i] = newResult; this[i] = newResult;
} }
else if (oldResult.Score != newResult.Score) else if (oldResult.Score != newResult.Score)
@ -244,7 +240,7 @@ namespace Wox.ViewModel
} }
if (newCount > oldCount) if (newCount >= oldCount)
{ {
for (int i = oldCount; i < newCount; i++) for (int i = oldCount; i < newCount; i++)
{ {
@ -253,17 +249,12 @@ namespace Wox.ViewModel
} }
else else
{ {
int removeIndex = newCount; for (int i = oldCount - 1; i >= newCount; i--)
for (int i = newCount; i < oldCount; i++)
{ {
RemoveAt(removeIndex); RemoveAt(i);
} }
} }
} }
} }
public ResultViewModel this[int selectedIndex] => Results[selectedIndex];
} }
} }