Skip to content

Commit

Permalink
Implement sorting in the favourites list
Browse files Browse the repository at this point in the history
Implement sorting the list of favourites in the same way as the downloads
list.  This resolves #126.
  • Loading branch information
IsaPlop authored and ribbons committed Aug 22, 2014
1 parent 11df3a2 commit e09bb5a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
3 changes: 3 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ Primary Developer
### Jon Egerton
Sorting in subscriptions list

### Isabelle Riverain
Sorting in favourites list

### Nick Sharples
Original proof of concept code for RSS server
69 changes: 67 additions & 2 deletions Classes/Model/Favourite.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 Favourite : Programme
{
private static Dictionary<int, int> sortCache;
private static object sortCacheLock = new object();

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

static Favourite()
{
Programme.Updated += Programme_Updated;
Expand All @@ -48,6 +52,54 @@ public Favourite(int progid)

public static event ProgrammeEventHandler Removed;

internal enum FavouriteCols
{
ProgrammeName = 0,
Provider = 1
}

public static FavouriteCols 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<Favourite> FetchAll()
{
List<Favourite> items = new List<Favourite>();
Expand Down Expand Up @@ -95,8 +147,21 @@ public static int Compare(int progid1, int progid2)
sortCache = new Dictionary<int, int>();

int sort = 0;
string orderBy = null;

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

using (SQLiteCommand command = new SQLiteCommand("select favourites.progid from favourites, programmes where programmes.progid=favourites.progid order by name", FetchDbConn()))
using (SQLiteCommand command = new SQLiteCommand("select favourites.progid from favourites, programmes where programmes.progid=favourites.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.Favourite.FavouriteCols FavouriteColSortBy
{
get
{
return (Model.Favourite.FavouriteCols)GetValue("FavouriteColSortBy", (int)Model.Favourite.FavouriteCols.ProgrammeName);
}

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

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

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

internal static Model.Subscription.SubscriptionCols SubscriptionColSortBy
{
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 @@ -634,6 +634,30 @@ private void ListDownloads_ColumnClick(object sender, System.Windows.Forms.Colum
this.ListDownloads.Sort();
}

private void ListFavourites_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
Model.Favourite.FavouriteCols clickedCol = (Model.Favourite.FavouriteCols)e.Column;

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

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

// Save the current sort
Settings.FavouriteColSortBy = Model.Favourite.SortByColumn;
Settings.FavouriteColSortAsc = Model.Favourite.SortAscending;

this.ListFavourites.Sort();
}

private void ListDownloads_ColumnReordered(object sender, System.Windows.Forms.ColumnReorderedEventArgs e)
{
string[] oldOrder = new string[this.ListDownloads.Columns.Count];
Expand Down Expand Up @@ -2308,6 +2332,11 @@ private void InitFavouriteList()
this.SetViewDefaults(); // Revert back to default sidebar and toolbar
}

// Apply the sort from the current settings
Model.Favourite.SortByColumn = Settings.FavouriteColSortBy;
Model.Favourite.SortAscending = Settings.FavouriteColSortAsc;
this.ListFavourites.ShowSortOnHeader((int)Model.Favourite.SortByColumn, Model.Favourite.SortAscending ? SortOrder.Ascending : SortOrder.Descending);

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

0 comments on commit e09bb5a

Please sign in to comment.