Skip to content

Commit

Permalink
Refactor IconManager
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Apr 1, 2024
1 parent cda7102 commit 87fb439
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 81 deletions.
52 changes: 17 additions & 35 deletions Guard/Core/Icons/IconManager.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
namespace Guard.Core.Icons
using System.IO;
using Guard.Core.Installation;

namespace Guard.Core.Icons
{
class IconManager
{
public enum IconColor
{
Colored,
Dark,
Light
}

internal enum IconType
{
Default,
Any,
SimpleIcons,
Custom
}

internal class TotpIcon
{
public required IconType Type { get; set; }
public required string Svg { get; set; }
public required string Name { get; set; }
public string? Svg { get; set; }
public string? Path { get; set; }
}

private static readonly string customIconsPath = Path.Combine(
InstallationInfo.GetAppDataFolderPath(),
"icons"
);

public static void LoadIcons()
{
Task.Run(() => SimpleIconsManager.LoadIcons());
Expand All @@ -44,41 +47,20 @@ public static string[] GetIconNames()
Name = "default",
};

public static TotpIcon GetIcon(string name, IconColor color, IconType type)
public static TotpIcon GetIcon(string name, IconType type)
{
if (name == null || name == "default" || type == IconType.Default)
{
return defaultIcon;
}
SimpleIcon? simpleIcon = SimpleIconsManager.GetIcon(name);

if (simpleIcon == null)
{
return defaultIcon;
}

string hexColor;

if (color == IconColor.Colored && simpleIcon.H != null)
{
hexColor = $"#{simpleIcon.H}";
}
else if (color == IconColor.Dark)
if (type == IconType.SimpleIcons || type == IconType.Any)
{
hexColor = "#313131";
}
else
{
hexColor = "#ffffff";
TotpIcon? simpleIcon = SimpleIconsManager.GetTotpIcon(name);
return simpleIcon ?? defaultIcon;
}

return new TotpIcon
{
Type = IconType.SimpleIcons,
Svg =
$"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" fill=\"{hexColor}\"><path d=\"{simpleIcon.P}\"/></svg>",
Name = name,
};
return defaultIcon;
}

public static string GetLicense(TotpIcon icon)
Expand Down
40 changes: 39 additions & 1 deletion Guard/Core/Icons/SimpleIconsManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Reflection;
using System.Text.Json;
using Wpf.Ui.Appearance;
using static Guard.Core.Icons.IconManager;

namespace Guard.Core.Icons
{
Expand Down Expand Up @@ -65,11 +67,47 @@ public static string[] GetIconNames() =>
icons?.Select(icon => icon.T).ToArray()
?? throw new InvalidOperationException("Icons not loaded");

public static SimpleIcon? GetIcon(string name)
public static SimpleIcon? GetSimpleIcon(string name)
{
return icons?.FirstOrDefault(icon => icon.T == name);
}

public static TotpIcon? GetTotpIcon(string name)
{
SimpleIcon? simpleIcon = GetSimpleIcon(name);
if (simpleIcon == null)
{
return null;
}

string hexColor = simpleIcon.H;
if (hexColor == null)
{
bool isDarkMode = ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Dark;
if (isDarkMode)
{
hexColor = "#ffffff";
}
else
{
hexColor = "#313131";
}
}

if (!hexColor.StartsWith('#'))
{
hexColor = $"#{hexColor}";
}

return new TotpIcon
{
Type = IconType.SimpleIcons,
Svg =
$"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" fill=\"{hexColor}\"><path d=\"{simpleIcon.P}\"/></svg>",
Name = name,
};
}

public static string? GetLicense()
{
return license;
Expand Down
1 change: 0 additions & 1 deletion Guard/Core/Import/Importer/AuthenticatorProImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ private enum BackupType

IconManager.TotpIcon icon = IconManager.GetIcon(
token.Issuer,
IconManager.IconColor.Colored,
IconManager.IconType.Any
);

Expand Down
9 changes: 4 additions & 5 deletions Guard/Core/Import/Importer/BitwardenImporter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Guard.Core.Icons;
using Guard.Core.Models;
using Guard.Core.Security;
using System.IO;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using Guard.Core.Icons;
using Guard.Core.Models;
using Guard.Core.Security;

namespace Guard.Core.Import.Importer
{
Expand Down Expand Up @@ -129,7 +129,6 @@ private static BitwardenEncryptionType GetEncryptionType(BitwardenExportFile exp

IconManager.TotpIcon icon = IconManager.GetIcon(
item.Name,
IconManager.IconColor.Colored,
IconManager.IconType.Any
);

Expand Down
11 changes: 5 additions & 6 deletions Guard/Core/Import/Importer/TwoFasImporter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Guard.Core.Icons;
using Guard.Core.Models;
using Guard.Core.Security;
using NSec.Cryptography;
using System.IO;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using Guard.Core.Icons;
using Guard.Core.Models;
using Guard.Core.Security;
using NSec.Cryptography;

namespace Guard.Core.Import.Importer
{
Expand Down Expand Up @@ -82,7 +82,6 @@ internal class TwoFasImporter : IImporter

IconManager.TotpIcon icon = IconManager.GetIcon(
service.OTP.Issuer,
IconManager.IconColor.Colored,
IconManager.IconType.Any
);

Expand Down
10 changes: 3 additions & 7 deletions Guard/Core/Import/OTPUriParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ internal static DBTOTPToken ConvertToDBToken(OTPUri otpUri)

IconManager.TotpIcon icon = IconManager.GetIcon(
otpUri.Issuer,
IconManager.IconColor.Colored,
IconManager.IconType.Any
);

Expand Down Expand Up @@ -161,11 +160,9 @@ internal static DBTOTPToken ConvertToDBToken(OTPUri otpUri)

internal static string NormalizeSecret(string secret)
{

return new string(secret.ToCharArray()
.Where(c => !Char.IsWhiteSpace(c))
.ToArray()).ToUpper();

return new string(
secret.ToCharArray().Where(c => !Char.IsWhiteSpace(c)).ToArray()
).ToUpper();
}

internal static bool IsValidSecret(string secret)
Expand All @@ -180,6 +177,5 @@ internal static bool IsValidSecret(string secret)
return false;
}
}

}
}
9 changes: 6 additions & 3 deletions Guard/Views/Controls/TokenCard.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<svgc:SvgViewbox
x:Name="SvgIconView"
<StackPanel
Grid.Column="0"
Width="45"
Margin="22,0,0,0" />
Margin="22,0,0,0"
VerticalAlignment="Center">
<svgc:SvgViewbox x:Name="SvgIconView" />
<ui:Image x:Name="ImageIconView" />
</StackPanel>

<StackPanel
Grid.Column="1"
Expand Down
9 changes: 2 additions & 7 deletions Guard/Views/Controls/TokenCard.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,14 @@ internal TokenCard(TOTPTokenHelper token)
{
icon = IconManager.GetIcon(
token.dBToken.Icon,
IconManager.IconColor.Colored,
(IconManager.IconType)token.dBToken.IconType
token.dBToken.IconType ?? IconManager.IconType.Any
);

SvgIconView.SvgSource = icon.Svg;
}
else
{
icon = IconManager.GetIcon(
"default",
IconManager.IconColor.Colored,
IconManager.IconType.Default
);
icon = IconManager.GetIcon("default", IconManager.IconType.Default);
SvgIconView.SvgSource = icon.Svg;
}

Expand Down
23 changes: 7 additions & 16 deletions Guard/Views/Pages/Add/TokenSettings.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Guard.Core;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Guard.Core;
using Guard.Core.Icons;
using Guard.Core.Import;
using Guard.Core.Models;
using Guard.Core.Security;
using Guard.Views.UIComponents;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Wpf.Ui.Controls;

namespace Guard.Views.Pages.Add
Expand Down Expand Up @@ -45,11 +45,7 @@ public TokenSettings()

NavigationContextManager.ClearContext();

defaultIcon = IconManager.GetIcon(
"default",
IconManager.IconColor.Colored,
IconManager.IconType.Default
);
defaultIcon = IconManager.GetIcon("default", IconManager.IconType.Default);
IconSvgView.SvgSource = defaultIcon.Svg;
Issuer.OriginalItemsSource = IconManager.GetIconNames();

Expand All @@ -75,7 +71,6 @@ public TokenSettings()
{
selectedIcon = IconManager.GetIcon(
existingToken.dBToken.Icon,
IconManager.IconColor.Colored,
existingToken.dBToken.IconType ?? IconManager.IconType.Any
);
IconSvgView.SvgSource = selectedIcon.Svg;
Expand Down Expand Up @@ -156,11 +151,7 @@ AutoSuggestBoxSuggestionChosenEventArgs args
if (args.SelectedItem is not string selectedSuggestBoxItem)
return;

selectedIcon = IconManager.GetIcon(
selectedSuggestBoxItem,
IconManager.IconColor.Colored,
IconManager.IconType.Any
);
selectedIcon = IconManager.GetIcon(selectedSuggestBoxItem, IconManager.IconType.Any);
IconSvgView.SvgSource = selectedIcon.Svg;
ImageLicense.Text = IconManager.GetLicense(selectedIcon);
NoIconText.Visibility = Visibility.Collapsed;
Expand Down

0 comments on commit 87fb439

Please sign in to comment.