Skip to content

Commit

Permalink
Update observable properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Apr 13, 2024
1 parent bffd15b commit 0ea5664
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 31 deletions.
86 changes: 74 additions & 12 deletions Core/Data/Result/ConciseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ConciseModel(string id, string title, string link = null, string descript
/// Gets or sets the identifier.
/// </summary>
[JsonPropertyName("id")]
[Description("The identifier of the model.")]
[Description("The identifier of the item.")]
public string Id
{
get => GetCurrentProperty<string>();
Expand All @@ -70,7 +70,7 @@ public string Id
/// Gets or sets the title.
/// </summary>
[JsonPropertyName("title")]
[Description("The title of the model.")]
[Description("The title of the item.")]
public string Title
{
get => GetCurrentProperty<string>();
Expand All @@ -82,7 +82,7 @@ public string Title
/// </summary>
[JsonPropertyName("desc")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Description("The short description of the model.")]
[Description("The short description of the item.")]
public string Description
{
get => GetCurrentProperty<string>();
Expand All @@ -102,9 +102,9 @@ public Uri ImageUri
/// <summary>
/// Gets or sets the image URI (thumbnail or avatar).
/// </summary>
[JsonPropertyName("id")]
[JsonPropertyName("image")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Description("The thumbnail or avatar URL of the model.")]
[Description("The thumbnail or avatar URL of the item.")]
public string ImageUrl
{
get => ImageUri?.OriginalString;
Expand All @@ -116,7 +116,7 @@ public string ImageUrl
/// </summary>
[JsonPropertyName("url")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Description("The URL to details page of the model.")]
[Description("The URL to details page of the item.")]
public string Link
{
get => GetCurrentProperty<string>();
Expand All @@ -128,7 +128,8 @@ public string Link
/// </summary>
[JsonPropertyName("keywords")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Description("The keywords of the model.")]
[Description("The keywords of the item.")]
[JsonConverter(typeof(JsonStringListConverter.SemicolonSeparatedConverter))]
public ObservableCollection<string> Keywords
{
get => GetCurrentProperty<ObservableCollection<string>>();
Expand Down Expand Up @@ -216,7 +217,7 @@ public override string ToString()
}

/// <summary>
/// The model with observable name and value.
/// The model with observable key and value.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
[DataContract]
Expand All @@ -232,16 +233,16 @@ public KeyValueObservableModel()
/// <summary>
/// Initializes a new instance of the KeyValueObservableModel class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public KeyValueObservableModel(string name, T value)
public KeyValueObservableModel(string key, T value)
{
Key = name;
Key = key;
Value = value;
}

/// <summary>
/// Gets or sets the name.
/// Gets or sets the key.
/// </summary>
[DataMember(Name = "key")]
[JsonPropertyName("key")]
Expand Down Expand Up @@ -275,3 +276,64 @@ public object Tag
set => SetCurrentProperty(value);
}
}

/// <summary>
/// The model with observable name and value.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
[DataContract]
public class NameValueObservableModel<T> : BaseObservableProperties
{
/// <summary>
/// Initializes a new instance of the NameValueObservableModel class.
/// </summary>
public NameValueObservableModel()
{
}

/// <summary>
/// Initializes a new instance of the NameValueObservableModel class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
public NameValueObservableModel(string name, T value)
{
Name = name;
Value = value;
}

/// <summary>
/// Gets or sets the name.
/// </summary>
[DataMember(Name = "name")]
[JsonPropertyName("name")]
[Description("The item name.")]
public string Name

{
get => GetCurrentProperty<string>();
set => SetCurrentProperty(value);
}

/// <summary>
/// Gets or sets the value.
/// </summary>
[DataMember(Name = "value")]
[JsonPropertyName("value")]
[Description("The value.")]
public T Value
{
get => GetCurrentProperty<T>();
set => SetCurrentProperty(value);
}

/// <summary>
/// Gets or sets the optional additional object.
/// </summary>
[JsonIgnore]
public object Tag
{
get => GetCurrentProperty<object>();
set => SetCurrentProperty(value);
}
}
12 changes: 1 addition & 11 deletions Core/Maths/Statistics/Average.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Arithmetic\Basic.cs" company="Nanchang Jinchen Software Co., Ltd.">
// Copyright (c) 2010 Nanchang Jinchen Software Co., Ltd. All rights reserved.
// </copyright>
// <summary>
// The basic arithmetic functions.
// </summary>
// <author>Kingcean Tuan</author>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down
124 changes: 124 additions & 0 deletions Core/Maths/Statistics/Difference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Trivial.Maths;

/// <summary>
/// The functions of probability theory and mathematical statistics.
/// </summary>
public static partial class StatisticalMethod
{
/// <summary>
/// Gets a sequence about each item is different than before one of a specific numbers.
/// </summary>
/// <param name="col">The input numbers.</param>
/// <returns>The increasement of each item.</returns>
public static IEnumerable<double> Difference(IEnumerable<double> col)
{
if (col == null) yield break;
double? test = null;
foreach (var item in col)
{
if (test.HasValue) yield return item - test.Value;
test = item;
}
}

/// <summary>
/// Gets a sequence about each item is different than before one of a specific numbers.
/// </summary>
/// <param name="col">The input numbers.</param>
/// <returns>The increasement of each item.</returns>
public static IEnumerable<float> Difference(IEnumerable<float> col)
{
if (col == null) yield break;
float? test = null;
foreach (var item in col)
{
if (test.HasValue) yield return item - test.Value;
test = item;
}
}

/// <summary>
/// Gets a sequence about each item is different than before one of a specific numbers.
/// </summary>
/// <param name="col">The input numbers.</param>
/// <returns>The increasement of each item.</returns>
public static IEnumerable<decimal> Difference(IEnumerable<decimal> col)
{
if (col == null) yield break;
decimal? test = null;
foreach (var item in col)
{
if (test.HasValue) yield return item - test.Value;
test = item;
}
}

/// <summary>
/// Gets a sequence about each item is different than before one of a specific numbers.
/// </summary>
/// <param name="col">The input numbers.</param>
/// <returns>The increasement of each item.</returns>
public static IEnumerable<long> Difference(IEnumerable<long> col)
{
if (col == null) yield break;
long? test = null;
foreach (var item in col)
{
if (test.HasValue) yield return item - test.Value;
test = item;
}
}

/// <summary>
/// Gets a sequence about each item is different than before one of a specific numbers.
/// </summary>
/// <param name="col">The input numbers.</param>
/// <returns>The increasement of each item.</returns>
public static IEnumerable<int> Difference(IEnumerable<int> col)
{
if (col == null) yield break;
int? test = null;
foreach (var item in col)
{
if (test.HasValue) yield return item - test.Value;
test = item;
}
}

/// <summary>
/// Gets a sequence about each item is different than before one of a specific numbers.
/// </summary>
/// <param name="col">The input numbers.</param>
/// <returns>The increasement of each item.</returns>
public static IEnumerable<TimeSpan> Difference(IEnumerable<DateTime> col)
{
if (col == null) yield break;
DateTime? test = null;
foreach (var item in col)
{
if (test.HasValue) yield return item - test.Value;
test = item;
}
}

/// <summary>
/// Gets a sequence about each item is different than before one of a specific numbers.
/// </summary>
/// <param name="col">The input numbers.</param>
/// <returns>The increasement of each item.</returns>
public static IEnumerable<TimeSpan> Difference(IEnumerable<TimeSpan> col)
{
if (col == null) yield break;
TimeSpan? test = null;
foreach (var item in col)
{
if (test.HasValue) yield return item - test.Value;
test = item;
}
}
}
37 changes: 32 additions & 5 deletions Core/Reflection/Lifecycle/ObservableProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected T GetCurrentProperty<T>(T defaultValue = default, [CallerMemberName] s
if (string.IsNullOrWhiteSpace(key)) return defaultValue;
try
{
return cache.TryGetValue(key, out var v) ? (T)v : defaultValue;
return TryGetPropertyInternal(key, out var v) ? (T)v : defaultValue;
}
catch (InvalidCastException)
{
Expand Down Expand Up @@ -146,7 +146,7 @@ protected T GetProperty<T>(string key, T defaultValue = default)
if (string.IsNullOrWhiteSpace(key)) return defaultValue;
try
{
return cache.TryGetValue(key, out var v) && v is T t ? t : defaultValue;
return TryGetPropertyInternal(key, out var v) && v is T t ? t : defaultValue;
}
catch (InvalidOperationException)
{
Expand Down Expand Up @@ -175,7 +175,7 @@ protected bool GetProperty<T>(string key, out T result)
{
try
{
if (!string.IsNullOrWhiteSpace(key) && cache.TryGetValue(key, out var v) && v is T t)
if (!string.IsNullOrWhiteSpace(key) && TryGetPropertyInternal(key, out var v) && v is T t)
{
result = t;
return true;
Expand Down Expand Up @@ -226,14 +226,26 @@ protected bool SetProperty(string key, object value)
}

/// <summary>
/// Test whether the new property to set is valid.
/// Tests whether the new property to set is valid.
/// </summary>
/// <param name="key">The property key.</param>
/// <param name="value">The value of the property to set.</param>
/// <returns>true if the property is valid; otherwise, false.</returns>
protected virtual bool IsPropertyValid(string key, object value)
=> true;

/// <summary>
/// Occurs on request to get a specific value. This can be used to fill a default value.
/// </summary>
/// <param name="key">The property key.</param>
/// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
/// <returns>true if need to set a default value; otherwise, false.</returns>
protected virtual bool FillNonExistProperty(string key, out object value)
{
value = default;
return false;
}

/// <summary>
/// Removes a property.
/// </summary>
Expand Down Expand Up @@ -269,7 +281,7 @@ protected Type GetPropertyType(string key)
if (string.IsNullOrWhiteSpace(key)) return null;
try
{
return cache.TryGetValue(key, out var v) ? v?.GetType() : null;
return TryGetPropertyInternal(key, out var v) ? v?.GetType() : null;
}
catch (ArgumentException)
{
Expand Down Expand Up @@ -352,6 +364,21 @@ protected void CopyFrom(BaseObservableProperties props, bool skipIfExist = false
protected IEnumerator<KeyValuePair<string, object>> EnumerateObject()
=> cache.GetEnumerator();

/// <summary>
/// Tries to get property.
/// </summary>
/// <param name="key">The property key.</param>
/// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
/// <returns>true if the cache contains an element with the specified key; otherwise, false.</returns>
private bool TryGetPropertyInternal(string key, out object value)
{
if (cache.TryGetValue(key, out value)) return true;
if (!FillNonExistProperty(key, out value)) return false;
cache[key] = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(key));
return true;
}

/// <summary>
/// Sets the property.
/// </summary>
Expand Down
Loading

0 comments on commit 0ea5664

Please sign in to comment.