/// <summary>
        /// Get the selected simple and Genre tags and determine if there has been any changes
        /// </summary>
        private void OnOk(MultiSpinner genreSpinner, Spinner tagSpinner)
        {
            // Get the selected record from the Genre spinner. If not all of the items are selected then add an entry for each selected item to a new TagGroup
            List <TagGroup> selectedGroups = new List <TagGroup>();

            if (genreSpinner.SelectionRecord.All(genre => genre) == false)
            {
                TagGroup group = new TagGroup()
                {
                    Name = FilterManagementModel.GenreTags.Name
                };
                selectedGroups.Add(group);

                // Merge the Spinner's selection record and the Genre tags into a single list and then add to the new group any tags that are selected
                IEnumerable <Tuple <bool, Tag> > merged = genreSpinner.SelectionRecord.Zip(FilterManagementModel.GenreTags.Tags, (x, y) => Tuple.Create(x, y));
                group.Tags.AddRange(merged.Where(t => (t.Item1 == true)).Select(t => t.Item2));
            }

            // Get the simple tag
            Tag newTag = (tagSpinner.SelectedItemPosition == 0) ? null : Tags.GetTagByName(tagSpinner.SelectedItem.ToString());

            // Check for simple or group tag changes
            if ((newTag != CurrentlySelectedFilter) || (selectedGroups.Count != CurrentlySelectedTagGroups.Count) ||
                (selectedGroups.Any(group => GroupChanged(group)) == true))
            {
                // Update the FilterManagementModel TagGroups with the possibly updated data from the Adapter
                CurrentlySelectedTagGroups.Clear();
                CurrentlySelectedTagGroups.AddRange(selectedGroups);
                SelectionDelegate?.Invoke(newTag);
            }
        }
예제 #2
0
        /// <summary>
        /// Form a new Albums collection where albums with multiple genres are given multiple entries. The Albums will be in genre order
        /// </summary>
        private static void GenerateGenreAlbumList()
        {
            // This is the album list that we'll be generating
            AlbumsViewModel.GenreSortedAlbums = new List <Album>();

            // This genre list is generated alongside the main Album list
            AlbumsViewModel.AlbumIndexToGenreLookup = new List <string>();

            // We need a lookup table for all the Albums in the current filtered Album list
            Dictionary <int, Album> albumIds = AlbumsViewModel.FilteredAlbums.ToDictionary(alb => alb.Id);

            // The Albums need to be sorted in genre order. If there is no genre filter then use all the genre tags, otherwise just use
            // the tags in the filter
            TagGroup genreTags = AlbumsViewModel.FilterSelector.TagGroups.SingleOrDefault(ta => ta.Name == "Genre") ?? FilterManagementModel.GenreTags;

            // Get the Genre GroupTag and order the Tags by name. Copy the list so that we don't change it
            List <Tag> sortedTags = genreTags.Tags.ToList();

            sortedTags.Sort((a, b) => a.Name.CompareTo(b.Name));

            // Use the Genre GroupTag to order the Album entries
            foreach (Tag genreTag in sortedTags)
            {
                // For each TaggedAlbum in this tag that refers to an Album in the Album list add a new entry to the new Genre album list
                foreach (TaggedAlbum taggedAlbum in genreTag.TaggedAlbums)
                {
                    Album genreAlbum = albumIds.GetValueOrDefault(taggedAlbum.AlbumId);
                    if (genreAlbum != null)
                    {
                        AlbumsViewModel.AlbumIndexToGenreLookup.Add(genreTag.Name);
                        AlbumsViewModel.GenreSortedAlbums.Add(genreAlbum);
                    }
                }
            }
        }
        /// <summary>
        /// Determine whether or not the group represents a changed selection
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        private bool GroupChanged(TagGroup selectedGroup)
        {
            bool selectionChanged = false;

            // Get the matching group in the current selection
            TagGroup existingGroup = CurrentlySelectedTagGroups.SingleOrDefault(tg => tg.Name == selectedGroup.Name);

            // If there is no existing group, or the group size has changed, or there are entries in the new group that are not in the old one
            if ((existingGroup == null) || (selectedGroup.Tags.Count != existingGroup.Tags.Count) ||
                (existingGroup.Tags.Except(selectedGroup.Tags).ToList().Count > 0))
            {
                selectionChanged = true;
            }

            return(selectionChanged);
        }
        /// <summary>
        /// Initialise the contents of the Genre spinner
        /// </summary>
        /// <param name="genreSpinner"></param>
        private void InitialiseGenreSpinner(MultiSpinner genreSpinner)
        {
            // Get the currently selected Genre items from the CurrentlySelectedTagGroups.
            // If there is no Genre TagGroup then all the Genre items are selected.
            // Otherwise only the items in the TagGroup are selected
            bool[] selected = Enumerable.Repeat(true, FilterManagementModel.GenreTags.Tags.Count).ToArray();

            // Is there a Genre TagGroup
            TagGroup genreGroup = CurrentlySelectedTagGroups.FirstOrDefault(group => (group.Name == FilterManagementModel.GenreTags.Name));

            if (genreGroup != null)
            {
                // Set the selected flag for each tag according to whether or not it is in the TagGroup
                for (int genreIndex = 0; genreIndex < FilterManagementModel.GenreTags.Tags.Count; ++genreIndex)
                {
                    selected[genreIndex] = genreGroup.Tags.Exists(tag => tag.Name == FilterManagementModel.GenreTags.Tags[genreIndex].Name);
                }
            }

            // Display the names of all the genre tags in a multi-select spinner
            genreSpinner.SetItems(FilterManagementModel.GenreTags.Tags.Select(ta => ta.Name).ToList(), selected, "All");
        }