Skip to content

Commit

Permalink
Merge pull request #88 from ChrisPulman/FixForClearRemoveAtConflict
Browse files Browse the repository at this point in the history
Fix Conflict in Collection IList - Clear RemoveAt
  • Loading branch information
ChrisPulman authored Oct 12, 2024
2 parents 78c89b2 + a262b90 commit 07653b2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "2.0.3",
"version": "2.0.4",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
6 changes: 0 additions & 6 deletions global.json

This file was deleted.

11 changes: 11 additions & 0 deletions src/ReactiveList/IReactiveList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,23 @@ public interface IReactiveList<T> : IList<T>, IList, IReadOnlyList<T>, INotifyCo
/// </value>
IObservable<IEnumerable<T>> Removed { get; }

/// <summary>
/// Removes the <see cref="T:System.Collections.Generic.IList`1"></see> item at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the item to remove.</param>
new void RemoveAt(int index);

/// <summary>
/// Adds the range.
/// </summary>
/// <param name="items">The items.</param>
void AddRange(IEnumerable<T> items);

/// <summary>
/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
/// </summary>
new void Clear();

/// <summary>
/// Replaces all existing items with new items.
/// </summary>
Expand Down
49 changes: 35 additions & 14 deletions src/ReactiveList/ReactiveList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,6 @@ public void AddRange(IEnumerable<T> items)
}
}

/// <summary>
/// Clears this instance.
/// </summary>
public void Clear()
{
lock (_lock)
{
ClearHistoryIfCountIsZero();
_sourceList.Edit(l => l.Clear());
OnPropertyChanged(nameof(Count));
OnPropertyChanged("Item[]");
}
}

/// <summary>
/// Determines whether this instance contains the object.
/// </summary>
Expand Down Expand Up @@ -505,6 +491,18 @@ public void Insert(int index, T item)
}
}

/// <summary>
/// Removes at.
/// </summary>
/// <param name="index">The index.</param>
void IList<T>.RemoveAt(int index) => RemoveAt(index);

/// <summary>
/// Removes at.
/// </summary>
/// <param name="index">The index.</param>
void IList.RemoveAt(int index) => RemoveAt(index);

/// <summary>
/// Removes at.
/// </summary>
Expand Down Expand Up @@ -685,6 +683,29 @@ public void CopyTo(Array array, int index)
}
}

/// <summary>
/// Clears this instance.
/// </summary>
void ICollection<T>.Clear() => Clear();
/// <summary>
/// Clears this instance.
/// </summary>
void IList.Clear() => Clear();

/// <summary>
/// Clears this instance.
/// </summary>
public void Clear()
{
lock (_lock)
{
ClearHistoryIfCountIsZero();
_sourceList.Edit(l => l.Clear());
OnPropertyChanged(nameof(Count));
OnPropertyChanged("Item[]");
}
}

/// <summary>
/// Raises a PropertyChanged event (per <see cref="INotifyPropertyChanged" />).
/// </summary>
Expand Down
10 changes: 8 additions & 2 deletions src/ReactiveListTestApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
Title="MainWindow"
Width="800"
Height="450"
d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel}"
mc:Ignorable="d">
<Grid>
<StackPanel>
<Button
Command="{Binding AddItemCommand}"
CommandParameter="INJECTED VALUE "
Content="Add Item" />
<Button Command="{Binding ClearItemsCommand}" Content="CLear Items" />
<ListBox ItemsSource="{Binding Items}" />
</Grid>
</StackPanel>
</Window>
32 changes: 28 additions & 4 deletions src/ReactiveListTestApp/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using CP.Reactive;
using CrissCross;
using ReactiveUI;

namespace ReactiveListTestApp;

internal class MainWindowViewModel
internal class MainWindowViewModel : RxObject
{
/// <summary>
/// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
Expand All @@ -32,12 +35,33 @@ public MainWindowViewModel()
Items.Remove("Lets");
Items.Remove("Count");
Items.RemoveAt(0);
Items.RemoveAt(0);

var l = Items as IList<string>;
l.RemoveAt(0);

Items.Add(x);
i++;
}
});
}).DisposeWith(Disposables);

var ii = 0;
AddItemCommand = ReactiveCommand.Create<string>(x => Items.Add($"{x}{ii++}")).DisposeWith(Disposables);
ClearItemsCommand = ReactiveCommand.Create(() => Items.Clear()).DisposeWith(Disposables);
}

public ReactiveList<string> Items { get; } = [];
public IReactiveList<string> Items { get; } = new ReactiveList<string>();

public ReactiveCommand<string, Unit> AddItemCommand { get; }

public ReactiveCommand<Unit, Unit> ClearItemsCommand { get; }

protected override void Dispose(bool disposing)
{
if (disposing)
{
Items.Dispose();
}

base.Dispose(disposing);
}
}
2 changes: 1 addition & 1 deletion src/ReactiveListTestApp/ReactiveListTestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
Expand Down

0 comments on commit 07653b2

Please sign in to comment.