| | 1 | | using System.ComponentModel; |
| | 2 | | using System.Runtime.CompilerServices; |
| | 3 | |
|
| | 4 | | namespace Common.Core.Classes; |
| | 5 | |
|
| | 6 | | /// <summary>Base class for models that require the INotifyPropertyChanged interface.</summary> |
| | 7 | | public abstract class ModelBase : INotifyPropertyChanged |
| | 8 | | { |
| | 9 | | #region INotifyPropertyChanged Implementation |
| | 10 | |
|
| | 11 | | /// <summary>Method that will handle the PropertyChanged event raised when a property is |
| | 12 | | /// changed on a component.</summary> |
| | 13 | | public event PropertyChangedEventHandler? PropertyChanged; |
| | 14 | |
|
| | 15 | | /// <summary>Invoked whenever the effective value has been updated.</summary> |
| | 16 | | /// <param name="property">Name of the property that has changed.</param> |
| | 17 | | public void OnPropertyChanged( [CallerMemberName] string property = "" ) |
| 1273 | 18 | | { |
| 1273 | 19 | | PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( property ) ); |
| 1273 | 20 | | } |
| | 21 | |
|
| | 22 | | #endregion |
| | 23 | |
|
| | 24 | | #region Public Methods |
| | 25 | |
|
| | 26 | | /// <summary>Calculate the current age based on a DateTime value.</summary> |
| | 27 | | /// <param name="date">Date to use.</param> |
| | 28 | | /// <returns><see langword="null"/> is returned if no date is provided.</returns> |
| | 29 | | public static int? CalculateAge( DateTime? date ) |
| 24 | 30 | | { |
| | 31 | | // https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-based-on-a-datetime-type-birthday |
| 24 | 32 | | return date is not null ? (int)( ( DateTime.Now - date.Value ).TotalDays / 365.242199 ) : null; |
| 24 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary>Calculate the current age based on a DateOnly value.</summary> |
| | 36 | | /// <param name="date">Date to use.</param> |
| | 37 | | /// <returns><see langword="null"/> is returned if no date is provided.</returns> |
| | 38 | | public static int? CalculateAge( DateOnly? date ) |
| 24 | 39 | | { |
| 24 | 40 | | return date.HasValue ? CalculateAge( date.Value.ToDateTime( TimeOnly.MinValue ) ) : null; |
| 24 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary>Sets a string as null if the length is zero.</summary> |
| | 44 | | /// <param name="value">Value to check.</param> |
| | 45 | | /// <returns><see langword="null"/> is returned if the string length is zero.</returns> |
| | 46 | | public static string? SetNullString( string? value ) |
| 644 | 47 | | { |
| 644 | 48 | | return value is not null && value.Length > 0 ? value : null; |
| 644 | 49 | | } |
| | 50 | |
|
| | 51 | | #endregion |
| | 52 | | } |