-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small fixes for descriptions, added extension and utils for handling the attributes
- Loading branch information
1 parent
c6f3530
commit 9c26775
Showing
8 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace UsageWatcher | ||
{ | ||
/// <summary> | ||
/// Taken from: https://stackoverflow.com/a/19621488 | ||
/// </summary> | ||
public static class EnumExtensions | ||
{ | ||
/// <summary> | ||
/// Extensible base method, can be used to get the attributes of an Enum | ||
/// </summary> | ||
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This is an extension method")] | ||
public static T GetAttribute<T>(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; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the Description attribute of the given Enum | ||
/// </summary> | ||
[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<DescriptionAttribute>(); | ||
return attribute == null ? value.ToString() : attribute.Description; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the DisplayAttribute.DisplayAttribute Name property | ||
/// </summary> | ||
[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<DisplayAttribute>(); | ||
return attribute == null ? value.ToString() : attribute.Name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace UsageWatcher | ||
{ | ||
[Serializable] | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace UsageWatcher.Enums.Utils | ||
{ | ||
public static class EnumUtil | ||
{ | ||
public static IEnumerable<T> GetValues<T>() where T : Enum | ||
{ | ||
return Enum.GetValues(typeof(T)).Cast<T>(); | ||
} | ||
|
||
public static EnumMatchResult<T> GetEnumForString<T>(string enumDisplayNameOrDescription) where T : Enum | ||
{ | ||
IEnumerable<T> enumElements = GetValues<T>(); | ||
|
||
EnumMatchResult<T> result = null; | ||
foreach (T enumElem in enumElements) | ||
{ | ||
if (enumElem.GetDisplayName() == enumDisplayNameOrDescription || | ||
enumElem.GetDescription() == enumDisplayNameOrDescription) | ||
{ | ||
result = new EnumMatchResult<T>(enumElem); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
public class EnumMatchResult<T> where T : Enum | ||
{ | ||
public T FoundEnum { get; private set; } | ||
|
||
public EnumMatchResult(T foundEnum) | ||
{ | ||
FoundEnum = foundEnum; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters