using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using InABox.Core; namespace InABox.Core { public class DFLayoutEmbeddedMediaValues : IEnumerable, INotifyPropertyChanged, IDFLayoutValue>, INotifyCollectionChanged, IList { private readonly ObservableCollection _values = new ObservableCollection(); public DFLayoutEmbeddedMediaValue[] Values => _values.ToArray(); public bool Present { get; set; } = false; public List AsByteArrays { get => _values.Select(x => x?.Data).OfType().ToList(); set { _values.Clear(); CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); foreach (var bytes in value) { var v = new DFLayoutEmbeddedMediaValue() { ID = Guid.Empty, Data = bytes }; _values.Add(v); CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, v)); } OnPropertyChanged(nameof(Values)); } } public DFLayoutEmbeddedMediaValues() { _values.CollectionChanged += _values_CollectionChanged; } private void _values_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { CollectionChanged?.Invoke(this, e); } public void Serialize(DFSaveStorageEntry storage) { var results = new List(); foreach (var value in _values) results.Add(Serialization.Serialize((value.ID, value.Thumbnail))); if (results.Any()) storage.SetBlobValue(results); } public void Deserialize(DFLoadStorageEntry storage) { _values.Clear(); Present = storage.HasValue(); if (Present) { var values = storage.GetValue() ?? Array.Empty(); foreach (string s in values) { if (!s.IsNullOrWhiteSpace()) { var externaldata = Serialization.Deserialize<(Guid, byte[])>(s); if (externaldata.Item1 != Guid.Empty) { _values.Add(new DFLayoutEmbeddedMediaValue() { ID = externaldata.Item1, Thumbnail = externaldata.Item2 }); } } } } } public IEnumerator GetEnumerator() => _values.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public void Add(DFLayoutEmbeddedMediaValue value) { _values.Add(value); OnPropertyChanged(nameof(Values)); } public void Remove(DFLayoutEmbeddedMediaValue value) { if (_values.Contains(value)) { _values.Remove(value); OnPropertyChanged(nameof(Values)); } } public void Clear() { _values.Clear(); OnPropertyChanged(nameof(Values)); } public event PropertyChangedEventHandler? PropertyChanged; public event NotifyCollectionChangedEventHandler? CollectionChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected bool SetField(ref T field, T value, [CallerMemberName] string propertyName = "") { if (EqualityComparer.Default.Equals(field, value)) return false; field = value; OnPropertyChanged(propertyName); return true; } #region IList public int Count => ((ICollection)_values).Count; public bool IsReadOnly => ((ICollection)_values).IsReadOnly; public bool IsFixedSize => ((IList)_values).IsFixedSize; public bool IsSynchronized => ((ICollection)_values).IsSynchronized; public object SyncRoot => ((ICollection)_values).SyncRoot; object IList.this[int index] { get => ((IList)_values)[index]; set => ((IList)_values)[index] = value; } public int Add(object value) { return ((IList)_values).Add(value); } public bool Contains(object value) { return ((IList)_values).Contains(value); } public int IndexOf(object value) { return ((IList)_values).IndexOf(value); } public void Insert(int index, object value) { ((IList)_values).Insert(index, value); } public void Remove(object value) { ((IList)_values).Remove(value); } public void RemoveAt(int index) { ((IList)_values).RemoveAt(index); } public void CopyTo(Array array, int index) { ((ICollection)_values).CopyTo(array, index); } #endregion } }