Skip to content

Commit

Permalink
AbstractContentManager - Add IsShowing, Groups, GetAllShowingOfType
Browse files Browse the repository at this point in the history
 - Allow callers to check if a specific view controller is showing in a group and, optionally, receive the instance via out parameter
 - Allow callers to get all showing of a specific view type
 - Allow callers to iterate groups
  • Loading branch information
mbaker3 committed Jul 13, 2024
1 parent c5207b4 commit 45c2243
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion Content/AbstractContentManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Anvil.CSharp.Core;

namespace Anvil.CSharp.Content
Expand Down Expand Up @@ -42,6 +43,14 @@ public abstract class AbstractContentManager : AbstractAnvilBase

private readonly Dictionary<string, AbstractContentGroup> m_ContentGroups = new Dictionary<string, AbstractContentGroup>();

/// <summary>
/// A collection of all <see cref="AbstractContentGroup"/>s that are currently managed by this content manager.
/// </summary>
public IReadOnlyCollection<AbstractContentGroup> Groups
{
get => m_ContentGroups.Values;
}

protected AbstractContentManager() { }

protected override void DisposeSelf()
Expand Down Expand Up @@ -138,6 +147,60 @@ public bool HasGroup(string contentGroupID)
return m_ContentGroups.ContainsKey(contentGroupID);
}

/// <summary>
/// Get all instances of the provided content controller type that are currently showing in a group.
/// </summary>
/// <param name="includeSubtypes">
/// (default true) If true will include instances that derive from the provided type.
/// </param>
/// <typeparam name="T">The type of content to gather.</typeparam>
/// <returns>A collection of instances that match the given type.</returns>
public IEnumerable<T> GetAllShowingOfType<T>(bool includeSubtypes = true)
where T : AbstractContentController
{
Type type = typeof(T);

return Groups
.Where(group => includeSubtypes
? type.IsInstanceOfType(group.ActiveContentController)
: type == group.ActiveContentController?.GetType())
.Select(group => group.ActiveContentController as T);
}

/// <summary>
/// Returns true if at least one instance of the content controller type is currently showing in any group.
/// </summary>
/// <param name="contentController">
/// The first content controller instance found that matches the provided type.
/// </param>
/// <param name="includeSubtypes">
/// (default true) If true will include instances that derive from the provided type.
/// </param>
/// <typeparam name="T">The type of content to search for.</typeparam>
/// <returns>true if at least one instance of the provided type is currently showing.</returns>
public bool IsShowing<T>(out T contentController, bool includeSubtypes = true)
where T : AbstractContentController
{
contentController = GetAllShowingOfType<T>(includeSubtypes).FirstOrDefault();
return contentController != null;
}

/// <inheritdoc cref="IsShowing{T}(out T,bool)"/>
public bool IsShowing<T>(bool includeSubtypes = true)
where T : AbstractContentController
{
return GetAllShowingOfType<T>(includeSubtypes).Any();
}

/// <summary>
/// Returns true if the provided instance of the content controller is currently showing in any group.
/// </summary>
/// <param name="targetContentController">The content controller instance to search for.</param>
/// <returns>True if the provided instance is showing in any group.</returns>
public bool IsShowing(AbstractContentController targetContentController)
{
return Groups.Any(group => group.ActiveContentController == targetContentController);
}

/// <summary>
/// Shows an instance of <see cref="AbstractContentController"/>.
Expand Down Expand Up @@ -216,4 +279,4 @@ private void ContentGroup_OnPlayOutComplete(AbstractContentController contentCon
OnPlayOutComplete?.Invoke(contentController);
}
}
}
}

0 comments on commit 45c2243

Please sign in to comment.