diff options
-rw-r--r-- | src/github/daneren2005/dsub/domain/Artist.java | 67 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/domain/Indexes.java | 15 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/service/OfflineMusicService.java | 37 |
3 files changed, 85 insertions, 34 deletions
diff --git a/src/github/daneren2005/dsub/domain/Artist.java b/src/github/daneren2005/dsub/domain/Artist.java index e4a9001b..f30147e6 100644 --- a/src/github/daneren2005/dsub/domain/Artist.java +++ b/src/github/daneren2005/dsub/domain/Artist.java @@ -18,12 +18,18 @@ */ package github.daneren2005.dsub.domain; +import android.util.Log; + import java.io.Serializable; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; /** * @author Sindre Mehus */ public class Artist implements Serializable { + private static final String TAG = Artist.class.getSimpleName(); private String id; private String name; @@ -71,8 +77,69 @@ public class Artist implements Serializable { this.closeness = closeness; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Artist entry = (Artist) o; + return id.equals(entry.id); + } + + @Override + public int hashCode() { + return id.hashCode(); + } + @Override public String toString() { return name; } + + public static class ArtistComparator implements Comparator<Artist> { + private String[] ignoredArticles; + + public ArtistComparator(String[] ignoredArticles) { + this.ignoredArticles = ignoredArticles; + } + + public int compare(Artist lhsArtist, Artist rhsArtist) { + String lhs = lhsArtist.getName().toLowerCase(); + String rhs = rhsArtist.getName().toLowerCase(); + + char lhs1 = lhs.charAt(0); + char rhs1 = rhs.charAt(0); + + if (Character.isDigit(lhs1) && !Character.isDigit(rhs1)) { + return 1; + } else if (Character.isDigit(rhs1) && !Character.isDigit(lhs1)) { + return -1; + } + + 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.compareTo(rhs); + } + } + + public static void sort(List<Artist> artists, String[] ignoredArticles) { + try { + Collections.sort(artists, new ArtistComparator(ignoredArticles)); + } catch (Exception e) { + Log.w(TAG, "Failed to sort artists", e); + } + } }
\ No newline at end of file diff --git a/src/github/daneren2005/dsub/domain/Indexes.java b/src/github/daneren2005/dsub/domain/Indexes.java index 63c3a8d4..e15ccf9f 100644 --- a/src/github/daneren2005/dsub/domain/Indexes.java +++ b/src/github/daneren2005/dsub/domain/Indexes.java @@ -18,10 +18,16 @@ */ package github.daneren2005.dsub.domain; +import android.content.Context; +import android.content.SharedPreferences; + import java.util.ArrayList; import java.util.List; import java.io.Serializable; +import github.daneren2005.dsub.util.Constants; +import github.daneren2005.dsub.util.Util; + /** * @author Sindre Mehus */ @@ -76,4 +82,13 @@ public class Indexes implements Serializable { public List<MusicDirectory.Entry> getEntries() { return entries; } + + public void sortChildren(Context context) { + SharedPreferences prefs = Util.getPreferences(context); + String ignoredArticlesString = prefs.getString(Constants.CACHE_KEY_IGNORE, "The El La Los Las Le Les"); + final String[] ignoredArticles = ignoredArticlesString.split(" "); + + Artist.sort(shortcuts, ignoredArticles); + Artist.sort(artists, ignoredArticles); + } }
\ No newline at end of file diff --git a/src/github/daneren2005/dsub/service/OfflineMusicService.java b/src/github/daneren2005/dsub/service/OfflineMusicService.java index 41489ee6..b62d04a1 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -92,40 +92,9 @@ public class OfflineMusicService implements MusicService { } } - SharedPreferences prefs = Util.getPreferences(context); - String ignoredArticlesString = prefs.getString(Constants.CACHE_KEY_IGNORE, "The El La Los Las Le Les"); - final String[] ignoredArticles = ignoredArticlesString.split(" "); - - Collections.sort(artists, new Comparator<Artist>() { - public int compare(Artist lhsArtist, Artist rhsArtist) { - String lhs = lhsArtist.getName().toLowerCase(); - String rhs = rhsArtist.getName().toLowerCase(); - - char lhs1 = lhs.charAt(0); - char rhs1 = rhs.charAt(0); - - if(Character.isDigit(lhs1) && !Character.isDigit(rhs1)) { - return 1; - } else if(Character.isDigit(rhs1) && !Character.isDigit(lhs1)) { - return -1; - } - - 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.compareTo(rhs); - } - }); - - return new Indexes(0L, Collections.<Artist>emptyList(), artists); + Indexes indexes = new Indexes(0L, Collections.<Artist>emptyList(), artists); + indexes.sortChildren(context); + return indexes; } @Override |