예제 #1
0
        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);
            }
        }
예제 #2
0
        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>
 /// Iterates through the collection on subscription, and then pumps add and replace events.
 /// </summary>
 /// <param name="collection"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static IObservable <T> Observe <T>(this IReadOnlyReactiveCollection <T> collection)
 {
     return(Observable.Merge(
                collection.ToObservable(),
                collection.ObserveAdd().Select(e => e.Value),
                collection.ObserveReplace().Select(e => e.NewValue)));
 }
예제 #4
0
 public static IObservable <T> ToObservableAndAdded <T>(this IReadOnlyReactiveCollection <T> collection)
 {
     return(collection.ToObservable()
            .Merge(collection
                   .ObserveAdd()
                   .Select(added => added.Value)));
 }
예제 #5
0
        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);
        }
예제 #6
0
 public static void Bind <T>(this IBinder This, IReadOnlyReactiveCollection <T> source, ListControl target)
 {
     if (target != null)
     {
         This.Bind(source.ObserveCurrentAddRemove(), target);
     }
 }
예제 #7
0
 /// <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);
 }
예제 #8
0
 public MonoBehaviourReactiveCollectionView(IReadOnlyReactiveCollection <T> collection, TView template, Transform parentTransform, Action <TView, T> initializer)
 {
     _collectionView = new ReactiveCollectionView <T, TView>(collection, new Instantiator <TView>(template), (view, element) =>
     {
         view.transform.SetParent(parentTransform, worldPositionStays: false);
         initializer(view, element);
     });
 }
예제 #9
0
 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()));
 }
 /// <summary>
 /// Iterates through the collection on subscription, and pumps add and replace events with the index and the new
 /// document always in the new value field.
 /// </summary>
 /// <param name="collection"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static IObservable <CollectionReplaceEvent <T> > ObserveChanges <T>(
     this IReadOnlyReactiveCollection <T> collection)
 {
     return(Observable.Merge(
                Observable.Range(0, collection.Count)
                .Select(i => new CollectionReplaceEvent <T>(i, default(T), collection[i])),
                collection.ObserveAdd().Select(e => new CollectionReplaceEvent <T>(e.Index, default(T), e.Value)),
                collection.ObserveReplace()
                ));
 }
예제 #11
0
        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);
            }
        }
예제 #12
0
        public void TestReactiveCollection()
        {
            var _nums = new ReactiveCollection <int>();
            IReadOnlyReactiveCollection <int> nums = _nums;

            nums.ObserveAdd().Subscribe(Add);
            nums.ObserveAdd().Subscribe(Add);
            _nums.Add(42);

            Assert.AreEqual(0, _index);
            Assert.AreEqual(42 * 2, _added);
        }
예제 #13
0
        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();
        }
예제 #14
0
        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);
                    }
                }
            });
        }
예제 #15
0
        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));
        }
예제 #16
0
        public void ClearModel()
        {
            foreach (CardView cardView in cardViews.Values)
            {
                cardView.ClearModel();
                Destroy(cardView.gameObject);
            }

            cardViews.Clear();

            foreach (IDisposable subscription in subscriptions.Values)
            {
                subscription.Dispose();
            }

            subscriptions.Clear();
            collectionSubscriptions.Clear();
            model = null;

            if (removalCancellation != null && removalCancellation.Token.CanBeCanceled)
            {
                removalCancellation.Cancel();
            }
        }
 public static IDisposable BindTo <T>(this IReadOnlyReactiveCollection <T> This, ListControl listControl) =>
 This.ObserveCurrentAddRemove().BindTo(listControl);
예제 #18
0
 public static IDisposable BindChildPrefabsTo <T>(this GameObject input, IReadOnlyReactiveCollection <T> list,
                                                  GameObject prefab, Action <T, GameObject> onChildCreated = null, Action <T, GameObject> onChildRemoving = null)
 {
     return(BindChildPrefabsTo(input, list, prefab, GameObject.Instantiate, onChildCreated, onChildRemoving));
 }
 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))));
예제 #20
0
 public static IReadOnlyReactiveCollection <T> Where <T>(this IReadOnlyReactiveCollection <T> source,
                                                         System.Func <T, bool> filter, IObservable <Unit> filterChanged)
 {
     return(new WhereReactiveCollection <T>(source, filter, filterChanged));
 }
예제 #21
0
 public static IReadOnlyReactiveCollection <T> Sort <T>(this IReadOnlyReactiveCollection <T> source,
                                                        Func <T, T, int> comparer, IObservable <Unit> observable)
 {
     return(new SortedReactiveCollection <T>(source, comparer, observable));
 }
예제 #22
0
 public static IReadOnlyReactiveCollection <T> WhereMany <T>(this IReadOnlyReactiveCollection <T> source,
                                                             System.Func <T, bool> filter, Func <T, IObservable <Unit> > filterChanged, IObservable <Unit> generalFilterChanged = null)
 {
     return(new WhereManyReactiveCollection <T>(source, filter, filterChanged, generalFilterChanged));
 }
예제 #23
0
 public static MonoBehaviourReactiveCollectionView <T, TView> CreateView <T, TView>(this IReadOnlyReactiveCollection <T> collection, TView template, Transform parentTransform, Action <TView, T> initializer)
     where TView : MonoBehaviour, IDisposable => new MonoBehaviourReactiveCollectionView <T, TView>(collection, template, parentTransform, initializer);
예제 #24
0
 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);
 }
 public UIItemsViewModel(IReadOnlyReactiveCollection <ItemModel> items)
 {
     Items = items;
 }