From 302dd4b2e8ea0514cd489295f403edd4bfba33f1 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 24 Jun 2015 18:32:20 -0700 Subject: Make UpdateView be template based for easier use --- .../daneren2005/dsub/adapter/MainAdapter.java | 3 +- .../daneren2005/dsub/adapter/SearchAdapter.java | 4 +- .../daneren2005/dsub/view/AlbumListCountView.java | 6 +- .../github/daneren2005/dsub/view/AlbumView.java | 23 ++-- .../daneren2005/dsub/view/ArtistEntryView.java | 15 +-- .../github/daneren2005/dsub/view/ArtistView.java | 13 +- .../daneren2005/dsub/view/BasicHeaderView.java | 6 +- .../daneren2005/dsub/view/BasicListView.java | 7 +- .../github/daneren2005/dsub/view/GenreView.java | 5 +- .../daneren2005/dsub/view/PlaylistSongView.java | 16 +-- .../github/daneren2005/dsub/view/PlaylistView.java | 11 +- .../daneren2005/dsub/view/PodcastChannelView.java | 11 +- .../github/daneren2005/dsub/view/SettingView.java | 20 +-- .../github/daneren2005/dsub/view/ShareView.java | 5 +- .../github/daneren2005/dsub/view/SongView.java | 149 ++++++++++----------- .../github/daneren2005/dsub/view/UpdateView.java | 43 ++---- .../github/daneren2005/dsub/view/UpdateView2.java | 47 +++++++ .../github/daneren2005/dsub/view/UserView.java | 10 +- 18 files changed, 183 insertions(+), 211 deletions(-) create mode 100644 app/src/main/java/github/daneren2005/dsub/view/UpdateView2.java (limited to 'app') diff --git a/app/src/main/java/github/daneren2005/dsub/adapter/MainAdapter.java b/app/src/main/java/github/daneren2005/dsub/adapter/MainAdapter.java index e451bc19..8f1f1c38 100644 --- a/app/src/main/java/github/daneren2005/dsub/adapter/MainAdapter.java +++ b/app/src/main/java/github/daneren2005/dsub/adapter/MainAdapter.java @@ -59,8 +59,7 @@ public class MainAdapter extends SectionAdapter { if(viewType == VIEW_TYPE_ALBUM_LIST) { updateView.setObject(context.getResources().getString(item)); } else { - // Hacky method to make sure updateBackground is called in background thread - updateView.setObject(item, null); + updateView.setObject(item); } } diff --git a/app/src/main/java/github/daneren2005/dsub/adapter/SearchAdapter.java b/app/src/main/java/github/daneren2005/dsub/adapter/SearchAdapter.java index f395216e..c1185702 100644 --- a/app/src/main/java/github/daneren2005/dsub/adapter/SearchAdapter.java +++ b/app/src/main/java/github/daneren2005/dsub/adapter/SearchAdapter.java @@ -85,10 +85,10 @@ public class SearchAdapter extends SectionAdapter { UpdateView view = holder.getUpdateView(); if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) { AlbumView albumView = (AlbumView) view; - albumView.setObject(item, imageLoader); + albumView.setObject((Entry) item, imageLoader); } else if(viewType == VIEW_TYPE_SONG) { SongView songView = (SongView) view; - songView.setObject(item, false); + songView.setObject((Entry) item, false); } else if(viewType == VIEW_TYPE_ARTIST) { view.setObject(item); } diff --git a/app/src/main/java/github/daneren2005/dsub/view/AlbumListCountView.java b/app/src/main/java/github/daneren2005/dsub/view/AlbumListCountView.java index 81695f53..a1b14f61 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/AlbumListCountView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/AlbumListCountView.java @@ -32,12 +32,11 @@ import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.Util; -public class AlbumListCountView extends UpdateView { +public class AlbumListCountView extends UpdateView2 { private final String TAG = AlbumListCountView.class.getSimpleName(); private TextView titleView; private TextView countView; - private int albumListString; private int startCount; private int count = 0; @@ -50,8 +49,7 @@ public class AlbumListCountView extends UpdateView { countView = (TextView) findViewById(R.id.basic_count_count); } - protected void setObjectImpl(Object obj1, Object obj2) { - this.albumListString = (Integer) obj1; + protected void setObjectImpl(Integer albumListString, Void dummy) { titleView.setText(albumListString); SharedPreferences prefs = Util.getPreferences(context); diff --git a/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java b/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java index 69f2ef13..3104864a 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java @@ -20,7 +20,6 @@ package github.daneren2005.dsub.view; import android.content.Context; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageButton; @@ -31,17 +30,13 @@ import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.ImageLoader; -import github.daneren2005.dsub.util.Util; + import java.io.File; -import java.util.List; -public class AlbumView extends UpdateView { +public class AlbumView extends UpdateView2 { private static final String TAG = AlbumView.class.getSimpleName(); - private Context context; - private MusicDirectory.Entry album; private File file; - private View coverArtView; private TextView titleView; private TextView artistView; @@ -49,7 +44,6 @@ public class AlbumView extends UpdateView { public AlbumView(Context context, boolean cell) { super(context); - this.context = context; if(cell) { LayoutInflater.from(context).inflate(R.layout.album_cell_item, this, true); @@ -72,8 +66,7 @@ public class AlbumView extends UpdateView { this.showArtist = showArtist; } - protected void setObjectImpl(Object obj1, Object obj2) { - this.album = (MusicDirectory.Entry) obj1; + protected void setObjectImpl(MusicDirectory.Entry album, ImageLoader imageLoader) { titleView.setText(album.getAlbumDisplay()); String artist = ""; if(showArtist) { @@ -88,23 +81,23 @@ public class AlbumView extends UpdateView { artist += album.getYear(); } artistView.setText(album.getArtist() == null ? "" : artist); - imageTask = ((ImageLoader)obj2).loadImage(coverArtView, album, false, true); + imageTask = imageLoader.loadImage(coverArtView, album, false, true); file = null; } @Override protected void updateBackground() { if(file == null) { - file = FileUtil.getAlbumDirectory(context, album); + file = FileUtil.getAlbumDirectory(context, item); } exists = file.exists(); - isStarred = album.isStarred(); - isRated = album.getRating(); + isStarred = item.isStarred(); + isRated = item.getRating(); } public MusicDirectory.Entry getEntry() { - return album; + return item; } public File getFile() { diff --git a/app/src/main/java/github/daneren2005/dsub/view/ArtistEntryView.java b/app/src/main/java/github/daneren2005/dsub/view/ArtistEntryView.java index 71bdeb78..f6a6adb3 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/ArtistEntryView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/ArtistEntryView.java @@ -19,7 +19,6 @@ package github.daneren2005.dsub.view; import android.content.Context; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageButton; @@ -28,26 +27,21 @@ import android.widget.TextView; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.util.FileUtil; -import github.daneren2005.dsub.util.ImageLoader; -import github.daneren2005.dsub.util.Util; + import java.io.File; /** * Used to display albums in a {@code ListView}. * * @author Sindre Mehus */ -public class ArtistEntryView extends UpdateView { +public class ArtistEntryView extends UpdateView { private static final String TAG = ArtistEntryView.class.getSimpleName(); - private Context context; - private MusicDirectory.Entry artist; private File file; - private TextView titleView; public ArtistEntryView(Context context) { super(context); - this.context = context; LayoutInflater.from(context).inflate(R.layout.basic_list_item, this, true); titleView = (TextView) findViewById(R.id.item_name); @@ -61,8 +55,7 @@ public class ArtistEntryView extends UpdateView { }); } - protected void setObjectImpl(Object obj) { - this.artist = (MusicDirectory.Entry) obj; + protected void setObjectImpl(MusicDirectory.Entry artist) { titleView.setText(artist.getTitle()); file = FileUtil.getArtistDirectory(context, artist); } @@ -70,7 +63,7 @@ public class ArtistEntryView extends UpdateView { @Override protected void updateBackground() { exists = file.exists(); - isStarred = artist.isStarred(); + isStarred = item.isStarred(); } public File getFile() { diff --git a/app/src/main/java/github/daneren2005/dsub/view/ArtistView.java b/app/src/main/java/github/daneren2005/dsub/view/ArtistView.java index c255be69..d86c5d26 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/ArtistView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/ArtistView.java @@ -35,18 +35,14 @@ import java.io.File; * * @author Sindre Mehus */ -public class ArtistView extends UpdateView { +public class ArtistView extends UpdateView { private static final String TAG = ArtistView.class.getSimpleName(); - - private Context context; - private Artist artist; - private File file; + private File file; private TextView titleView; public ArtistView(Context context) { super(context); - this.context = context; LayoutInflater.from(context).inflate(R.layout.basic_list_item, this, true); titleView = (TextView) findViewById(R.id.item_name); @@ -60,8 +56,7 @@ public class ArtistView extends UpdateView { }); } - protected void setObjectImpl(Object obj) { - this.artist = (Artist) obj; + protected void setObjectImpl(Artist artist) { titleView.setText(artist.getName()); file = FileUtil.getArtistDirectory(context, artist); } @@ -69,7 +64,7 @@ public class ArtistView extends UpdateView { @Override protected void updateBackground() { exists = file.exists(); - isStarred = artist.isStarred(); + isStarred = item.isStarred(); } public File getFile() { diff --git a/app/src/main/java/github/daneren2005/dsub/view/BasicHeaderView.java b/app/src/main/java/github/daneren2005/dsub/view/BasicHeaderView.java index b5dad2d1..01aa42e1 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/BasicHeaderView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/BasicHeaderView.java @@ -21,7 +21,7 @@ import android.widget.TextView; import github.daneren2005.dsub.R; -public class BasicHeaderView extends UpdateView { +public class BasicHeaderView extends UpdateView { TextView nameView; public BasicHeaderView(Context context) { @@ -34,7 +34,7 @@ public class BasicHeaderView extends UpdateView { nameView = (TextView) findViewById(R.id.item_name); } - protected void setObjectImpl(Object obj) { - nameView.setText((String) obj); + protected void setObjectImpl(String string) { + nameView.setText(string); } } diff --git a/app/src/main/java/github/daneren2005/dsub/view/BasicListView.java b/app/src/main/java/github/daneren2005/dsub/view/BasicListView.java index 3169f903..ca7e8993 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/BasicListView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/BasicListView.java @@ -24,12 +24,11 @@ import android.widget.TextView; import github.daneren2005.dsub.R; -public class BasicListView extends UpdateView { +public class BasicListView extends UpdateView { private TextView titleView; public BasicListView(Context context) { super(context, false); - this.context = context; LayoutInflater.from(context).inflate(R.layout.basic_list_item, this, true); titleView = (TextView) findViewById(R.id.item_name); @@ -39,7 +38,7 @@ public class BasicListView extends UpdateView { moreButton.setVisibility(View.GONE); } - protected void setObjectImpl(Object obj) { - titleView.setText((String) obj); + protected void setObjectImpl(String string) { + titleView.setText(string); } } diff --git a/app/src/main/java/github/daneren2005/dsub/view/GenreView.java b/app/src/main/java/github/daneren2005/dsub/view/GenreView.java index 8dbcf89d..b29aefba 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/GenreView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/GenreView.java @@ -25,7 +25,7 @@ import android.widget.TextView; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.Genre; -public class GenreView extends UpdateView { +public class GenreView extends UpdateView { private static final String TAG = GenreView.class.getSimpleName(); private TextView titleView; @@ -41,8 +41,7 @@ public class GenreView extends UpdateView { albumsView = (TextView) findViewById(R.id.genre_albums); } - public void setObjectImpl(Object obj) { - Genre genre = (Genre) obj; + public void setObjectImpl(Genre genre) { titleView.setText(genre.getName()); if(genre.getAlbumCount() != null) { diff --git a/app/src/main/java/github/daneren2005/dsub/view/PlaylistSongView.java b/app/src/main/java/github/daneren2005/dsub/view/PlaylistSongView.java index 0264a785..581b92e4 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/PlaylistSongView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/PlaylistSongView.java @@ -34,16 +34,12 @@ import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.SyncUtil; import github.daneren2005.dsub.util.Util; -public class PlaylistSongView extends UpdateView { +public class PlaylistSongView extends UpdateView2> { private static final String TAG = PlaylistSongView.class.getSimpleName(); - private Context context; - private Playlist playlist; - private TextView titleView; private TextView countView; private int count = 0; - private List songs; public PlaylistSongView(Context context) { super(context, false); @@ -54,9 +50,7 @@ public class PlaylistSongView extends UpdateView { countView = (TextView) findViewById(R.id.basic_count_count); } - protected void setObjectImpl(Object obj1, Object obj2) { - this.playlist = (Playlist) obj1; - this.songs = (List) obj2; + protected void setObjectImpl(Playlist playlist, List songs) { count = 0; titleView.setText(playlist.getName()); // Make sure to hide initially so it's not present briefly before update @@ -69,11 +63,11 @@ public class PlaylistSongView extends UpdateView { count = 0; // Don't try to lookup playlist for Create New - if(!"-1".equals(playlist.getId())) { - MusicDirectory cache = FileUtil.deserialize(context, Util.getCacheName(context, "playlist", playlist.getId()), MusicDirectory.class); + if(!"-1".equals(item.getId())) { + MusicDirectory cache = FileUtil.deserialize(context, Util.getCacheName(context, "playlist", item.getId()), MusicDirectory.class); if(cache != null) { // Try to find song instances in the given playlists - for(MusicDirectory.Entry song: songs) { + for(MusicDirectory.Entry song: item2) { if(cache.getChildren().contains(song)) { count++; } diff --git a/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java b/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java index 29d5cc1c..cb7bd186 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java @@ -33,17 +33,13 @@ import github.daneren2005.dsub.util.SyncUtil; * * @author Sindre Mehus */ -public class PlaylistView extends UpdateView { +public class PlaylistView extends UpdateView { private static final String TAG = PlaylistView.class.getSimpleName(); - private Context context; - private Playlist playlist; - private TextView titleView; public PlaylistView(Context context) { super(context); - this.context = context; LayoutInflater.from(context).inflate(R.layout.basic_list_item, this, true); titleView = (TextView) findViewById(R.id.item_name); @@ -52,13 +48,12 @@ public class PlaylistView extends UpdateView { moreButton = (ImageView) findViewById(R.id.item_more); } - protected void setObjectImpl(Object obj) { - this.playlist = (Playlist) obj; + protected void setObjectImpl(Playlist playlist) { titleView.setText(playlist.getName()); } @Override protected void updateBackground() { - pinned = SyncUtil.isSyncedPlaylist(context, playlist.getId()); + pinned = SyncUtil.isSyncedPlaylist(context, item.getId()); } } diff --git a/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java b/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java index ada8019e..4878ad67 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java @@ -30,18 +30,14 @@ import github.daneren2005.dsub.util.SyncUtil; import github.daneren2005.dsub.util.FileUtil; import java.io.File; -public class PodcastChannelView extends UpdateView { +public class PodcastChannelView extends UpdateView { private static final String TAG = PodcastChannelView.class.getSimpleName(); - private Context context; - private PodcastChannel channel; private File file; - private TextView titleView; public PodcastChannelView(Context context) { super(context); - this.context = context; LayoutInflater.from(context).inflate(R.layout.basic_list_item, this, true); titleView = (TextView) findViewById(R.id.item_name); @@ -55,8 +51,7 @@ public class PodcastChannelView extends UpdateView { }); } - protected void setObjectImpl(Object obj) { - channel = (PodcastChannel) obj; + protected void setObjectImpl(PodcastChannel channel) { if(channel.getName() != null) { titleView.setText(channel.getName()); } else { @@ -67,7 +62,7 @@ public class PodcastChannelView extends UpdateView { @Override protected void updateBackground() { - if(SyncUtil.isSyncedPodcast(context, channel.getId())) { + if(SyncUtil.isSyncedPodcast(context, item.getId())) { if(exists) { shaded = false; exists = false; diff --git a/app/src/main/java/github/daneren2005/dsub/view/SettingView.java b/app/src/main/java/github/daneren2005/dsub/view/SettingView.java index 63b2c844..d46dc5d2 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/SettingView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/SettingView.java @@ -26,13 +26,10 @@ import github.daneren2005.dsub.domain.User; import static github.daneren2005.dsub.domain.User.Setting; -public class SettingView extends UpdateView { +public class SettingView extends UpdateView2 { private final TextView titleView; private final CheckBox checkBox; - private Setting setting; - private boolean enabled = false; - public SettingView(Context context) { super(context, false); this.context = context; @@ -43,21 +40,19 @@ public class SettingView extends UpdateView { checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if(setting != null) { - setting.setValue(isChecked); + if(item != null) { + item.setValue(isChecked); } } }); checkBox.setClickable(false); } - protected void setObjectImpl(Object obj, Object editable) { - this.setting = (Setting) obj; - + protected void setObjectImpl(Setting setting, Boolean isEditable) { // Can't edit non-role parts String name = setting.getName(); if(name.indexOf("Role") == -1) { - editable = false; + item2 = false; } int res = -1; @@ -100,13 +95,12 @@ public class SettingView extends UpdateView { checkBox.setChecked(false); } - enabled = (Boolean) editable; - checkBox.setEnabled(enabled); + checkBox.setEnabled(item2); } @Override public boolean isCheckable() { - return this.enabled; + return item2; } public void setChecked(boolean checked) { diff --git a/app/src/main/java/github/daneren2005/dsub/view/ShareView.java b/app/src/main/java/github/daneren2005/dsub/view/ShareView.java index bfb5b198..12294369 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/ShareView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/ShareView.java @@ -31,7 +31,7 @@ import java.util.Locale; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.Share; -public class ShareView extends UpdateView { +public class ShareView extends UpdateView { private static final String TAG = ShareView.class.getSimpleName(); private TextView titleView; @@ -53,8 +53,7 @@ public class ShareView extends UpdateView { }); } - public void setObjectImpl(Object obj) { - Share share = (Share) obj; + public void setObjectImpl(Share share) { titleView.setText(share.getName()); if(share.getExpires() != null) { descriptionView.setText(context.getResources().getString(R.string.share_expires, new SimpleDateFormat("E MMM d, yyyy", Locale.ENGLISH).format(share.getExpires()))); diff --git a/app/src/main/java/github/daneren2005/dsub/view/SongView.java b/app/src/main/java/github/daneren2005/dsub/view/SongView.java index 49a09578..fadf100e 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/SongView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/SongView.java @@ -38,20 +38,18 @@ import java.io.File; * * @author Sindre Mehus */ -public class SongView extends UpdateView implements Checkable { - private static final String TAG = SongView.class.getSimpleName(); - - private MusicDirectory.Entry song; - - private CheckedTextView checkedTextView; - private TextView titleTextView; - private TextView artistTextView; - private TextView durationTextView; - private TextView statusTextView; +public class SongView extends UpdateView2 implements Checkable { + private static final String TAG = SongView.class.getSimpleName(); + + private CheckedTextView checkedTextView; + private TextView titleTextView; + private TextView artistTextView; + private TextView durationTextView; + private TextView statusTextView; private ImageView statusImageView; private ImageView bookmarkButton; private View bottomRowView; - + private DownloadService downloadService; private long revision = -1; private DownloadFile downloadFile; @@ -68,30 +66,29 @@ public class SongView extends UpdateView implements Checkable { private boolean isBookmarked = false; private boolean bookmarked = false; - public SongView(Context context) { - super(context); - LayoutInflater.from(context).inflate(R.layout.song_list_item, this, true); + public SongView(Context context) { + super(context); + LayoutInflater.from(context).inflate(R.layout.song_list_item, this, true); - checkedTextView = (CheckedTextView) findViewById(R.id.song_check); - titleTextView = (TextView) findViewById(R.id.song_title); - artistTextView = (TextView) findViewById(R.id.song_artist); - durationTextView = (TextView) findViewById(R.id.song_duration); - statusTextView = (TextView) findViewById(R.id.song_status); + checkedTextView = (CheckedTextView) findViewById(R.id.song_check); + titleTextView = (TextView) findViewById(R.id.song_title); + artistTextView = (TextView) findViewById(R.id.song_artist); + durationTextView = (TextView) findViewById(R.id.song_duration); + statusTextView = (TextView) findViewById(R.id.song_status); statusImageView = (ImageView) findViewById(R.id.song_status_icon); ratingBar = (RatingBar) findViewById(R.id.song_rating); - starButton = (ImageButton) findViewById(R.id.song_star); - starButton.setFocusable(false); + starButton = (ImageButton) findViewById(R.id.song_star); + starButton.setFocusable(false); bookmarkButton = (ImageButton) findViewById(R.id.song_bookmark); bookmarkButton.setFocusable(false); moreButton = (ImageView) findViewById(R.id.more_button); bottomRowView = findViewById(R.id.song_bottom); - } + } + + public void setObjectImpl(MusicDirectory.Entry song, Boolean checkable) { + this.checkable = checkable; - public void setObjectImpl(Object obj1, Object obj2) { - this.song = (MusicDirectory.Entry) obj1; - checkable = (Boolean) obj2; - - StringBuilder artist = new StringBuilder(40); + StringBuilder artist = new StringBuilder(40); boolean isPodcast = song instanceof PodcastEpisode; if(!song.isVideo() || isPodcast) { @@ -105,11 +102,11 @@ public class SongView extends UpdateView implements Checkable { else if(song.getArtist() != null) { artist.append(song.getArtist()); } - + if(isPodcast) { String status = ((PodcastEpisode) song).getStatus(); int statusRes = -1; - + if("error".equals(status)) { statusRes = R.string.song_details_error; } else if("skipped".equals(status)) { @@ -117,7 +114,7 @@ public class SongView extends UpdateView implements Checkable { } else if("downloading".equals(status)) { statusRes = R.string.song_details_downloading; } - + if(statusRes != -1) { artist.append(" ("); artist.append(getContext().getString(statusRes)); @@ -131,16 +128,16 @@ public class SongView extends UpdateView implements Checkable { bottomRowView.setVisibility(View.GONE); statusTextView.setText(Util.formatDuration(song.getDuration())); } - + String title = song.getTitle(); Integer track = song.getTrack(); if(track != null && Util.getDisplayTrack(context)) { title = String.format("%02d", track) + " " + title; } - titleTextView.setText(title); + titleTextView.setText(title); artistTextView.setText(artist); - checkedTextView.setVisibility(checkable ? View.VISIBLE : View.GONE); + checkedTextView.setVisibility(checkable ? View.VISIBLE : View.GONE); this.setBackgroundColor(0x00000000); ratingBar.setVisibility(View.GONE); @@ -150,28 +147,28 @@ public class SongView extends UpdateView implements Checkable { loaded = false; dontChangeDownloadFile = false; } - + public void setDownloadFile(DownloadFile downloadFile) { this.downloadFile = downloadFile; dontChangeDownloadFile = true; } - + public DownloadFile getDownloadFile() { return downloadFile; } - + @Override protected void updateBackground() { - if (downloadService == null) { + if (downloadService == null) { downloadService = DownloadService.getInstance(); if(downloadService == null) { return; } - } + } long newRevision = downloadService.getDownloadListUpdateRevision(); if((revision != newRevision && dontChangeDownloadFile == false) || downloadFile == null) { - downloadFile = downloadService.forSong(song); + downloadFile = downloadService.forSong(item); revision = newRevision; } @@ -179,27 +176,27 @@ public class SongView extends UpdateView implements Checkable { isSaved = downloadFile.isSaved(); partialFile = downloadFile.getPartialFile(); partialFileExists = partialFile.exists(); - isStarred = song.isStarred(); - isBookmarked = song.getBookmark() != null; - isRated = song.getRating(); - + isStarred = item.isStarred(); + isBookmarked = item.getBookmark() != null; + isRated = item.getRating(); + // Check if needs to load metadata: check against all fields that we know are null in offline mode - if(song.getBitRate() == null && song.getDuration() == null && song.getDiscNumber() == null && isWorkDone) { - song.loadMetadata(downloadFile.getCompleteFile()); + if(item.getBitRate() == null && item.getDuration() == null && item.getDiscNumber() == null && isWorkDone) { + item.loadMetadata(downloadFile.getCompleteFile()); loaded = true; } } @Override - protected void update() { + protected void update() { if(loaded) { - setObjectImpl(song, checkedTextView.getVisibility() == View.VISIBLE); + setObjectImpl(item, checkedTextView.getVisibility() == View.VISIBLE); + } + if (downloadService == null || downloadFile == null) { + return; } - if (downloadService == null || downloadFile == null) { - return; - } - if(song.isStarred()) { + if(item.isStarred()) { if(!starred) { starButton.setVisibility(View.VISIBLE); starred = true; @@ -211,13 +208,13 @@ public class SongView extends UpdateView implements Checkable { } } - if (isWorkDone) { + if (isWorkDone) { int moreImage = isSaved ? R.drawable.download_pinned : R.drawable.download_cached; if(moreImage != this.moreImage) { moreButton.setImageResource(moreImage); this.moreImage = moreImage; } - } else if(this.moreImage != R.drawable.download_none_light) { + } else if(this.moreImage != R.drawable.download_none_light) { int[] attrs = new int[] {R.attr.download_none}; TypedArray typedArray = context.obtainStyledAttributes(attrs); moreButton.setImageResource(typedArray.getResourceId(0, 0)); @@ -225,7 +222,7 @@ public class SongView extends UpdateView implements Checkable { this.moreImage = R.drawable.download_none_light; } - if (downloadFile.isDownloading() && !downloadFile.isDownloadCancelled() && partialFileExists) { + if (downloadFile.isDownloading() && !downloadFile.isDownloadCancelled() && partialFileExists) { double percentage = (partialFile.length() * 100.0) / downloadFile.getEstimatedSize(); percentage = Math.min(percentage, 100); statusTextView.setText((int)percentage + " %"); @@ -233,24 +230,24 @@ public class SongView extends UpdateView implements Checkable { statusImageView.setVisibility(View.VISIBLE); rightImage = true; } - } else if(rightImage) { - statusTextView.setText(null); + } else if(rightImage) { + statusTextView.setText(null); statusImageView.setVisibility(View.GONE); rightImage = false; - } + } - boolean playing = downloadService.getCurrentPlaying() == downloadFile; - if (playing) { + boolean playing = downloadService.getCurrentPlaying() == downloadFile; + if (playing) { if(!this.playing) { this.playing = playing; int[] attrs = new int[] {R.attr.media_button_start}; TypedArray typedArray = context.obtainStyledAttributes(attrs); - titleTextView.setCompoundDrawablesWithIntrinsicBounds(typedArray.getResourceId(0, 0), 0, 0, 0); + titleTextView.setCompoundDrawablesWithIntrinsicBounds(typedArray.getResourceId(0, 0), 0, 0, 0); } - } else { + } else { if(this.playing) { this.playing = playing; - titleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + titleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } } @@ -290,24 +287,24 @@ public class SongView extends UpdateView implements Checkable { rating = isRated; } - } + } - @Override - public void setChecked(boolean b) { - checkedTextView.setChecked(b); - } + @Override + public void setChecked(boolean b) { + checkedTextView.setChecked(b); + } - @Override - public boolean isChecked() { - return checkedTextView.isChecked(); - } + @Override + public boolean isChecked() { + return checkedTextView.isChecked(); + } - @Override - public void toggle() { - checkedTextView.toggle(); - } + @Override + public void toggle() { + checkedTextView.toggle(); + } public MusicDirectory.Entry getEntry() { - return song; + return item; } } diff --git a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java index 44831c60..8e1029df 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java @@ -20,7 +20,6 @@ package github.daneren2005.dsub.view; import android.content.Context; import android.content.res.TypedArray; -import android.graphics.Color; import android.os.Handler; import android.os.Looper; import android.support.v7.widget.RecyclerView; @@ -38,20 +37,20 @@ import java.util.List; import java.util.WeakHashMap; import github.daneren2005.dsub.domain.MusicDirectory; -import github.daneren2005.dsub.util.ImageLoader; import github.daneren2005.dsub.R; import github.daneren2005.dsub.util.SilentBackgroundTask; -public class UpdateView extends LinearLayout { +public abstract class UpdateView extends LinearLayout { private static final String TAG = UpdateView.class.getSimpleName(); private static final WeakHashMap INSTANCES = new WeakHashMap(); - private static Handler backgroundHandler; - private static Handler uiHandler; + protected static Handler backgroundHandler; + protected static Handler uiHandler; private static Runnable updateRunnable; private static int activeActivities = 0; protected Context context; + protected T item; protected RatingBar ratingBar; protected ImageButton starButton; protected ImageView moreButton; @@ -91,37 +90,16 @@ public class UpdateView extends LinearLayout { } - public void setObject(Object obj) { + public void setObject(T obj) { + item = obj; setObjectImpl(obj); updateBackground(); update(); } - public void setObject(Object obj1, Object obj2) { - if(imageTask != null) { - imageTask.cancel(); - imageTask = null; - } - - setObjectImpl(obj1, obj2); - backgroundHandler.post(new Runnable() { - @Override - public void run() { - updateBackground(); - uiHandler.post(new Runnable() { - @Override - public void run() { - update(); - } - }); - } - }); - } - protected void setObjectImpl(Object obj) { - - } - protected void setObjectImpl(Object obj1, Object obj2) { - + public void setObject(T obj1, Object obj2) { + setObject(obj1, null); } + protected abstract void setObjectImpl(T obj); private static synchronized void startUpdater() { if(uiHandler != null) { @@ -310,7 +288,7 @@ public class UpdateView extends LinearLayout { this.view = view; } - public UpdateView getUpdateView() { + public UpdateView getUpdateView() { return updateView; } public View getView() { @@ -324,3 +302,4 @@ public class UpdateView extends LinearLayout { } } } + diff --git a/app/src/main/java/github/daneren2005/dsub/view/UpdateView2.java b/app/src/main/java/github/daneren2005/dsub/view/UpdateView2.java new file mode 100644 index 00000000..0f0b5455 --- /dev/null +++ b/app/src/main/java/github/daneren2005/dsub/view/UpdateView2.java @@ -0,0 +1,47 @@ +package github.daneren2005.dsub.view; + +import android.content.Context; + +public abstract class UpdateView2 extends UpdateView { + protected T2 item2; + + public UpdateView2(Context context) { + super(context); + } + + public UpdateView2(Context context, boolean autoUpdate) { + super(context, autoUpdate); + } + + public final void setObject(T1 obj1) { + setObject(obj1, null); + } + @Override + public void setObject(T1 obj1, Object obj2) { + item = obj1; + item2 = (T2) obj2; + if(imageTask != null) { + imageTask.cancel(); + imageTask = null; + } + + setObjectImpl(item, item2); + backgroundHandler.post(new Runnable() { + @Override + public void run() { + updateBackground(); + uiHandler.post(new Runnable() { + @Override + public void run() { + update(); + } + }); + } + }); + } + + protected final void setObjectImpl(T1 obj1) { + setObjectImpl(obj1, null); + } + protected abstract void setObjectImpl(T1 obj1, T2 obj2); +} diff --git a/app/src/main/java/github/daneren2005/dsub/view/UserView.java b/app/src/main/java/github/daneren2005/dsub/view/UserView.java index dec8dbef..a97d755b 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/UserView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/UserView.java @@ -25,15 +25,12 @@ import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.User; import github.daneren2005.dsub.util.ImageLoader; -public class UserView extends UpdateView { - private User user; - +public class UserView extends UpdateView2 { private TextView usernameView; private ImageView avatarView; public UserView(Context context) { super(context, false); - this.context = context; LayoutInflater.from(context).inflate(R.layout.user_list_item, this, true); usernameView = (TextView) findViewById(R.id.item_name); @@ -46,9 +43,8 @@ public class UserView extends UpdateView { }); } - protected void setObjectImpl(Object obj, Object obj2) { - this.user = (User) obj; + protected void setObjectImpl(User user, ImageLoader imageLoader) { usernameView.setText(user.getUsername()); - imageTask = ((ImageLoader)obj2).loadAvatar(context, avatarView, user.getUsername()); + imageTask = imageLoader.loadAvatar(context, avatarView, user.getUsername()); } } -- cgit v1.2.3