From f034b3223e17ab40d995a456f6209b8dec56c98e Mon Sep 17 00:00:00 2001 From: pongo1231 Date: Mon, 13 Nov 2023 01:11:59 +0000 Subject: [PATCH] ConfigApp: Add workshop sorting option Allows for sorting by Name, Last Updated and Author --- ConfigApp/Tabs/WorkshopTab.cs | 72 +++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/ConfigApp/Tabs/WorkshopTab.cs b/ConfigApp/Tabs/WorkshopTab.cs index 60f5a89c5..84a370ce3 100644 --- a/ConfigApp/Tabs/WorkshopTab.cs +++ b/ConfigApp/Tabs/WorkshopTab.cs @@ -1,14 +1,10 @@ using Microsoft.CSharp.RuntimeBinder; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; -using System.Linq; using System.Net.Http; using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; @@ -23,11 +19,48 @@ namespace ConfigApp.Tabs { public class WorkshopTab : Tab { + enum SortingMode + { + Name, + LastUpdated, + Author + } + private Dictionary m_SortingModeLabels = new Dictionary() + { + { SortingMode.Name, "Name" }, + { SortingMode.LastUpdated, "Last Updated" }, + { SortingMode.Author, "Author" } + }; + private SortingMode m_SortingMode = SortingMode.Name; + private ObservableCollection m_WorkshopSubmissionItems = new ObservableCollection(); private WatermarkTextBox m_SearchBox; private ItemsControl m_ItemsControl; + private void SortSubmissionItems() + { + IOrderedEnumerable? items = null; + + switch (m_SortingMode) + { + case SortingMode.Name: + items = m_WorkshopSubmissionItems.OrderBy(item => item.Name.ToLower()); + break; + case SortingMode.LastUpdated: + items = m_WorkshopSubmissionItems.OrderByDescending(item => item.LastUpdated); + break; + case SortingMode.Author: + items = m_WorkshopSubmissionItems.OrderBy(item => item.Author.ToLower()); + break; + default: + throw new NotImplementedException(); + } + + m_WorkshopSubmissionItems = new ObservableCollection(items.OrderBy(item => item.InstallState)); + m_ItemsControl.ItemsSource = m_WorkshopSubmissionItems; + } + private void HandleWorkshopSubmissionsSearchFilter() { var transformedText = m_SearchBox.Text.Trim().ToLower(); @@ -177,9 +210,7 @@ T getDataItem(dynamic item, T defaultValue) } } - m_WorkshopSubmissionItems = new ObservableCollection(m_WorkshopSubmissionItems.OrderBy(item => item.InstallState).ThenBy(item => item.Name)); - - m_ItemsControl.ItemsSource = m_WorkshopSubmissionItems; + SortSubmissionItems(); HandleWorkshopSubmissionsSearchFilter(); } @@ -198,7 +229,7 @@ private async Task ForceRefreshWorkshopContentFromRemote() } else { - var compressedResult = await result.Content?.ReadAsByteArrayAsync(); + var compressedResult = await result.Content.ReadAsByteArrayAsync(); ParseWorkshopSubmissionsFile(compressedResult); @@ -244,6 +275,14 @@ private void OnTextChangeSearch(object sender, TextChangedEventArgs eventArgs) HandleWorkshopSubmissionsSearchFilter(); } + private void OnSortingModeBoxSelectionChanged(object sender, SelectionChangedEventArgs eventArgs) + { + var box = (ComboBox)sender; + + m_SortingMode = (SortingMode)box.SelectedIndex; + SortSubmissionItems(); + } + protected override void InitContent() { PushNewColumn(new GridLength(1f, GridUnitType.Star)); @@ -252,6 +291,23 @@ protected override void InitContent() var headerGrid = new Grid(); + var sortingModeText = new TextBlock() + { + HorizontalAlignment = HorizontalAlignment.Left, + Margin = new Thickness(2f, 3f, 0f, 0f), + Text = "Sort By:" + }; + headerGrid.Children.Add(sortingModeText); + var sortingModeBox = new ComboBox() + { + HorizontalAlignment = HorizontalAlignment.Left, + Margin = new Thickness(55f, 0f, 0f, 0f), + ItemsSource = m_SortingModeLabels.Values, + SelectedIndex = 0 + }; + sortingModeBox.SelectionChanged += OnSortingModeBoxSelectionChanged; + headerGrid.Children.Add(sortingModeBox); + m_SearchBox = new WatermarkTextBox() { HorizontalAlignment = HorizontalAlignment.Right,