using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; namespace PepperDash.Essentials.Devices.Common.Codec { /// /// Represents a codec directory /// public class CodecDirectory { /// /// Represents the contents of the directory /// We don't want to serialize this for messages to MobileControl. MC can combine Contacts and Folders to get the same data /// [JsonIgnore] public List CurrentDirectoryResults { get; private set; } /// /// Gets the Contacts in the CurrentDirectoryResults /// [JsonProperty("contacts")] public List Contacts { get { return CurrentDirectoryResults.OfType().Cast().ToList(); } } /// /// Gets the Folders in the CurrentDirectoryResults /// [JsonProperty("folders")] public List Folders { get { return CurrentDirectoryResults.OfType().Cast().ToList(); } } /// /// Used to store the ID of the current folder for CurrentDirectoryResults /// Gets or sets the ResultsFolderId /// [JsonProperty("resultsFolderId")] public string ResultsFolderId { get; set; } /// /// Constructor for /// public CodecDirectory() { CurrentDirectoryResults = new List(); } /// /// Adds folders to the directory /// /// public void AddFoldersToDirectory(List folders) { if (folders != null) CurrentDirectoryResults.AddRange(folders); SortDirectory(); } /// /// Adds contacts to the directory /// /// public void AddContactsToDirectory(List contacts) { if (contacts != null) CurrentDirectoryResults.AddRange(contacts); SortDirectory(); } /// /// Filters the CurrentDirectoryResults by the predicate /// /// public void FilterContacts(Func predicate) { CurrentDirectoryResults = CurrentDirectoryResults.Where(predicate).ToList(); } /// /// Sorts the DirectoryResults list to display all folders alphabetically, then all contacts alphabetically /// private void SortDirectory() { var sortedFolders = new List(); sortedFolders.AddRange(CurrentDirectoryResults.Where(f => f is DirectoryFolder)); sortedFolders.OrderBy(f => f.Name); var sortedContacts = new List(); sortedContacts.AddRange(CurrentDirectoryResults.Where(c => c is DirectoryContact)); sortedFolders.OrderBy(c => c.Name); CurrentDirectoryResults.Clear(); CurrentDirectoryResults.AddRange(sortedFolders); CurrentDirectoryResults.AddRange(sortedContacts); } } }