Ejemplo n.º 1
0
        /// <summary>
        /// <para>Extension method that binds an observable of a list of collection
        /// sections as the source of a <see cref="UICollectionView"/>.</para>
        /// <para>If your <see cref="IReadOnlyList{T}"/> is also an instance of
        /// <see cref="INotifyCollectionChanged"/>, then this method
        /// will silently update the bindings whenever it changes as well.
        /// Otherwise, it will just log a message.</para>
        /// </summary>
        /// <returns>The <see cref="IDisposable"/> used to dispose this binding.</returns>
        /// <param name="sectionsObservable">Sections observable.</param>
        /// <param name="collectionView">Collection view.</param>
        /// <param name="initSource">Optionally initializes some property of
        /// the <see cref="ReactiveCollectionViewSource{TSource}"/>.</param>
        /// <typeparam name="TSource">Type of the view source.</typeparam>
        /// <typeparam name="TCell">Type of the <see cref="UICollectionViewCell"/>.</typeparam>
        public static IDisposable BindTo <TSource, TCell>(
            this IObservable <IReadOnlyList <CollectionViewSectionInformation <TSource, TCell> > > sectionsObservable,
            UICollectionView collectionView,
            Func <ReactiveCollectionViewSource <TSource>, IDisposable>?initSource = null)
            where TCell : UICollectionViewCell
        {
            if (sectionsObservable is null)
            {
                throw new ArgumentNullException(nameof(sectionsObservable));
            }

            if (collectionView is null)
            {
                throw new ArgumentNullException(nameof(collectionView));
            }

            var source = new ReactiveCollectionViewSource <TSource>(collectionView);

            initSource?.Invoke(source);

            var bind = sectionsObservable.BindTo(source, x => x.Data);

            collectionView.Source = source;
            return(new CompositeDisposable(bind, source));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// <para>Extension method that binds an observable of a list of collection
        /// sections as the source of a <see cref="UICollectionView"/>.</para>
        /// <para>If your <see cref="IReadOnlyList"/> is also an instance of
        /// <see cref="IReactiveNotifyCollectionChanged"/>, then this method
        /// will silently update the bindings whenever it changes as well.
        /// Otherwise, it will just log a message.</para>
        /// </summary>
        /// <returns>The <see cref="IDisposable"/> used to dispose this binding.</returns>
        /// <param name="sectionsObservable">Sections observable.</param>
        /// <param name="collectionView">Collection view.</param>
        /// <param name="initSource">Optionally initializes some property of
        /// the <see cref="ReactiveCollectionViewSource"/>.</param>
        /// <typeparam name="TCell">Type of the <see cref="UICollectionViewCell"/>.</typeparam>
        public static IDisposable BindTo <TSource, TCell>(
            this IObservable <IReadOnlyList <CollectionViewSectionInformation <TSource, TCell> > > sectionsObservable,
            UICollectionView collectionView,
            Func <ReactiveCollectionViewSource <TSource>, IDisposable> initSource = null)
            where TCell : UICollectionViewCell
        {
            var source = new ReactiveCollectionViewSource <TSource>(collectionView);

            if (initSource != null)
            {
                initSource(source);
            }
            var bind = sectionsObservable.BindTo(source, x => x.Data);

            collectionView.Source = source;
            return(new CompositeDisposable(bind, source));
        }