Skip to content

Commit

Permalink
Merge pull request #92 from AathifMahir/Fix
Browse files Browse the repository at this point in the history
New and Improved ImageSource Extension
  • Loading branch information
AathifMahir committed Apr 1, 2024
2 parents b7e0c97 + 62c5ee3 commit 9a701ce
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 17 deletions.
1 change: 1 addition & 0 deletions samples/MauiIcons.Sample/MarkupPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private void AddMarkup()
new MauiIcon().Icon(FontAwesomeBrandIcons.Pinterest).IconColor(Colors.Red),
new Button().Icon(FontAwesomeBrandIcons.Github).IconSize(30),
new Entry().Icon(FontAwesomeIcons.Building).IconSize(20.0),
new Image{ Source = MaterialIcons.Favorite.ToImageSource(iconColor: Colors.Blue, iconSize: 50) },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
<PropertyGroup Condition="'$(MSBuildProjectName)' != 'MauiIcons.Sample' and $(MSBuildProjectName) != 'MauiIcons.Modules.UnitTest'">
<Title>MauiIcons</Title>
<PackageIcon>icon.png</PackageIcon>
<AssemblyVersion>2.1.2.0</AssemblyVersion>
<AssemblyFileVersion>2.1.2.0</AssemblyFileVersion>
<Version>2.1.2</Version>
<AssemblyVersion>2.1.5.0</AssemblyVersion>
<AssemblyFileVersion>2.1.5.0</AssemblyFileVersion>
<Version>2.1.5</Version>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<PackageVersion>$(Version)$(VersionSuffix)</PackageVersion>
<UseFullSemVerForNuGet>false</UseFullSemVerForNuGet>
Expand Down
30 changes: 30 additions & 0 deletions src/MauiIcons.Core/Extensions/CommonExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using MauiIcons.Core.Helpers;

namespace MauiIcons.Core;
public static class CommonExtension
{
/// <summary>
/// this is used for seamlessly transforming the MauiIcons Enum Constructs to an ImageSource
/// </summary>
/// <param name="iconColor">sets the color of the icon. Defaults to black or white based on App Theme</param>
/// <param name="iconSize">sets the size of the icon. Defaults to 30.0</param>
/// <param name="iconAutoScaling">sets a value indicating whether the icon should automatically scale. Defaults to false</param>
/// <returns>Icon as ImageSource</returns>
/// <exception cref="MauiIconsExpection">
/// Thrown when the Enum is not a MauiIcons Construct
/// </exception>
public static ImageSource ToImageSource(this Enum icon, Color? iconColor = null, double iconSize = 30.0, bool iconAutoScaling = false)
{
if (icon.GetDescription() is null)
throw new MauiIconsExpection("MauiIcons ToImageSourceExtension Only Works on MauiIcons Constructs Not on All the Enum Types");

return new FontImageSource()
{
Glyph = icon.GetDescription(),
Color = iconColor.SetDefaultOrAssignedColor(),
FontFamily = icon.GetFontFamily(),
Size = iconSize,
FontAutoScalingEnabled = iconAutoScaling
};
}
}
4 changes: 2 additions & 2 deletions src/MauiIcons.Core/Helpers/ThemeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace MauiIcons.Core.Helpers;
internal static class ThemeHelper
{
internal static Color SetDefaultOrAssignedColor(this Color value, Color originalColor)
internal static Color SetDefaultOrAssignedColor(this Color? value, Color originalColor)
{
if (value is null)
{
Expand All @@ -10,7 +10,7 @@ internal static Color SetDefaultOrAssignedColor(this Color value, Color original
return value;
}

internal static Color SetDefaultOrAssignedColor(this Color value)
internal static Color SetDefaultOrAssignedColor(this Color? value)
{
if (value is null)
{
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Core/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Cupertino/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Fluent.Filled/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Fluent/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.FontAwesome.Brand/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.FontAwesome.Solid/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.FontAwesome/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Material.Outlined/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Material.Rounded/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Material.Sharp/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
5 changes: 4 additions & 1 deletion src/MauiIcons.Material/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down
154 changes: 154 additions & 0 deletions src/MauiIcons.Modules.UnitTest/Extensions/CommonExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using MauiIcons.Fluent.Filled;
using MauiIcons.Cupertino;
using MauiIcons.Core;

namespace MauiIcons.Modules.UnitTest.Extensions;
public class CommonExtensionTests : BaseHandlerTest
{
[Fact]
public void Icon()
{
// Arrange
Image image;
var icon = FluentFilledIcons.Accessibility16Filled.ToImageSource();
var iconCode = "\uF102";
FontImageSource source;

// Act
image = new Image { Source = icon };
source = (FontImageSource)image.Source;

// Assert
image.Source.Should().NotBeNull();
source.Glyph.Should().Be(iconCode);
source.FontFamily.Should().Be("FluentFilledIcons");
}

[Fact]
public void IconChanged()
{
// Arrange
Image image;
bool changedSignaled = false;
var assignedIcon = CupertinoIcons.Airplane.ToImageSource();
var assignedIconCode = "\ue900";
FontImageSource source;

// Act
image = new Image();
image.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "Source")
{
changedSignaled = true;
}
};
image.Source = assignedIcon;
source = (FontImageSource)image.Source;


// Assert
image.Source.Should().NotBeNull();
source.Glyph.Should().Be(assignedIconCode);
source.FontFamily.Should().Be("CupertinoIcons");
changedSignaled.Should().BeTrue();
}

[Fact]
public void IconColorChanged()
{
// Arrange
Image image;
bool changedSignaled = false;
var assignedColor = Colors.Blue;
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(iconColor: assignedColor);
FontImageSource source;

// Act
image = new Image();
image.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "Source")
{
changedSignaled = true;
}
};
image.Source = assignedIcon;
source = (FontImageSource)image.Source;

// Assert
image.Source.Should().NotBeNull();
source.Color.Should().Be(assignedColor);
changedSignaled.Should().BeTrue();
}

[Fact]
public void IconColorChangedToDefault()
{
// Arrange
Image image;
var assignedIcon = CupertinoIcons.Airplane.ToImageSource();
var assignedColoredIcon = CupertinoIcons.Airplane.ToImageSource(iconColor: Colors.Green);
FontImageSource source;

// Act
image = new Image { Source = assignedColoredIcon };
image.Source = assignedIcon;
source = (FontImageSource)image.Source;


// Assert
image.Source.Should().NotBeNull();
source.Color.Should().Be(Colors.Black);
}


[Fact]
public void IconSizeChanged()
{
// Arrange
Image image;
bool changedSignaled = false;
var assignedSize = 25.0;
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(iconSize: assignedSize);
FontImageSource source;

// Act
image = new Image();
image.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "Source")
{
changedSignaled = true;
}
};
image.Source = assignedIcon;
source = (FontImageSource)image.Source;


// Assert
image.Source.Should().NotBeNull();
source.Size.Should().Be(assignedSize);
changedSignaled.Should().BeTrue();
}

[Fact]
public void IconSizeChangedToDefault()
{
// Arrange
Image image;
var defaultSize = 30.0;
var defaultIcon = CupertinoIcons.Airplane.ToImageSource();
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(iconSize: 40);
FontImageSource source;

// Act
image = new Image { Source = assignedIcon };
image.Source = defaultIcon;
source = (FontImageSource)image.Source;

// Assert
image.Source.Should().NotBeNull();
source.Size.Should().Be(defaultSize);
}
}
5 changes: 4 additions & 1 deletion src/MauiIcons.SegoeFluent/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v2.1.2
v2.1.5
• New and Improved Extension for MauiIcon C#

v2.1.2
• Minor Fixes and Improvements for Base Extension

v2.1.1
Expand Down

0 comments on commit 9a701ce

Please sign in to comment.