Skip to content

Commit

Permalink
Merge pull request #341 from marverenic/m/title-sort-localization
Browse files Browse the repository at this point in the history
Localize title sorting rules
  • Loading branch information
andrewbailey authored Feb 17, 2018
2 parents 4f1e4a9 + 8ad207e commit 059cac7
Show file tree
Hide file tree
Showing 29 changed files with 188 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public AutoPlaylist makePlaylist(AutoPlaylist playlist) {

AutoPlaylist created = new AutoPlaylist.Builder(playlist)
.setId(localReference.getPlaylistId())
.build();
.build(mContext);

saveAutoPlaylistConfiguration(created);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private static List<Playlist> getPlaylists(Context context, @Nullable String sel
return Collections.emptyList();
}

List<Playlist> playlists = Playlist.buildPlaylistList(cur);
List<Playlist> playlists = Playlist.buildPlaylistList(cur, context.getResources());
Collections.sort(playlists);
cur.close();

Expand Down Expand Up @@ -460,7 +460,7 @@ public static Playlist findPlaylistByName(Context context, String playlistName)
return null;
}

Playlist found = (cur.moveToFirst()) ? new Playlist(cur) : null;
Playlist found = (cur.moveToFirst()) ? new Playlist(cur, context.getResources()) : null;
cur.close();

return found;
Expand Down Expand Up @@ -496,7 +496,7 @@ public static Playlist createPlaylist(Context context, String playlistName,
}

cursor.moveToFirst();
Playlist playlist = new Playlist(cursor);
Playlist playlist = new Playlist(cursor, context.getResources());
cursor.close();

// If we have a list of songs, associate it with the playlist
Expand Down Expand Up @@ -644,7 +644,7 @@ public static List<Song> buildSongListFromFile(Context context, File file, Strin
return null;
}

List<Song> songs = getPlaylistSongs(context, new Playlist(cur));
List<Song> songs = getPlaylistSongs(context, new Playlist(cur, context.getResources()));
cur.close();
return songs;
} else {
Expand Down
65 changes: 11 additions & 54 deletions app/src/main/java/com/marverenic/music/model/Album.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import java.util.Comparator;
import java.util.List;

import static com.marverenic.music.model.ModelUtil.compareTitle;
import static com.marverenic.music.model.ModelUtil.hashLong;
import static com.marverenic.music.model.ModelUtil.parseUnknown;
import static com.marverenic.music.model.ModelUtil.sortableTitle;

public final class Album implements Parcelable, Comparable<Album> {

Expand All @@ -38,6 +38,8 @@ public Album[] newArray(int size) {
protected int year;
protected String artUri;

private String sortableName;

private Album() {

}
Expand All @@ -57,6 +59,8 @@ public Album(Resources res, Cursor cur) {
artistId = cur.getLong(cur.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
year = cur.getInt(cur.getColumnIndex(MediaStore.Audio.Albums.LAST_YEAR));
artUri = cur.getString(cur.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

sortableName = sortableTitle(albumName, res);
}

private Album(Parcel in) {
Expand All @@ -66,6 +70,8 @@ private Album(Parcel in) {
artistName = in.readString();
year = in.readInt();
artUri = in.readString();

sortableName = in.readString();
}

/**
Expand Down Expand Up @@ -101,6 +107,8 @@ public static List<Album> buildAlbumList(Cursor cur, Resources res) {
next.year = cur.getInt(yearIndex);
next.artUri = cur.getString(artIndex);

next.sortableName = sortableTitle(next.albumName, res);

albums.add(next);
}

Expand Down Expand Up @@ -159,65 +167,14 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(artistName);
dest.writeInt(year);
dest.writeString(artUri);
dest.writeString(sortableName);
}

@Override
public int compareTo(@NonNull Album another) {
return compareTitle(getAlbumName(), another.getAlbumName());
return sortableName.compareTo(another.sortableName);
}

public static final Comparator<Album> YEAR_COMPARATOR = (a1, a2) -> a1.getYear() - a2.getYear();

public static class Builder {

private long albumId;
private String albumName;
private long artistId;
private String artistName;
private int year;
private String artUri;

public Builder setAlbumId(long albumId) {
this.albumId = albumId;
return this;
}

public Builder setAlbumName(String albumName) {
this.albumName = albumName;
return this;
}

public Builder setArtistId(long artistId) {
this.artistId = artistId;
return this;
}

public Builder setArtistName(String artistName) {
this.artistName = artistName;
return this;
}

public Builder setYear(int year) {
this.year = year;
return this;
}

public Builder setArtUri(String artUri) {
this.artUri = artUri;
return this;
}

public Album build() {
Album built = new Album();

built.albumId = albumId;
built.albumName = albumName;
built.artistId = artistId;
built.artistName = artistName;
built.year = year;
built.artUri = artUri;

return built;
}
}
}
40 changes: 12 additions & 28 deletions app/src/main/java/com/marverenic/music/model/Artist.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.util.ArrayList;
import java.util.List;

import static com.marverenic.music.model.ModelUtil.compareTitle;
import static com.marverenic.music.model.ModelUtil.parseUnknown;
import static com.marverenic.music.model.ModelUtil.sortableTitle;

public final class Artist implements Parcelable, Comparable<Artist> {

Expand All @@ -32,6 +32,8 @@ public Artist[] newArray(int size) {
protected int artistId;
protected String artistName;

private String sortableName;

private Artist() {

}
Expand All @@ -46,11 +48,15 @@ public Artist(Context context, Cursor cur) {
artistName = parseUnknown(
cur.getString(cur.getColumnIndex(MediaStore.Audio.Artists.ARTIST)),
context.getResources().getString(R.string.unknown_artist));

sortableName = sortableTitle(artistName, context.getResources());
}

private Artist(Parcel in) {
artistId = in.readInt();
artistName = in.readString();

sortableName = in.readString();
}

/**
Expand All @@ -77,6 +83,8 @@ public static List<Artist> buildArtistList(Cursor cur, Resources res) {
next.artistId = cur.getInt(idIndex);
next.artistName = parseUnknown(cur.getString(artistIndex), unknownName);

next.sortableName = sortableTitle(next.artistName, res);

artists.add(next);
}

Expand Down Expand Up @@ -115,36 +123,12 @@ public int describeContents() {
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(artistId);
dest.writeString(artistName);

dest.writeString(sortableName);
}

@Override
public int compareTo(@NonNull Artist another) {
return compareTitle(getArtistName(), another.getArtistName());
}

public static class Builder {

private int artistId;
private String artistName;

public Builder setArtistId(int artistId) {
this.artistId = artistId;
return this;
}

public Builder setArtistName(String artistName) {
this.artistName = artistName;
return this;
}

public Artist build() {
Artist built = new Artist();

built.artistId = artistId;
built.artistName = artistName;

return built;
}

return sortableName.compareTo(another.sortableName);
}
}
15 changes: 9 additions & 6 deletions app/src/main/java/com/marverenic/music/model/AutoPlaylist.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.marverenic.music.model;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Parcel;
import android.os.Parcelable;

Expand Down Expand Up @@ -75,7 +77,7 @@ public class AutoPlaylist extends Playlist implements Parcelable {
@SerializedName("sortAscending")
private final boolean mSortAscending;

public static AutoPlaylist emptyPlaylist() {
public static AutoPlaylist emptyPlaylist(Context context) {
return new AutoPlaylist.Builder()
.setName("")
.setId(AutoPlaylist.Builder.NO_ID)
Expand All @@ -85,7 +87,7 @@ public static AutoPlaylist emptyPlaylist() {
.setTruncateMethod(AutoPlaylistRule.ID)
.setTruncateAscending(true)
.setRules(AutoPlaylistRule.emptyRule())
.build();
.build(context);
}

/**
Expand All @@ -106,8 +108,8 @@ public static AutoPlaylist emptyPlaylist() {
*/
private AutoPlaylist(long playlistId, String playlistName, int maximumEntries, int sortMethod,
int truncateMethod, boolean truncateAscending, boolean sortAscending,
boolean matchAllRules, List<AutoPlaylistRule> rules) {
super(playlistId, playlistName);
boolean matchAllRules, List<AutoPlaylistRule> rules, Resources res) {
super(playlistId, playlistName, res);
mMaximumEntries = maximumEntries;
mMatchAllRules = matchAllRules;
mRules = Collections.unmodifiableList(rules);
Expand Down Expand Up @@ -456,9 +458,10 @@ && getSortMethod() == reference.getSortMethod()
&& isSortAscending() == reference.isSortAscending();
}

public AutoPlaylist build() {
public AutoPlaylist build(Context context) {
return new AutoPlaylist(mId, mName, mMaximumEntries, mSortMethod, mTruncateMethod,
mTruncateAscending, mSortAscending, mMatchAllRules, mRules);
mTruncateAscending, mSortAscending, mMatchAllRules, mRules,
context.getResources());
}

@Override
Expand Down
34 changes: 7 additions & 27 deletions app/src/main/java/com/marverenic/music/model/Genre.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import java.util.ArrayList;
import java.util.List;

import static com.marverenic.music.model.ModelUtil.compareTitle;
import static com.marverenic.music.model.ModelUtil.hashLong;
import static com.marverenic.music.model.ModelUtil.parseUnknown;
import static com.marverenic.music.model.ModelUtil.sortableTitle;

public final class Genre implements Parcelable, Comparable<Genre> {

Expand All @@ -32,13 +32,16 @@ public Genre[] newArray(int size) {
protected long genreId;
protected String genreName;

private String sortableName;

private Genre() {

}

private Genre(Parcel in) {
genreId = in.readLong();
genreName = in.readString();
sortableName = in.readString();
}

/**
Expand Down Expand Up @@ -66,6 +69,8 @@ public static List<Genre> buildGenreList(Context context, Cursor cur) {
next.genreId = cur.getLong(idIndex);
next.genreName = parseUnknown(cur.getString(nameIndex), unknownName);

next.sortableName = sortableTitle(next.genreName, context.getResources());

genres.add(next);
}
return genres;
Expand Down Expand Up @@ -107,32 +112,7 @@ public void writeToParcel(Parcel dest, int flags) {

@Override
public int compareTo(@NonNull Genre another) {
return compareTitle(getGenreName(), another.getGenreName());
return sortableName.compareTo(another.sortableName);
}

public static class Builder {

private long genreId;
private String genreName;

public Builder setGenreId(long genreId) {
this.genreId = genreId;
return this;
}

public Builder setGenreName(String genreName) {
this.genreName = genreName;
return this;
}

public Genre build() {
Genre built = new Genre();

built.genreId = genreId;
built.genreName = genreName;

return built;
}

}
}
Loading

0 comments on commit 059cac7

Please sign in to comment.