Core - Rewrote profile configuration icons

WPF UI - Adjusted for the above rewrite 
Avalonia UI - Implemented profile icons
This commit is contained in:
Robert committed 2021-12-04 12:02:17 +01:00
1 parent 4c5c785aa6
commit a6f1b05c19
31 files changed
+659 -105

No files matched your search

@@ -27,7 +27,8 @@ namespace Artemis.Core
_category = category;
Entity = new ProfileConfigurationEntity();
Icon = new ProfileConfigurationIcon(Entity) {MaterialIcon = icon};
Icon = new ProfileConfigurationIcon(Entity);
Icon.SetIconByName(icon);
ActivationCondition = new NodeScript<bool>("Activate profile", "Whether or not the profile should be active", this);
}
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using Artemis.Core.JsonConverters;
using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json;
@@ -8,7 +9,7 @@ namespace Artemis.Core
/// <summary>
/// A model that can be used to serialize a profile configuration, it's profile and it's icon
/// </summary>
public class ProfileConfigurationExportModel
public class ProfileConfigurationExportModel : IDisposable
{
/// <summary>
/// Gets or sets the storage entity of the profile configuration
@@ -26,5 +27,11 @@ namespace Artemis.Core
/// </summary>
[JsonConverter(typeof(StreamConverter))]
public Stream? ProfileImage { get; set; }
/// <inheritdoc />
public void Dispose()
{
ProfileImage?.Dispose();
}
}
}
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.IO;
using Artemis.Storage.Entities.Profile;
@@ -10,9 +11,10 @@ namespace Artemis.Core
public class ProfileConfigurationIcon : CorePropertyChanged, IStorageModel
{
private readonly ProfileConfigurationEntity _entity;
private Stream? _fileIcon;
private string? _iconName;
private Stream? _iconStream;
private ProfileConfigurationIconType _iconType;
private string? _materialIcon;
private string? _originalFileName;
internal ProfileConfigurationIcon(ProfileConfigurationEntity entity)
{
@@ -20,31 +22,82 @@ namespace Artemis.Core
}
/// <summary>
/// Gets or sets the type of icon this profile configuration uses
/// Gets the type of icon this profile configuration uses
/// </summary>
public ProfileConfigurationIconType IconType
{
get => _iconType;
set => SetAndNotify(ref _iconType, value);
private set => SetAndNotify(ref _iconType, value);
}
/// <summary>
/// Gets or sets the icon if it is a Material icon
/// Gets the name of the icon if <see cref="IconType" /> is <see cref="ProfileConfigurationIconType.MaterialIcon" />
/// </summary>
public string? MaterialIcon
public string? IconName
{
get => _materialIcon;
set => SetAndNotify(ref _materialIcon, value);
get => _iconName;
private set => SetAndNotify(ref _iconName, value);
}
/// <summary>
/// Gets or sets a stream containing the icon if it is bitmap or SVG
/// Gets the original file name of the icon (if applicable)
/// </summary>
/// <returns></returns>
public Stream? FileIcon
public string? OriginalFileName
{
get => _fileIcon;
set => SetAndNotify(ref _fileIcon, value);
get => _originalFileName;
private set => SetAndNotify(ref _originalFileName, value);
}
/// <summary>
/// Updates the <see cref="IconName" /> to the provided value and changes the <see cref="IconType" /> is
/// <see cref="ProfileConfigurationIconType.MaterialIcon" />
/// </summary>
/// <param name="iconName">The name of the icon</param>
public void SetIconByName(string iconName)
{
IconName = iconName ?? throw new ArgumentNullException(nameof(iconName));
OriginalFileName = null;
IconType = ProfileConfigurationIconType.MaterialIcon;
_iconStream?.Dispose();
}
/// <summary>
/// Updates the stream returned by <see cref="GetIconStream" /> to the provided stream
/// </summary>
/// <param name="originalFileName">The original file name backing the stream, should include the extension</param>
/// <param name="stream">The stream to copy</param>
public void SetIconByStream(string originalFileName, Stream stream)
{
if (originalFileName == null) throw new ArgumentNullException(nameof(originalFileName));
if (stream == null) throw new ArgumentNullException(nameof(stream));
_iconStream?.Dispose();
_iconStream = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(_iconStream);
_iconStream.Seek(0, SeekOrigin.Begin);
IconName = null;
OriginalFileName = originalFileName;
IconType = OriginalFileName.EndsWith(".svg") ? ProfileConfigurationIconType.SvgImage : ProfileConfigurationIconType.BitmapImage;
}
/// <summary>
/// Creates a copy of the stream containing the icon
/// </summary>
/// <returns>A stream containing the icon</returns>
public Stream? GetIconStream()
{
if (_iconStream == null)
return null;
MemoryStream stream = new();
_iconStream.CopyTo(stream);
stream.Seek(0, SeekOrigin.Begin);
_iconStream.Seek(0, SeekOrigin.Begin);
return stream;
}
#region Implementation of IStorageModel
@@ -53,14 +106,15 @@ namespace Artemis.Core
public void Load()
{
IconType = (ProfileConfigurationIconType) _entity.IconType;
MaterialIcon = _entity.MaterialIcon;
if (IconType == ProfileConfigurationIconType.MaterialIcon)
IconName = _entity.MaterialIcon;
}
/// <inheritdoc />
public void Save()
{
_entity.IconType = (int) IconType;
_entity.MaterialIcon = MaterialIcon;
_entity.MaterialIcon = IconType == ProfileConfigurationIconType.MaterialIcon ? IconName : null;
}
#endregion
@@ -304,10 +304,13 @@ namespace Artemis.Core.Services
{
if (profileConfiguration.Icon.IconType == ProfileConfigurationIconType.MaterialIcon)
return;
if (profileConfiguration.Icon.FileIcon != null)
return;
// This can happen if the icon was saved before the original file name was stored (pre-Avalonia)
profileConfiguration.Entity.IconOriginalFileName ??= profileConfiguration.Icon.IconType == ProfileConfigurationIconType.BitmapImage ? "icon.png" : "icon.svg";
profileConfiguration.Icon.FileIcon = _profileCategoryRepository.GetProfileIconStream(profileConfiguration.Entity.FileIconId);
using Stream? stream = _profileCategoryRepository.GetProfileIconStream(profileConfiguration.Entity.FileIconId);
if (stream != null)
profileConfiguration.Icon.SetIconByStream(profileConfiguration.Entity.IconOriginalFileName, stream);
}
public void SaveProfileConfigurationIcon(ProfileConfiguration profileConfiguration)
@@ -315,10 +318,11 @@ namespace Artemis.Core.Services
if (profileConfiguration.Icon.IconType == ProfileConfigurationIconType.MaterialIcon)
return;
if (profileConfiguration.Icon.FileIcon != null)
using Stream? stream = profileConfiguration.Icon.GetIconStream();
if (stream != null && profileConfiguration.Icon.OriginalFileName != null)
{
profileConfiguration.Icon.FileIcon.Position = 0;
_profileCategoryRepository.SaveProfileIconStream(profileConfiguration.Entity, profileConfiguration.Icon.FileIcon);
profileConfiguration.Entity.IconOriginalFileName = profileConfiguration.Icon.OriginalFileName;
_profileCategoryRepository.SaveProfileIconStream(profileConfiguration.Entity, stream);
}
}
@@ -532,7 +536,7 @@ namespace Artemis.Core.Services
{
ProfileConfigurationEntity = profileConfiguration.Entity,
ProfileEntity = profile.ProfileEntity,
ProfileImage = profileConfiguration.Icon.FileIcon
ProfileImage = profileConfiguration.Icon.GetIconStream()
};
}
@@ -579,12 +583,8 @@ namespace Artemis.Core.Services
profileConfiguration = new ProfileConfiguration(category, profileEntity.Name, "Import");
}
if (exportModel.ProfileImage != null)
{
profileConfiguration.Icon.FileIcon = new MemoryStream();
exportModel.ProfileImage.Position = 0;
exportModel.ProfileImage.CopyTo(profileConfiguration.Icon.FileIcon);
}
if (exportModel.ProfileImage != null && exportModel.ProfileConfigurationEntity?.IconOriginalFileName != null)
profileConfiguration.Icon.SetIconByStream(exportModel.ProfileConfigurationEntity.IconOriginalFileName, exportModel.ProfileImage);
profileConfiguration.Entity.ProfileId = profileEntity.Id;
category.AddProfileConfiguration(profileConfiguration, 0);