Skip to content

Commit

Permalink
Implement sorting in the subscriptions list
Browse files Browse the repository at this point in the history
Implement sorting the list of subscriptions in the same way as the
downloads list.  This resolves #125.
  • Loading branch information
jonegerton authored and ribbons committed Aug 15, 2014
1 parent dca9fc1 commit 11df3a2
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
3 changes: 3 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ The following people have contributed code to Radio Downloader:
### Matt Robinson
Primary Developer

### Jon Egerton
Sorting in subscriptions list

### Nick Sharples
Original proof of concept code for RSS server
73 changes: 71 additions & 2 deletions Classes/Model/Subscription.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of Radio Downloader.
* Copyright © 2007-2012 by the authors - see the AUTHORS file for details.
* Copyright © 2007-2014 by the authors - see the AUTHORS file for details.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
Expand All @@ -20,13 +20,17 @@ namespace RadioDld.Model
using System.Collections.Generic;
using System.Data.SQLite;
using System.Globalization;
using System.IO;
using System.Threading;

internal class Subscription : Programme
{
private static Dictionary<int, int> sortCache;
private static object sortCacheLock = new object();

private static SubscriptionCols sortBy = SubscriptionCols.ProgrammeName;
private static bool sortAsc;

static Subscription()
{
Programme.Updated += Programme_Updated;
Expand Down Expand Up @@ -56,6 +60,55 @@ public Subscription(int progid)

public static event ProgrammeEventHandler Removed;

internal enum SubscriptionCols
{
ProgrammeName = 0,
LastDownload = 1,
Provider = 2
}

public static SubscriptionCols SortByColumn
{
get
{
return sortBy;
}

set
{
lock (sortCacheLock)
{
if (value != sortBy)
{
sortCache = null;
}

sortBy = value;
}
}
}

public static bool SortAscending
{
get
{
return sortAsc;
}

set
{
lock (sortCacheLock)
{
if (value != sortAsc)
{
sortCache = null;
}

sortAsc = value;
}
}
}

public static List<Subscription> FetchAll()
{
List<Subscription> items = new List<Subscription>();
Expand Down Expand Up @@ -124,8 +177,24 @@ public static int Compare(int progid1, int progid2)
sortCache = new Dictionary<int, int>();

int sort = 0;
string orderBy = null;

switch (sortBy)
{
case SubscriptionCols.ProgrammeName:
orderBy = "name" + (sortAsc ? string.Empty : " desc");
break;
case SubscriptionCols.LastDownload:
orderBy = "latestdownload" + (sortAsc ? string.Empty : " desc");
break;
case SubscriptionCols.Provider:
orderBy = "pluginid" + (sortAsc ? " desc" : string.Empty);
break;
default:
throw new InvalidDataException("Invalid column: " + sortBy.ToString());
}

using (SQLiteCommand command = new SQLiteCommand("select subscriptions.progid from subscriptions, programmes where programmes.progid=subscriptions.progid order by name", FetchDbConn()))
using (SQLiteCommand command = new SQLiteCommand("select subscriptions.progid from subscriptions, programmes where programmes.progid=subscriptions.progid order by " + orderBy, FetchDbConn()))
{
using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader()))
{
Expand Down
26 changes: 26 additions & 0 deletions Classes/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ internal static bool ShownTrayBalloon
}
}

internal static Model.Subscription.SubscriptionCols SubscriptionColSortBy
{
get
{
return (Model.Subscription.SubscriptionCols)GetValue("SubscriptionColSortBy", (int)Model.Subscription.SubscriptionCols.ProgrammeName);
}

set
{
SetValue("SubscriptionColSortBy", (int)value);
}
}

internal static bool SubscriptionColSortAsc
{
get
{
return GetValue("SubscriptionColSortAsc", true);
}

set
{
SetValue("SubscriptionColSortAsc", value);
}
}

internal static string DownloadCols
{
get
Expand Down
1 change: 1 addition & 0 deletions Forms/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Forms/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,30 @@ private void ListSubscribed_SelectedIndexChanged(object sender, EventArgs e)
}
}

private void ListSubscribed_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
Model.Subscription.SubscriptionCols clickedCol = (Model.Subscription.SubscriptionCols)e.Column;

if (Model.Subscription.SortByColumn != clickedCol)
{
Model.Subscription.SortByColumn = clickedCol;
Model.Subscription.SortAscending = true;
}
else
{
Model.Subscription.SortAscending = !Model.Subscription.SortAscending;
}

// Set the column header to display the new sort order
this.ListSubscribed.ShowSortOnHeader((int)Model.Subscription.SortByColumn, Model.Subscription.SortAscending ? SortOrder.Ascending : SortOrder.Descending);

// Save the current sort
Settings.SubscriptionColSortBy = Model.Subscription.SortByColumn;
Settings.SubscriptionColSortAsc = Model.Subscription.SortAscending;

this.ListSubscribed.Sort();
}

private void ShowSubscriptionInfo(int progid)
{
Model.Subscription info = new Model.Subscription(progid);
Expand Down Expand Up @@ -2304,6 +2328,11 @@ private void InitSubscriptionList()
this.SetViewDefaults(); // Revert back to default sidebar and toolbar
}

// Apply the sort from the current settings
Model.Subscription.SortByColumn = Settings.SubscriptionColSortBy;
Model.Subscription.SortAscending = Settings.SubscriptionColSortAsc;
this.ListSubscribed.ShowSortOnHeader((int)Model.Subscription.SortByColumn, Model.Subscription.SortAscending ? SortOrder.Ascending : SortOrder.Descending);

// Convert the list of Subscription items to an array of ListItems
List<Model.Subscription> initData = Model.Subscription.FetchAll();
ListViewItem[] initItems = new ListViewItem[initData.Count];
Expand Down

0 comments on commit 11df3a2

Please sign in to comment.