-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Cysharp/hadashiA/initial-elements-add
Support for firing add event for initial elements
- Loading branch information
Showing
30 changed files
with
501 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,66 @@ | ||
using ObservableCollections; | ||
using System; | ||
using System.Collections.Specialized; | ||
using System; | ||
using System.Linq; | ||
using ObservableCollections; | ||
|
||
var models = new ObservableList<int>(Enumerable.Range(0, 10)); | ||
|
||
var viewModels = models.CreateView(x => new ViewModel | ||
{ | ||
Id = x, | ||
Value = "@" + x | ||
}); | ||
|
||
viewModels.AttachFilter(new HogeFilter(), true); | ||
|
||
// Basic sample, use like ObservableCollection<T>. | ||
// CollectionChanged observes all collection modification | ||
var list = new ObservableList<int>(); | ||
var view = list.CreateView(x => x.ToString() + "$"); | ||
models.Add(100); | ||
|
||
list.Add(10); | ||
list.Add(20); | ||
list.AddRange(new[] { 30, 40, 50 }); | ||
list[1] = 60; | ||
list.RemoveAt(3); | ||
foreach (var (x, xs) in viewModels) | ||
{ | ||
System.Console.WriteLine(xs.Value); | ||
} | ||
|
||
foreach (var (_, v) in view) | ||
class ViewModel | ||
{ | ||
// 10$, 60$, 30$, 50$ | ||
Console.WriteLine(v); | ||
public int Id { get; set; } | ||
public string Value { get; set; } | ||
} | ||
|
||
// Dispose view is unsubscribe collection changed event. | ||
view.Dispose(); | ||
class HogeFilter : ISynchronizedViewFilter<int, ViewModel> | ||
{ | ||
public bool IsMatch(int value, ViewModel view) | ||
{ | ||
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( | ||
ChangedKind changedKind, | ||
int value, | ||
ViewModel view, | ||
in NotifyCollectionChangedEventArgs<int> eventArgs) | ||
{ | ||
switch (changedKind) | ||
{ | ||
case ChangedKind.Add: | ||
view.Value += " Add"; | ||
break; | ||
case ChangedKind.Remove: | ||
view.Value += " Remove"; | ||
break; | ||
case ChangedKind.Move: | ||
view.Value += $" Move {eventArgs.OldStartingIndex} {eventArgs.NewStartingIndex}"; | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(changedKind), changedKind, null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.