From ad66a9397d5d1541fd47b4122daf955538d93c63 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 9 Dec 2013 21:38:00 -0800 Subject: #213 Use ignored articles in podcast sort --- .../daneren2005/dsub/domain/PodcastChannel.java | 33 ++++++++++++++++++++-- .../dsub/service/parser/PodcastChannelParser.java | 2 +- 2 files changed, 32 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/domain/PodcastChannel.java b/src/github/daneren2005/dsub/domain/PodcastChannel.java index 8549b06d..912b0e29 100644 --- a/src/github/daneren2005/dsub/domain/PodcastChannel.java +++ b/src/github/daneren2005/dsub/domain/PodcastChannel.java @@ -18,7 +18,16 @@ */ package github.daneren2005.dsub.domain; +import android.content.Context; +import android.content.SharedPreferences; + import java.io.Serializable; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import github.daneren2005.dsub.util.Constants; +import github.daneren2005.dsub.util.Util; /** * @@ -79,12 +88,32 @@ public class PodcastChannel implements Serializable { } public static class PodcastComparator implements Comparator { + private static String[] ignoredArticles; + @Override public int compare(PodcastChannel podcast1, PodcastChannel podcast2) { - return podcast1.getName().compareToIgnoreCase(podcast2.getName()); + String lhs = podcast1.getName().toLowerCase(); + String rhs = podcast2.getName().toLowerCase(); + + for(String article: ignoredArticles) { + int index = lhs.indexOf(article.toLowerCase() + " "); + if(index == 0) { + lhs = lhs.substring(article.length() + 1); + } + index = rhs.indexOf(article.toLowerCase() + " "); + if(index == 0) { + rhs = rhs.substring(article.length() + 1); + } + } + + return lhs.compareToIgnoreCase(rhs); } - public static List sort(List podcasts) { + public static List sort(List podcasts, Context context) { + SharedPreferences prefs = Util.getPreferences(context); + String ignoredArticlesString = prefs.getString(Constants.CACHE_KEY_IGNORE, "The El La Los Las Le Les"); + ignoredArticles = ignoredArticlesString.split(" "); + Collections.sort(podcasts, new PodcastComparator()); return podcasts; } diff --git a/src/github/daneren2005/dsub/service/parser/PodcastChannelParser.java b/src/github/daneren2005/dsub/service/parser/PodcastChannelParser.java index 570ec423..bfdef7f2 100644 --- a/src/github/daneren2005/dsub/service/parser/PodcastChannelParser.java +++ b/src/github/daneren2005/dsub/service/parser/PodcastChannelParser.java @@ -62,6 +62,6 @@ public class PodcastChannelParser extends AbstractParser { } while (eventType != XmlPullParser.END_DOCUMENT); validate(); - return PodcastChannel.PodcastComparator.sort(channels); + return PodcastChannel.PodcastComparator.sort(channels, context); } } -- cgit v1.2.3