aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-09-10 06:56:05 -0700
committerScott Jackson <daneren2005@gmail.com>2014-09-10 06:56:05 -0700
commitc6188027e605a9c4f8fc4b5f166f304dea28238b (patch)
tree83be2ee50544f4fe549dce06ee069f2c5c62ac37 /src
parent32abfa0ccd14d6b23af7324700a87ce36bac5bb3 (diff)
downloaddsub-c6188027e605a9c4f8fc4b5f166f304dea28238b.tar.gz
dsub-c6188027e605a9c4f8fc4b5f166f304dea28238b.tar.bz2
dsub-c6188027e605a9c4f8fc4b5f166f304dea28238b.zip
Move Indexes sorting to domain definition, add Artist.equals
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/domain/Artist.java67
-rw-r--r--src/github/daneren2005/dsub/domain/Indexes.java15
-rw-r--r--src/github/daneren2005/dsub/service/OfflineMusicService.java37
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