From e09bb5a01c7b1ff8e017b49179eac029cfc662dd Mon Sep 17 00:00:00 2001 From: Isabelle Riverain Date: Sun, 17 Aug 2014 11:31:07 +0100 Subject: [PATCH] Implement sorting in the favourites list Implement sorting the list of favourites in the same way as the downloads list. This resolves #126. --- AUTHORS.md | 3 ++ Classes/Model/Favourite.cs | 69 ++++++++++++++++++++++++++++++++++++-- Classes/Settings.cs | 26 ++++++++++++++ Forms/Main.Designer.cs | 1 + Forms/Main.cs | 29 ++++++++++++++++ 5 files changed, 126 insertions(+), 2 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index a47df572..92e8eb30 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -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 diff --git a/Classes/Model/Favourite.cs b/Classes/Model/Favourite.cs index d07fd293..2c10c40d 100644 --- a/Classes/Model/Favourite.cs +++ b/Classes/Model/Favourite.cs @@ -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 @@ -20,6 +20,7 @@ namespace RadioDld.Model using System.Collections.Generic; using System.Data.SQLite; using System.Globalization; + using System.IO; using System.Threading; internal class Favourite : Programme @@ -27,6 +28,9 @@ internal class Favourite : Programme private static Dictionary sortCache; private static object sortCacheLock = new object(); + private static FavouriteCols sortBy = FavouriteCols.ProgrammeName; + private static bool sortAsc; + static Favourite() { Programme.Updated += Programme_Updated; @@ -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 FetchAll() { List items = new List(); @@ -95,8 +147,21 @@ public static int Compare(int progid1, int progid2) sortCache = new Dictionary(); 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())) { diff --git a/Classes/Settings.cs b/Classes/Settings.cs index 87424868..2533bbba 100644 --- a/Classes/Settings.cs +++ b/Classes/Settings.cs @@ -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 diff --git a/Forms/Main.Designer.cs b/Forms/Main.Designer.cs index f682831f..bf7aee4a 100644 --- a/Forms/Main.Designer.cs +++ b/Forms/Main.Designer.cs @@ -695,6 +695,7 @@ private void InitializeComponent() this.ListFavourites.UseCompatibleStateImageBehavior = false; this.ListFavourites.View = System.Windows.Forms.View.Details; this.ListFavourites.ItemActivate += new System.EventHandler(this.ListFavourites_ItemActivate); + this.ListFavourites.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.ListFavourites_ColumnClick); this.ListFavourites.SelectedIndexChanged += new System.EventHandler(this.ListFavourites_SelectedIndexChanged); // // Main diff --git a/Forms/Main.cs b/Forms/Main.cs index 870d9a9b..e51bdb82 100644 --- a/Forms/Main.cs +++ b/Forms/Main.cs @@ -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]; @@ -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 initData = Model.Favourite.FetchAll(); ListViewItem[] initItems = new ListViewItem[initData.Count];