Skip to content

Commit

Permalink
Merge pull request #65 from Cysharp/rework-filter-and-view
Browse files Browse the repository at this point in the history
Rework filter and view(v3)
  • Loading branch information
neuecc authored Sep 3, 2024
2 parents 3488094 + e19ebb4 commit 074f52a
Show file tree
Hide file tree
Showing 47 changed files with 3,515 additions and 2,817 deletions.
473 changes: 286 additions & 187 deletions README.md

Large diffs are not rendered by default.

Binary file modified docs/assets.pptx
Binary file not shown.
2 changes: 1 addition & 1 deletion sandbox/AvaloniaApp/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MainWindow()
public class ViewModel
{
private ObservableList<int> observableList { get; } = new ObservableList<int>();
public INotifyCollectionChangedSynchronizedView<int> ItemsView { get; }
public INotifyCollectionChangedSynchronizedViewList<int> ItemsView { get; }
public ReactiveCommand<Unit> AddCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> ClearCommand { get; } = new ReactiveCommand<Unit>();

Expand Down
16 changes: 5 additions & 11 deletions sandbox/BlazorApp/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class Index : IDisposable
{
ObservableList<int> list;
public ISynchronizedView<int, int> ItemsView { get; set; }
int adder = 99;
int count = 99;

protected override void OnInitialized()
{
Expand All @@ -20,19 +20,13 @@ protected override void OnInitialized()
};
}

public void Dispose()
void OnClick()
{
ItemsView.Dispose();
list.Add(count++);
}




void OnClick()
public void Dispose()
{
ThreadPool.QueueUserWorkItem(_ =>
{
list.Add(adder++);
});
ItemsView.Dispose();
}
}
65 changes: 25 additions & 40 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,38 @@
using R3;
using System.Linq;
using ObservableCollections;
using System.Collections;
using System.Collections.Generic;




var list = new ObservableList<int>();
list.ObserveAdd()
.Subscribe(x =>
{
Console.WriteLine(x);
});

list.Add(10);
list.Add(20);
list.AddRange(new[] { 10, 20, 30 });


// Queue <-> List Synchronization
var queue = new ObservableQueue<int>();

queue.Enqueue(1);
queue.Enqueue(10);
queue.Enqueue(100);
queue.Enqueue(1000);
queue.Enqueue(10000);

using var view = queue.CreateView(x => x.ToString() + "$");

using var viewList = view.ToViewList();

Console.WriteLine(viewList[2]); // 100$


var models = new ObservableList<int>(Enumerable.Range(0, 10));
view.ViewChanged += View_ViewChanged;

var viewModels = models.CreateView(x => new ViewModel
void View_ViewChanged(in SynchronizedViewChangedEventArgs<int, string> eventArgs)
{
Id = x,
Value = "@" + x
});

viewModels.AttachFilter(new HogeFilter(), true);

models.Add(100);
if (eventArgs.Action == NotifyCollectionChangedAction.Add)
{
// eventArgs.OldItem.View.
}

foreach (var (x, xs) in viewModels)
{
System.Console.WriteLine(xs.Value);
throw new NotImplementedException();
}

class ViewModel
Expand All @@ -48,38 +43,28 @@ class ViewModel
public string Value { get; set; } = default!;
}

class HogeFilter : ISynchronizedViewFilter<int, ViewModel>
class HogeFilter : ISynchronizedViewFilter<int>
{
public bool IsMatch(int value, ViewModel view)
public bool IsMatch(int value)
{
return value % 2 == 0;
}

public void WhenTrue(int value, ViewModel view)
{
view.Value = $"@{value} (even)";
}

public void WhenFalse(int value, ViewModel view)
{
view.Value = $"@{value} (odd)";
}

public void OnCollectionChanged(in SynchronizedViewChangedEventArgs<int, ViewModel> eventArgs)
{
switch (eventArgs.Action)
{
case NotifyCollectionChangedAction.Add:
eventArgs.NewView.Value += " Add";
eventArgs.NewItem.View.Value += " Add";
break;
case NotifyCollectionChangedAction.Remove:
eventArgs.OldView.Value += " Remove";
eventArgs.OldItem.View.Value += " Remove";
break;
case NotifyCollectionChangedAction.Move:
eventArgs.NewView.Value += $" Move {eventArgs.OldViewIndex} {eventArgs.NewViewIndex}";
eventArgs.NewItem.View.Value += $" Move {eventArgs.OldStartingIndex} {eventArgs.NewStartingIndex}";
break;
case NotifyCollectionChangedAction.Replace:
eventArgs.NewView.Value += $" Replace {eventArgs.NewViewIndex}";
eventArgs.NewItem.View.Value += $" Replace {eventArgs.NewStartingIndex}";
break;
case NotifyCollectionChangedAction.Reset:
break;
Expand Down
6 changes: 6 additions & 0 deletions sandbox/WpfApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<StackPanel>
<ListBox ItemsSource="{Binding ItemsView}" />
<Button Content="Add" Command="{Binding AddCommand}" />
<Button Content="Insert" Command="{Binding InsertAtRandomCommand}" />
<Button Content="Remove" Command="{Binding RemoveAtRandomCommand}" />
<Button Content="Clear" Command="{Binding ClearCommand}" />
<Button Content="Reverse" Command="{Binding ReverseCommand}" />
<Button Content="Sort" Command="{Binding SortCommand}" />
<Button Content="AttachFilter" Command="{Binding AttachFilterCommand}" />
<Button Content="ResetFilter" Command="{Binding ResetFilterCommand}" />
</StackPanel>
</Window>
50 changes: 44 additions & 6 deletions sandbox/WpfApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,28 @@ public MainWindow()
public class ViewModel
{
private ObservableList<int> observableList { get; } = new ObservableList<int>();
public INotifyCollectionChangedSynchronizedView<int> ItemsView { get; }
public INotifyCollectionChangedSynchronizedViewList<int> ItemsView { get; }
public ReactiveCommand<Unit> AddCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> InsertAtRandomCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> RemoveAtRandomCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> ClearCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> ReverseCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> SortCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> AttachFilterCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> ResetFilterCommand { get; } = new ReactiveCommand<Unit>();

public ViewModel()
{
observableList.Add(1);
observableList.Add(2);

ItemsView = observableList.CreateView(x => x).ToNotifyCollectionChanged(SynchronizationContextCollectionEventDispatcher.Current);
var view = observableList.CreateView(x => x);
ItemsView = view.ToNotifyCollectionChanged(SynchronizationContextCollectionEventDispatcher.Current);


// ItemsView = observableList.CreateView(x => x).ToNotifyCollectionChanged();
// check for optimize list
// ItemsView = observableList.ToNotifyCollectionChanged(SynchronizationContextCollectionEventDispatcher.Current);

// BindingOperations.EnableCollectionSynchronization(ItemsView, new object());

AddCommand.Subscribe(_ =>
{
Expand All @@ -98,12 +105,43 @@ public ViewModel()
});
});

// var iii = 10;
InsertAtRandomCommand.Subscribe(_ =>
{
var from = Random.Shared.Next(0, view.Count);
observableList.Insert(from, Random.Shared.Next());
});

RemoveAtRandomCommand.Subscribe(_ =>
{
var from = Random.Shared.Next(0, view.Count);
observableList.RemoveAt(from);
});

ClearCommand.Subscribe(_ =>
{
// observableList.Add(iii++);
observableList.Clear();
});


ReverseCommand.Subscribe(_ =>
{
observableList.Reverse();
});

SortCommand.Subscribe(_ =>
{
observableList.Sort();
});

AttachFilterCommand.Subscribe(_ =>
{
view.AttachFilter(x => x % 2 == 0);
});

ResetFilterCommand.Subscribe(_ =>
{
view.ResetFilter();
});
}
}

Expand Down
Loading

0 comments on commit 074f52a

Please sign in to comment.