Skip to content

Commit

Permalink
Sort by date added
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Nov 18, 2024
1 parent bb55a47 commit 000286c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/J.App/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public sealed partial class MainForm : Form
_optionsButton,
_shuffleButton,
_sortAscendingButton,
_sortByDateAddedButton,
_sortByNameButton,
_sortDescendingButton;
private readonly ToolStripButton _browseBackButton,
Expand Down Expand Up @@ -192,6 +193,12 @@ Preferences preferences
_sortByNameButton.Checked = true;
_sortByNameButton.Click += SortByNameButton_Click;
}

_sortButton.DropDownItems.Add(_sortByDateAddedButton = ui.NewToolStripMenuItem("By date added"));
{
_sortByDateAddedButton.Checked = true;
_sortByDateAddedButton.Click += SortByDateAddedButton_Click;
}
}

_toolStrip.Items.Add(_menuButton = ui.NewToolStripDropDownButton("Menu"));
Expand Down Expand Up @@ -651,6 +658,11 @@ private async void SortByNameButton_Click(object? sender, EventArgs e)
await ChangeSortOrderAsync(x => x with { Field = "name" }).ConfigureAwait(true);
}

private async void SortByDateAddedButton_Click(object? sender, EventArgs e)
{
await ChangeSortOrderAsync(x => x with { Field = "date" }).ConfigureAwait(true);
}

private async void SortItem_Click(object? sender, EventArgs e)
{
var menuItem = (ToolStripMenuItem)sender!;
Expand Down Expand Up @@ -678,6 +690,7 @@ private async Task UpdateFilterSortFromPreferencesAsync(bool reload = true)
_sortDescendingButton.Checked = !sortOrder.Ascending;

_sortByNameButton.Checked = sortOrder.Field == "name";
_sortByDateAddedButton.Checked = sortOrder.Field == "date";
foreach (var menuItem in _sortButton.DropDownItems.OfType<ToolStripMenuItem>())
{
if (menuItem.Tag is TagTypeId tagTypeId)
Expand Down Expand Up @@ -827,10 +840,10 @@ private void UpdateTagTypes()
while (_filterButton.DropDownItems.Count > 0 && _filterButton.DropDownItems[0] is not ToolStripSeparator)
_filterButton.DropDownItems.RemoveAt(0);

// Remove sort menu items after the "sort by name" item.
var sortByNameIndex = _sortButton.DropDownItems.IndexOf(_sortByNameButton);
while (_sortButton.DropDownItems.Count > sortByNameIndex + 1)
_sortButton.DropDownItems.RemoveAt(sortByNameIndex + 1);
// Remove sort menu items after the "sort by date added" item.
var lastIndex = _sortButton.DropDownItems.IndexOf(_sortByDateAddedButton);
while (_sortButton.DropDownItems.Count > lastIndex + 1)
_sortButton.DropDownItems.RemoveAt(lastIndex + 1);

var tagTypes = _libraryProvider.GetTagTypes().ToDictionary(x => x.Id);

Expand All @@ -857,7 +870,7 @@ private void UpdateTagTypes()
var sortItem = _ui.NewToolStripMenuItem($"By {tagType.SingularName.ToLower()}");
sortItem.Tag = tagType.Id;
sortItem.Click += SortItem_Click;
_sortButton.DropDownItems.Insert(sortByNameIndex + 1, sortItem);
_sortButton.DropDownItems.Insert(lastIndex + 1, sortItem);
}

// Add a filter field for the filename, which is special.
Expand Down
1 change: 1 addition & 0 deletions src/J.Server/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public readonly record struct Block(
MovieId MovieId, // movie to get the clip from
TagId? TagId, // if targeting a tag instead of a movie on click, this is the tag
string Title,
DateTimeOffset Date,
Dictionary<TagTypeId, string> SortTags
);

Expand Down
9 changes: 7 additions & 2 deletions src/J.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ LibraryMetadata libraryMetadata
if (!dict.TryGetValue(tag.Id, out var movieId))
continue;

Page.Block block = new(movieId, tag.Id, tag.Name, []);
Page.Block block = new(movieId, tag.Id, tag.Name, DateTimeOffset.Now, []);
blocks.Add(block);
}
return NewPageFromBlocks(blocks, sortOrder, tagType.PluralName);
Expand All @@ -180,6 +180,8 @@ static string GetField(Page.Block block, string field)
{
if (field == "name")
return block.Title;
if (field == "date")
return block.Date.UtcDateTime.ToString("u");

TagTypeId tagTypeId = new(field);
if (block.SortTags.TryGetValue(tagTypeId, out var value))
Expand Down Expand Up @@ -245,7 +247,10 @@ string title
)
{
return NewPageFromBlocks(
(from x in movies select new Page.Block(x.Id, null, x.Filename, GetSortTags(x, libraryMetadata))).ToList(),
(
from x in movies
select new Page.Block(x.Id, null, x.Filename, x.DateAdded, GetSortTags(x, libraryMetadata))
).ToList(),
sortOrder,
title
);
Expand Down

0 comments on commit 000286c

Please sign in to comment.