Skip to content

Commit

Permalink
Merge pull request #27 from amywarble/set_singleton
Browse files Browse the repository at this point in the history
Allow overriding the provider's static instance
  • Loading branch information
SKProCH authored Dec 9, 2023
2 parents 19d5a86 + 08a9495 commit 3c41c56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 13 additions & 3 deletions Material.Icons/MaterialIconDataProvider.Declaration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Material.Icons;

/// ******************************************
Expand All @@ -7,16 +9,24 @@ namespace Material.Icons;
/// Allows retrieving data for icons
/// </summary>
public partial class MaterialIconDataProvider {
private static MaterialIconDataProvider? _instance;
private static MaterialIconDataProvider _instance = new();

/// <summary>
/// Gets the singleton instance of this provider
/// Gets or sets the singleton instance of this provider
/// </summary>
public static MaterialIconDataProvider Instance => _instance ??= new MaterialIconDataProvider();
public static MaterialIconDataProvider Instance {
get => _instance;
set {
_instance = value ?? throw new ArgumentNullException(nameof(value));
}
}

/// <summary>
/// Gets the data for the specified icon using the <see cref="Instance"/>
/// </summary>
/// <param name="kind">The icon kind</param>
/// <returns>SVG path for target icon kind</returns>

public static string GetData(MaterialIconKind kind) => Instance.ProvideData(kind);
/// <summary>
/// Provides the data for the specified icon kind
Expand Down
18 changes: 14 additions & 4 deletions build/Generators/MaterialIconDataDeclarationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ public class MaterialIconDataDeclarationGenerator {
public static void Write(AbsolutePath destinationPath) {
var path = destinationPath / "MaterialIconDataProvider.Declaration.cs";
Log.Information("Writing declaration for material icons data to {Path}", path);

var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("using System;");
stringBuilder.AppendLine("");
stringBuilder.AppendLine("namespace Material.Icons;");
stringBuilder.AppendLine("");
stringBuilder.AppendLine("/// ******************************************");
Expand All @@ -20,16 +22,24 @@ public static void Write(AbsolutePath destinationPath) {
stringBuilder.AppendLine("/// Allows retrieving data for icons");
stringBuilder.AppendLine("/// </summary>");
stringBuilder.AppendLine("public partial class MaterialIconDataProvider {");
stringBuilder.AppendLine(" private static MaterialIconDataProvider? _instance;");
stringBuilder.AppendLine(" private static MaterialIconDataProvider _instance = new();");
stringBuilder.AppendLine("");
stringBuilder.AppendLine(" /// <summary>");
stringBuilder.AppendLine(" /// Gets the singleton instance of this provider");
stringBuilder.AppendLine(" /// Gets or sets the singleton instance of this provider");
stringBuilder.AppendLine(" /// </summary>");
stringBuilder.AppendLine(" public static MaterialIconDataProvider Instance => _instance ??= new MaterialIconDataProvider();");
stringBuilder.AppendLine(" public static MaterialIconDataProvider Instance {");
stringBuilder.AppendLine(" get => _instance;");
stringBuilder.AppendLine(" set {");
stringBuilder.AppendLine(" _instance = value ?? throw new ArgumentNullException(nameof(value));");
stringBuilder.AppendLine(" }");
stringBuilder.AppendLine(" }");
stringBuilder.AppendLine("");
stringBuilder.AppendLine(" /// <summary>");
stringBuilder.AppendLine(" /// Gets the data for the specified icon using the <see cref=\"Instance\"/>");
stringBuilder.AppendLine(" /// </summary>");
stringBuilder.AppendLine(" /// <param name=\"kind\">The icon kind</param>");
stringBuilder.AppendLine(" /// <returns>SVG path for target icon kind</returns>");
stringBuilder.AppendLine("");
stringBuilder.AppendLine(" public static string GetData(MaterialIconKind kind) => Instance.ProvideData(kind);");
stringBuilder.AppendLine(" /// <summary>");
stringBuilder.AppendLine(" /// Provides the data for the specified icon kind");
Expand Down

0 comments on commit 3c41c56

Please sign in to comment.