public UnityStatRandomizer([NotNull] ICardSettings cardSettings, [NotNull] IReadOnlyReactiveCollection <Card> cards) { this.cardSettings = cardSettings ?? throw new ArgumentNullException(nameof(cardSettings)); this.cards = cards ?? throw new ArgumentNullException(nameof(cards)); subscription = cards.ObserveRemove().Subscribe(OnRemoved); }
public WhereManyReactiveCollection(IReadOnlyReactiveCollection <T> source, Func <T, bool> filter, Func <T, IObservable <Unit> > filterChanged, IObservable <Unit> generalFilterChanged = null) { this.filter = filter; this.filterChanged = filterChanged; onAdd = source.ObserveAdd().Subscribe(@event => { OnAdd(@event.Value); }); onRemove = source.ObserveRemove().Subscribe(@event => { OnRemove(@event.Value); }); onReset = source.ObserveReset().Subscribe(_ => ClearItems()); onReset = source.ObserveReplace().Subscribe(@event => { OnRemove(@event.OldValue); OnAdd(@event.NewValue); }); if (generalFilterChanged != null) { generalFilterChangedBinding = generalFilterChanged.Subscribe(_ => { foreach (var element in source) { UpdateElement(element); } }); } foreach (var el in source) { OnAdd(el); } }
public void SetModel([NotNull] IReadOnlyReactiveCollection <Card> cards, [NotNull] CardViewFactory cardViewFactory) { if (cards == null) { throw new ArgumentNullException(nameof(cards)); } if (cardViewFactory == null) { throw new ArgumentNullException(nameof(cardViewFactory)); } ClearModel(); model = cards; removalCancellation = new CancellationTokenSource(); foreach (Card card in cards) { SubscribeToCard(card); CardView view = cardViewFactory.Create(card); cardViews.Add(card, view); } cards.ObserveRemove().Subscribe(OnRemoved).AddTo(collectionSubscriptions); PositionCards(); }
/// <summary> /// Connect this collection to another so that it reacts to additions and removals from the connected collection. /// </summary> /// <typeparam name="TTarget">The type of the connected collection.</typeparam> /// <param name="self">The collection to connect to and watch.</param> /// <param name="add">Called when an element is added to the connected collection.</param> /// <param name="remove">Called when an element is removed from the connected collection.</param> public static void ObserveChanges <TTarget>( this IReadOnlyReactiveCollection <TTarget> self, Action <CollectionAddEvent <TTarget> > add, Action <CollectionRemoveEvent <TTarget> > remove) { self.ObserveAdd().Subscribe(add); self.ObserveRemove().Subscribe(remove); }
public static IObservable <Unit> AnyCollectionChangeAsObservable <T>(this IReadOnlyReactiveCollection <T> reactiveCollection) { return(Observable.Merge( reactiveCollection.ObserveReset().AsUnitObservable(), reactiveCollection.ObserveAdd().AsUnitObservable(), reactiveCollection.ObserveMove().AsUnitObservable(), reactiveCollection.ObserveRemove().AsUnitObservable(), reactiveCollection.ObserveReplace().AsUnitObservable())); }
public static IObservable <CollectionAddRemoveEvent <T> > ObserveCurrentAddRemove <T>(this IReadOnlyReactiveCollection <T> This) => This.Select((x, i) => new CollectionAddRemoveEvent <T>(i, x, true)) .ToObservable() .Concat(This .ObserveAdd() .Select(x => new CollectionAddRemoveEvent <T>(x.Index, x.Value, true)) .Merge(This .ObserveRemove() .Select(x => new CollectionAddRemoveEvent <T>(x.Index, x.Value, false))));
public SortedReactiveCollection(IReadOnlyReactiveCollection <T> source, System.Func <T, T, int> comparer, IObservable <Unit> observableToReact) { this.comparer = comparer; source.ObserveAdd().Subscribe(@event => { AddSorted(@event.Value); }); source.ObserveRemove().Subscribe(@event => { Remove(@event.Value); }); foreach (var el in source) { AddSorted(el); } }
public CollectionBinder(IReadOnlyReactiveCollection <T> source, BaseListItem <T> prefab, Transform parent) { this.source = source; this.prefab = prefab; this.parent = parent; prefab.gameObject.SetActive(false); onAdd = source.ObserveAdd().Subscribe(@event => UpdateCollection()); onMove = source.ObserveMove().Subscribe(@event => UpdateCollection()); onReset = source.ObserveReset().Subscribe(@event => UpdateCollection()); onRemove = source.ObserveRemove().Subscribe(@event => UpdateCollection()); onReplace = source.ObserveReplace().Subscribe(@event => UpdateCollection()); UpdateCollection(); }
public WhereReactiveCollection(IReadOnlyReactiveCollection <T> source, Func <T, bool> filter, IObservable <Unit> filterChanged) { this.filter = filter; onAdd = source.ObserveAdd().Subscribe(@event => { if (filter(@event.Value)) { Add(@event.Value); } }); onRemove = source.ObserveRemove().Subscribe(@event => { Remove(@event.Value); }); onReset = source.ObserveReset().Subscribe(_ => ClearItems()); onReset = source.ObserveReplace().Subscribe(@event => { Remove(@event.OldValue); if (filter(@event.NewValue)) { Add(@event.NewValue); } }); filterChanged.Subscribe(_ => { foreach (var element in source) { if (filter(element)) { if (!Contains(element)) { Add(element); } } else { Remove(element); } } }); }
public static IDisposable BindChildPrefabsTo <T>(this GameObject input, IReadOnlyReactiveCollection <T> list, GameObject prefab, Func <GameObject, Transform, GameObject> instantiator, Action <T, GameObject> onChildCreated = null, Action <T, GameObject> onChildRemoving = null) { var disposable = new CompositeDisposable(); void onElementAdded(CollectionAddEvent <T> data) { var newChild = instantiator(prefab, input.transform); onChildCreated?.Invoke(data.Value, newChild); } void onElementUpdated(CollectionReplaceEvent <T> data) { var existingChild = input.transform.GetChild(data.Index); onChildCreated?.Invoke(data.NewValue, existingChild.gameObject); } void onElementRemoved(CollectionRemoveEvent <T> data) { var existingChild = input.transform.GetChild(data.Index); onChildRemoving?.Invoke(data.Value, existingChild.gameObject); GameObject.Destroy(existingChild); } list.ObserveAdd().Subscribe(onElementAdded).AddTo(disposable); list.ObserveReplace().Subscribe(onElementUpdated).AddTo(disposable); list.ObserveRemove().Subscribe(onElementRemoved).AddTo(disposable); input.transform.DeleteAllChildren(); foreach (var element in list) { var newChild = instantiator(prefab, input.transform); onChildCreated?.Invoke(element, newChild); } return(disposable.AddTo(input)); }
public ReactiveCollectionView(IReadOnlyReactiveCollection <T> collection, IFactory <TView> factory, Action <TView, T> initializer) : base(collection, factory, initializer) { collection.ObserveAdd().Subscribe(addEvent => Add(addEvent.Index, addEvent.Value)).AddTo(Disposable); collection.ObserveRemove().Subscribe(removeEvent => Remove(removeEvent.Index)).AddTo(Disposable); }