// -----------------------------------------------------------------------
//
// Copyright 2015 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls.Generators
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using Perspex.Controls.Templates;
///
/// Creates containers for items and maintains a list of created containers.
///
public class ItemContainerGenerator : IItemContainerGenerator
{
private Dictionary containers = new Dictionary();
private Subject containersInitialized = new Subject();
///
/// Initializes a new instance of the class.
///
/// The owner control.
public ItemContainerGenerator(IControl owner)
{
this.Owner = owner;
}
///
/// Signalled whenever new containers are initialized.
///
public IObservable ContainersInitialized => this.containersInitialized;
///
/// Gets the owner control.
///
public IControl Owner { get; }
///
/// Creates container controls for a collection of items.
///
///
/// The index of the first item of the data in the containing collection.
///
/// The items.
/// An optional item template.
/// The created container controls.
public IList CreateContainers(
int startingIndex,
IEnumerable items,
IDataTemplate itemTemplate)
{
Contract.Requires(items != null);
int index = startingIndex;
var result = new List();
foreach (var item in items)
{
IControl container = this.CreateContainer(item, itemTemplate);
result.Add(container);
}
this.AddContainers(startingIndex, result);
this.containersInitialized.OnNext(new ItemContainers(startingIndex, result));
return result.Where(x => x != null).ToList();
}
///
/// Removes a set of created containers from the index and returns the removed controls.
///
///
/// The index of the first item of the data in the containing collection.
///
/// The items.
/// The removed controls.
public IList RemoveContainers(int startingIndex, IEnumerable items)
{
var result = new List();
var count = items.Cast