From 9c26775e6f781254cc6990c62e270508a9f87ad7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zsolt=20Gajd=C3=A1cs?=
<33376024+ZsoltGajdacs@users.noreply.github.com>
Date: Mon, 15 Feb 2021 12:12:12 +0100
Subject: [PATCH] DisplayNames for Enums
Small fixes for descriptions, added extension and utils for handling the attributes
---
Enums/DataPrecision.cs | 5 ++++-
Enums/EnumExtensions.cs | 46 ++++++++++++++++++++++++++++++++++++++
Enums/Resolution.cs | 10 +++++++++
Enums/SavePreference.cs | 6 ++++-
Enums/Utils/EnumUtil.cs | 41 +++++++++++++++++++++++++++++++++
Properties/AssemblyInfo.cs | 4 ++--
UsageWatcher.csproj | 6 +++++
packages.config | 1 +
8 files changed, 115 insertions(+), 4 deletions(-)
create mode 100644 Enums/EnumExtensions.cs
create mode 100644 Enums/Utils/EnumUtil.cs
diff --git a/Enums/DataPrecision.cs b/Enums/DataPrecision.cs
index 614bf1b..927fe7f 100644
--- a/Enums/DataPrecision.cs
+++ b/Enums/DataPrecision.cs
@@ -1,12 +1,15 @@
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
namespace UsageWatcher
{
public enum DataPrecision
{
- [Description("Data is kept for every usages, having a detailed record as per chosen resolution")]
+ [Description("Data is kept for every usage, having a detailed record as per chosen resolution")]
+ [Display(Name = "High Precision")]
HighPrecision,
[Description("Not Implemented yet")]
+ [Display(Name = "Low Precision")]
LowPrecision
}
}
diff --git a/Enums/EnumExtensions.cs b/Enums/EnumExtensions.cs
new file mode 100644
index 0000000..291dc20
--- /dev/null
+++ b/Enums/EnumExtensions.cs
@@ -0,0 +1,46 @@
+using System;
+using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
+using System.Diagnostics.CodeAnalysis;
+
+namespace UsageWatcher
+{
+ ///
+ /// Taken from: https://stackoverflow.com/a/19621488
+ ///
+ public static class EnumExtensions
+ {
+ ///
+ /// Extensible base method, can be used to get the attributes of an Enum
+ ///
+ [SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This is an extension method")]
+ public static T GetAttribute(this Enum value) where T : Attribute
+ {
+ Type type = value.GetType();
+ System.Reflection.MemberInfo[] memberInfo = type.GetMember(value.ToString());
+ object[] attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
+
+ return attributes.Length > 0 ? (T)attributes[0] : null;
+ }
+
+ ///
+ /// Retrieves the Description attribute of the given Enum
+ ///
+ [SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This is an extension method")]
+ public static string GetDescription(this Enum value)
+ {
+ DescriptionAttribute attribute = value.GetAttribute();
+ return attribute == null ? value.ToString() : attribute.Description;
+ }
+
+ ///
+ /// Retrieves the DisplayAttribute.DisplayAttribute Name property
+ ///
+ [SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This is an extension method")]
+ public static string GetDisplayName(this Enum value)
+ {
+ DisplayAttribute attribute = value.GetAttribute();
+ return attribute == null ? value.ToString() : attribute.Name;
+ }
+ }
+}
diff --git a/Enums/Resolution.cs b/Enums/Resolution.cs
index a24ecd0..ae9e699 100644
--- a/Enums/Resolution.cs
+++ b/Enums/Resolution.cs
@@ -1,18 +1,28 @@
using System;
+using System.ComponentModel.DataAnnotations;
namespace UsageWatcher
{
[Serializable]
public enum Resolution
{
+ [Display(Name = "1/2 min")]
HalfMinute = 30000,
+ [Display(Name = "1 min")]
OneMinute = 60000,
+ [Display(Name = "2 min")]
TwoMinutes = 120000,
+ [Display(Name = "3 min")]
ThreeMinutes = 180000,
+ [Display(Name = "4 min")]
FourMinutes = 240000,
+ [Display(Name = "5 min")]
FiveMinutes = 3000000,
+ [Display(Name = "10 min")]
TenMinutes = 600000,
+ [Display(Name = "15 min")]
FifteenMinutes = 900000,
+ [Display(Name = "30 min")]
ThirtyMinutes = 1800000
}
}
diff --git a/Enums/SavePreference.cs b/Enums/SavePreference.cs
index dbddb97..350f371 100644
--- a/Enums/SavePreference.cs
+++ b/Enums/SavePreference.cs
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
namespace UsageWatcher
{
@@ -7,10 +8,13 @@ namespace UsageWatcher
public enum SavePreference
{
[Description("No data will be saved, if the app containing the lib is shut down, the data is lost.")]
+ [Display(Name = "No data save")]
NoSave,
[Description("Data is saved for a single day.")]
+ [Display(Name = "Save Today")]
KeepDataForToday,
- [Description("Data is saved permanently, can be recalled any time.")]
+ [Description("Data is saved for every day, can be recalled any time.")]
+ [Display(Name = "Save All")]
KeepDataForever
}
}
diff --git a/Enums/Utils/EnumUtil.cs b/Enums/Utils/EnumUtil.cs
new file mode 100644
index 0000000..efbe2a3
--- /dev/null
+++ b/Enums/Utils/EnumUtil.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace UsageWatcher.Enums.Utils
+{
+ public static class EnumUtil
+ {
+ public static IEnumerable GetValues() where T : Enum
+ {
+ return Enum.GetValues(typeof(T)).Cast();
+ }
+
+ public static EnumMatchResult GetEnumForString(string enumDisplayNameOrDescription) where T : Enum
+ {
+ IEnumerable enumElements = GetValues();
+
+ EnumMatchResult result = null;
+ foreach (T enumElem in enumElements)
+ {
+ if (enumElem.GetDisplayName() == enumDisplayNameOrDescription ||
+ enumElem.GetDescription() == enumDisplayNameOrDescription)
+ {
+ result = new EnumMatchResult(enumElem);
+ }
+ }
+
+ return result;
+ }
+ }
+
+ public class EnumMatchResult where T : Enum
+ {
+ public T FoundEnum { get; private set; }
+
+ public EnumMatchResult(T foundEnum)
+ {
+ FoundEnum = foundEnum;
+ }
+ }
+}
diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs
index 783f241..e4899d9 100644
--- a/Properties/AssemblyInfo.cs
+++ b/Properties/AssemblyInfo.cs
@@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.1.0.0")]
-[assembly: AssemblyFileVersion("1.1.0.0")]
+[assembly: AssemblyVersion("1.2.0.0")]
+[assembly: AssemblyFileVersion("1.2.0.0")]
diff --git a/UsageWatcher.csproj b/UsageWatcher.csproj
index 744e528..8b4dbac 100644
--- a/UsageWatcher.csproj
+++ b/UsageWatcher.csproj
@@ -42,6 +42,10 @@
packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll
+
+ packages\System.ComponentModel.Annotations.5.0.0\lib\net461\System.ComponentModel.Annotations.dll
+
+
@@ -54,6 +58,8 @@
+
+
diff --git a/packages.config b/packages.config
index 64a5ef9..4eb446f 100644
--- a/packages.config
+++ b/packages.config
@@ -6,4 +6,5 @@
+
\ No newline at end of file