using System; using System.Collections; using System.Collections.Generic; namespace InABox.Core { public class FluentList : IEnumerable { public delegate void FluentListChangedEvent(object sender, EventArgs args); private bool _enabled = true; private readonly List _values = new List(); public bool Enabled { get => _enabled; set { if (value) EndUpdate(); else BeginUpdate(); } } public T this[int i] { get => _values[i]; set { _values[i] = value; Changed(); } } public int Count => _values.Count; public IEnumerator GetEnumerator() { return ((IEnumerable)_values).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)_values).GetEnumerator(); } public FluentList Add(T value) { if (!_values.Contains(value)) _values.Add(value); return Changed(); } public FluentList AddRange(params T[] values) { foreach (var value in values) if (!_values.Contains(value)) _values.Add(value); return Changed(); } public FluentList AddRange(IEnumerable values) { foreach (var value in values) if (!_values.Contains(value)) _values.Add(value); return Changed(); } public FluentList Remove(T value) { if (_values.Contains(value)) _values.Remove(value); return Changed(); } public FluentList RemoveAt(int index) { if (index > 0 && index < _values.Count) _values.RemoveAt(index); return Changed(); } public FluentList Clear() { _values.Clear(); return Changed(); } public event FluentListChangedEvent OnChanged; public FluentList BeginUpdate() { _enabled = false; return this; } private FluentList Changed() { if (_enabled) OnChanged?.Invoke(this, new EventArgs()); return this; } public FluentList EndUpdate() { _enabled = true; return Changed(); } } }