Skip to content

Commit

Permalink
Add generic options to dependencyItems.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Jun 19, 2020
1 parent f45bd97 commit 653ad88
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions uSync8.Core/Dependency/uSyncDependency.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Management;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

using Umbraco.Core;
Expand All @@ -24,23 +28,54 @@ public class uSyncDependency
/// </summary>
public string Name { get; set; }

/// <summary>
/// UDI reference for the object
/// </summary>
public Udi Udi { get; set; }

/// <summary>
/// Order value, used to sort the imports, lower order items are imported first.
/// </summary>
public int Order { get; set; }

/// <summary>
/// level in a tree - determains order within a section (lower level items imported first)
/// </summary>
public int Level { get; set; }

/// <summary>
/// what mode is the dependency running in (MustExist or MustMatch)
/// </summary>
public DependencyMode Mode { get; set; }

/// <summary>
/// Flags that control how an item is imported and sub dependencies calculated
/// </summary>
public DependencyFlags Flags { get; set; }


/// <summary>
/// generic options that can be filled based on the type of item we have.
/// </summary>
public IDictionary<string, object> Options { get; set; }

/// <summary>
/// Delegate event that you can listen to , to get messages about dependency updates.
/// </summary>
public static event uSyncDependencyUpdate DependencyUpdate;

/// <summary>
/// fires an update message to anything listening to the DependencyUpdate event
/// </summary>
/// <param name="message"></param>
public static void FireUpdate(string message)
{
FireUpdate(message, 1, 2);
}


/// <summary>
/// fires an update with progress to anything listing to the DependencyUpdate event
/// </summary>
public static void FireUpdate(string message, int count, int total)
{
DependencyUpdate?.Invoke(new DependencyMessageArgs
Expand All @@ -50,6 +85,30 @@ public static void FireUpdate(string message, int count, int total)
Total = total
});
}

/// <summary>
/// Get a generic option out of the DependencyItem
/// </summary>
public T GetOption<T>(string key, T defaultValue)
{
if (this.Options != null && this.Options.ContainsKey(key))
{
var attempt = Options[key].TryConvertTo<T>();
if (attempt.Success)
return attempt.Result;
}

return defaultValue;
}

/// <summary>
/// Add an option to the options for this item.
/// </summary>
public void SetOption<T>(string key, T value)
{
if (this.Options == null) this.Options = new Dictionary<string, object>();
Options[key] = value;
}
}

public enum DependencyMode
Expand Down

0 comments on commit 653ad88

Please sign in to comment.