using System; using System.ComponentModel; using System.IO; using Artemis.Storage.Entities.Profile; namespace Artemis.Core; /// /// Represents the icon of a /// public class ProfileConfigurationIcon : CorePropertyChanged, IStorageModel { private readonly ProfileContainerEntity _entity; private bool _fill; private string? _iconName; private byte[]? _iconBytes; private ProfileConfigurationIconType _iconType; internal ProfileConfigurationIcon(ProfileContainerEntity entity) { _entity = entity; } /// /// Gets the type of icon this profile configuration uses /// public ProfileConfigurationIconType IconType { get => _iconType; private set => SetAndNotify(ref _iconType, value); } /// /// Gets the name of the icon if is /// public string? IconName { get => _iconName; private set => SetAndNotify(ref _iconName, value); } /// /// Gets or sets a boolean indicating whether or not this icon should be filled. /// public bool Fill { get => _fill; set => SetAndNotify(ref _fill, value); } /// /// Gets or sets the icon bytes if is /// public byte[]? IconBytes { get => _iconBytes; private set => SetAndNotify(ref _iconBytes, value); } /// /// Updates the to the provided value and changes the is /// /// /// The name of the icon public void SetIconByName(string iconName) { ArgumentNullException.ThrowIfNull(iconName); IconBytes = null; IconName = iconName; IconType = ProfileConfigurationIconType.MaterialIcon; OnIconUpdated(); } /// /// Updates the to the provided value and changes the is /// /// The stream to copy public void SetIconByStream(Stream stream) { ArgumentNullException.ThrowIfNull(stream); if (stream.CanSeek) stream.Seek(0, SeekOrigin.Begin); using (MemoryStream ms = new()) { stream.CopyTo(ms); IconBytes = ms.ToArray(); } IconName = null; IconType = ProfileConfigurationIconType.BitmapImage; OnIconUpdated(); } /// /// Occurs when the icon was updated /// public event EventHandler? IconUpdated; /// /// Invokes the event /// protected virtual void OnIconUpdated() { IconUpdated?.Invoke(this, EventArgs.Empty); } #region Implementation of IStorageModel /// public void Load() { IconType = (ProfileConfigurationIconType) _entity.ProfileConfiguration.IconType; Fill = _entity.ProfileConfiguration.IconFill; if (IconType == ProfileConfigurationIconType.MaterialIcon) IconName = _entity.ProfileConfiguration.MaterialIcon; else IconBytes = _entity.Icon; OnIconUpdated(); } /// public void Save() { _entity.ProfileConfiguration.IconType = (int) IconType; _entity.ProfileConfiguration.MaterialIcon = IconType == ProfileConfigurationIconType.MaterialIcon ? IconName : null; _entity.ProfileConfiguration.IconFill = Fill; _entity.Icon = IconBytes ?? Array.Empty(); } #endregion } /// /// Represents a type of profile icon /// public enum ProfileConfigurationIconType { /// /// An icon picked from the Material Design Icons collection /// [Description("Material Design Icon")] MaterialIcon, /// /// A bitmap image icon /// [Description("Bitmap Image")] BitmapImage }