Skip to content

Commit

Permalink
ConfigApp: Add workshop sorting option
Browse files Browse the repository at this point in the history
Allows for sorting by Name, Last Updated and Author
  • Loading branch information
pongo1231 committed Nov 13, 2023
1 parent 7c252aa commit f034b32
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions ConfigApp/Tabs/WorkshopTab.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,11 +19,48 @@ namespace ConfigApp.Tabs
{
public class WorkshopTab : Tab
{
enum SortingMode
{
Name,
LastUpdated,
Author
}
private Dictionary<SortingMode, string> m_SortingModeLabels = new Dictionary<SortingMode, string>()
{
{ SortingMode.Name, "Name" },
{ SortingMode.LastUpdated, "Last Updated" },
{ SortingMode.Author, "Author" }
};
private SortingMode m_SortingMode = SortingMode.Name;

private ObservableCollection<WorkshopSubmissionItem> m_WorkshopSubmissionItems = new ObservableCollection<WorkshopSubmissionItem>();

private WatermarkTextBox m_SearchBox;
private ItemsControl m_ItemsControl;

private void SortSubmissionItems()
{
IOrderedEnumerable<WorkshopSubmissionItem>? 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<WorkshopSubmissionItem>(items.OrderBy(item => item.InstallState));
m_ItemsControl.ItemsSource = m_WorkshopSubmissionItems;
}

private void HandleWorkshopSubmissionsSearchFilter()
{
var transformedText = m_SearchBox.Text.Trim().ToLower();
Expand Down Expand Up @@ -177,9 +210,7 @@ T getDataItem<T>(dynamic item, T defaultValue)
}
}

m_WorkshopSubmissionItems = new ObservableCollection<WorkshopSubmissionItem>(m_WorkshopSubmissionItems.OrderBy(item => item.InstallState).ThenBy(item => item.Name));

m_ItemsControl.ItemsSource = m_WorkshopSubmissionItems;
SortSubmissionItems();

HandleWorkshopSubmissionsSearchFilter();
}
Expand All @@ -198,7 +229,7 @@ private async Task ForceRefreshWorkshopContentFromRemote()
}
else
{
var compressedResult = await result.Content?.ReadAsByteArrayAsync();
var compressedResult = await result.Content.ReadAsByteArrayAsync();

ParseWorkshopSubmissionsFile(compressedResult);

Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand Down

0 comments on commit f034b32

Please sign in to comment.