DFLayoutEmbeddedMediaValues.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Collections.Specialized;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using InABox.Core;
  10. namespace InABox.Core
  11. {
  12. public class DFLayoutEmbeddedMediaValues : IEnumerable<DFLayoutEmbeddedMediaValue>, INotifyPropertyChanged, IDFLayoutValue<List<DFLayoutEmbeddedMediaValue>>, INotifyCollectionChanged, IList
  13. {
  14. private readonly ObservableCollection<DFLayoutEmbeddedMediaValue> _values = new ObservableCollection<DFLayoutEmbeddedMediaValue>();
  15. public DFLayoutEmbeddedMediaValue[] Values => _values.ToArray();
  16. public bool Present { get; set; } = false;
  17. public List<byte[]> AsByteArrays
  18. {
  19. get => _values.Select(x => x?.Data).OfType<byte[]>().ToList();
  20. set
  21. {
  22. _values.Clear();
  23. CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  24. foreach (var bytes in value)
  25. {
  26. var v = new DFLayoutEmbeddedMediaValue() { ID = Guid.Empty, Data = bytes };
  27. _values.Add(v);
  28. CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, v));
  29. }
  30. OnPropertyChanged(nameof(Values));
  31. }
  32. }
  33. public DFLayoutEmbeddedMediaValues()
  34. {
  35. _values.CollectionChanged += _values_CollectionChanged;
  36. }
  37. private void _values_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  38. {
  39. CollectionChanged?.Invoke(this, e);
  40. }
  41. public void Serialize(DFSaveStorageEntry storage)
  42. {
  43. var results = new List<string>();
  44. foreach (var value in _values)
  45. results.Add(Serialization.Serialize((value.ID, value.Thumbnail)));
  46. if (results.Any())
  47. storage.SetBlobValue(results);
  48. }
  49. public void Deserialize(DFLoadStorageEntry storage)
  50. {
  51. _values.Clear();
  52. Present = storage.HasValue();
  53. if (Present)
  54. {
  55. var values = storage.GetValue<string[]>() ?? Array.Empty<string>();
  56. foreach (string s in values)
  57. {
  58. if (!s.IsNullOrWhiteSpace())
  59. {
  60. var externaldata = Serialization.Deserialize<(Guid, byte[])>(s);
  61. if (externaldata.Item1 != Guid.Empty)
  62. {
  63. _values.Add(new DFLayoutEmbeddedMediaValue()
  64. {
  65. ID = externaldata.Item1,
  66. Thumbnail = externaldata.Item2
  67. });
  68. }
  69. }
  70. }
  71. }
  72. }
  73. public IEnumerator<DFLayoutEmbeddedMediaValue> GetEnumerator() => _values.GetEnumerator();
  74. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  75. public void Add(DFLayoutEmbeddedMediaValue value)
  76. {
  77. _values.Add(value);
  78. OnPropertyChanged(nameof(Values));
  79. }
  80. public void Remove(DFLayoutEmbeddedMediaValue value)
  81. {
  82. if (_values.Contains(value))
  83. {
  84. _values.Remove(value);
  85. OnPropertyChanged(nameof(Values));
  86. }
  87. }
  88. public void Clear()
  89. {
  90. _values.Clear();
  91. OnPropertyChanged(nameof(Values));
  92. }
  93. public event PropertyChangedEventHandler? PropertyChanged;
  94. public event NotifyCollectionChangedEventHandler? CollectionChanged;
  95. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
  96. {
  97. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  98. }
  99. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
  100. {
  101. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  102. field = value;
  103. OnPropertyChanged(propertyName);
  104. return true;
  105. }
  106. #region IList
  107. public int Count => ((ICollection<DFLayoutEmbeddedMediaValue>)_values).Count;
  108. public bool IsReadOnly => ((ICollection<DFLayoutEmbeddedMediaValue>)_values).IsReadOnly;
  109. public bool IsFixedSize => ((IList)_values).IsFixedSize;
  110. public bool IsSynchronized => ((ICollection)_values).IsSynchronized;
  111. public object SyncRoot => ((ICollection)_values).SyncRoot;
  112. object IList.this[int index] { get => ((IList)_values)[index]; set => ((IList)_values)[index] = value; }
  113. public int Add(object value)
  114. {
  115. return ((IList)_values).Add(value);
  116. }
  117. public bool Contains(object value)
  118. {
  119. return ((IList)_values).Contains(value);
  120. }
  121. public int IndexOf(object value)
  122. {
  123. return ((IList)_values).IndexOf(value);
  124. }
  125. public void Insert(int index, object value)
  126. {
  127. ((IList)_values).Insert(index, value);
  128. }
  129. public void Remove(object value)
  130. {
  131. ((IList)_values).Remove(value);
  132. }
  133. public void RemoveAt(int index)
  134. {
  135. ((IList)_values).RemoveAt(index);
  136. }
  137. public void CopyTo(Array array, int index)
  138. {
  139. ((ICollection)_values).CopyTo(array, index);
  140. }
  141. #endregion
  142. }
  143. }