| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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<DFLayoutEmbeddedMediaValue>, INotifyPropertyChanged, IDFLayoutValue<List<DFLayoutEmbeddedMediaValue>>, INotifyCollectionChanged, IList
- {
- private readonly ObservableCollection<DFLayoutEmbeddedMediaValue> _values = new ObservableCollection<DFLayoutEmbeddedMediaValue>();
- public DFLayoutEmbeddedMediaValue[] Values => _values.ToArray();
- public bool Present { get; set; } = false;
- public List<byte[]> AsByteArrays
- {
- get => _values.Select(x => x?.Data).OfType<byte[]>().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<string>();
- 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<string[]>() ?? Array.Empty<string>();
- 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<DFLayoutEmbeddedMediaValue> 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<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
- {
- if (EqualityComparer<T>.Default.Equals(field, value)) return false;
- field = value;
- OnPropertyChanged(propertyName);
- return true;
- }
- #region IList
- public int Count => ((ICollection<DFLayoutEmbeddedMediaValue>)_values).Count;
- public bool IsReadOnly => ((ICollection<DFLayoutEmbeddedMediaValue>)_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
- }
- }
|