Skip to content

Commit

Permalink
find correct index when updating collection (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored May 18, 2020
1 parent 06e6753 commit 0253cf2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ui/ViewModelCollection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,13 @@ public:
/// <returns>Index of the first matching item, <c>-1</c> if not found.</returns>
gsl::index FindItemIndex(const IntModelProperty& pProperty, int nValue) const
{
gsl::index nIndex = 0;
for (const auto& pItem : m_vItems)
{
if (pItem.HasViewModel() && pItem.ViewModel().GetValue(pProperty) == nValue)
return pItem.Index();
return nIndex;

++nIndex;
}

return -1;
Expand Down
5 changes: 5 additions & 0 deletions src/ui/win32/bindings/ComboBoxBinding.hh
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ protected:
ComboBox_InsertString(m_hWnd, nIndex, NativeStr(pLabel).c_str());
}

void OnViewModelRemoved(gsl::index nIndex) noexcept override
{
ComboBox_DeleteString(m_hWnd, nIndex);
}

private:
void PopulateComboBox()
{
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/ViewModelCollection_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,36 @@ TEST_CLASS(ViewModelCollection_Tests)
oNotify.AssertItemReplaced(0);
}

TEST_METHOD(TestFindItemIndexAfterRemovedWhileUpdateSuspended)
{
ViewModelCollection<TestViewModel> vmCollection;
vmCollection.Add(1, L"Test1");
vmCollection.Add(2, L"Test2");
vmCollection.Add(3, L"Test3");
vmCollection.Add(4, L"Test4");

vmCollection.BeginUpdate();

Assert::AreEqual({ 0 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 1));
Assert::AreEqual({ 1 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 2));
Assert::AreEqual({ 2 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 3));
Assert::AreEqual({ 3 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 4));

vmCollection.RemoveAt(1);

Assert::AreEqual({ 0 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 1));
Assert::AreEqual({ -1 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 2));
Assert::AreEqual({ 1 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 3));
Assert::AreEqual({ 2 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 4));

vmCollection.EndUpdate();

Assert::AreEqual({ 0 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 1));
Assert::AreEqual({ -1 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 2));
Assert::AreEqual({ 1 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 3));
Assert::AreEqual({ 2 }, vmCollection.FindItemIndex(TestViewModel::IntProperty, 4));
}

TEST_METHOD(TestMovePropertyNotification)
{
ViewModelCollection<TestViewModel> vmCollection;
Expand Down

0 comments on commit 0253cf2

Please sign in to comment.