use of com.simplecity.amp_library.model.SuggestedHeader in project Shuttle by timusus.
the class SuggestedFragment method getRecentlyAddedViewModels.
Observable<List<ViewModel>> getRecentlyAddedViewModels() {
return Playlist.recentlyAddedPlaylist.getSongsObservable().flatMap(songs -> Observable.just(Operators.songsToAlbums(songs))).flatMapSingle(source -> Observable.fromIterable(source).sorted((a, b) -> ComparisonUtils.compareLong(b.songPlayCount, a.songPlayCount)).take(20).toList()).map(albums -> {
if (!albums.isEmpty()) {
List<ViewModel> viewModels = new ArrayList<>();
SuggestedHeader recentlyAddedHeader = new SuggestedHeader(getString(R.string.recentlyadded), getString(R.string.suggested_recently_added_subtitle), Playlist.recentlyAddedPlaylist);
SuggestedHeaderView recentlyAddedHeaderView = new SuggestedHeaderView(recentlyAddedHeader);
recentlyAddedHeaderView.setClickListener(this);
viewModels.add(recentlyAddedHeaderView);
viewModels.addAll(Stream.of(albums).map(album -> {
AlbumView albumView = new AlbumView(album, ViewType.ALBUM_CARD, requestManager);
albumView.setClickListener(this);
return albumView;
}).toList());
return viewModels;
} else {
return Collections.emptyList();
}
});
}
use of com.simplecity.amp_library.model.SuggestedHeader in project Shuttle by timusus.
the class SuggestedFragment method getFavoriteSongViewModels.
@SuppressLint("CheckResult")
Observable<List<ViewModel>> getFavoriteSongViewModels() {
Observable<List<Song>> favoritesSongs = DataManager.getInstance().getFavoriteSongsRelay().take(20);
return Observable.combineLatest(favoritesSongs, Playlist.favoritesPlaylist().filter(Optional::isPresent).map(Optional::get).toObservable(), (songs, playlist) -> {
if (!songs.isEmpty()) {
List<ViewModel> viewModels = new ArrayList<>();
SuggestedHeader favoriteHeader = new SuggestedHeader(getString(R.string.fav_title), getString(R.string.suggested_favorite_subtitle), playlist);
SuggestedHeaderView favoriteHeaderView = new SuggestedHeaderView(favoriteHeader);
favoriteHeaderView.setClickListener(SuggestedFragment.this);
viewModels.add(favoriteHeaderView);
viewModels.add(favoriteRecyclerView);
SongClickListener songClickListener = new SongClickListener(songs);
favoriteRecyclerView.viewModelAdapter.setItems(Stream.of(songs).map(song -> {
SuggestedSongView suggestedSongView = new SuggestedSongView(song, requestManager);
suggestedSongView.setClickListener(songClickListener);
return (ViewModel) suggestedSongView;
}).toList());
return viewModels;
} else {
return Collections.emptyList();
}
});
}
use of com.simplecity.amp_library.model.SuggestedHeader in project Shuttle by timusus.
the class SuggestedFragment method refreshAdapterItems.
void refreshAdapterItems() {
PermissionUtils.RequestStoragePermissions(() -> {
if (getActivity() != null && isAdded()) {
subscription = new CompositeSubscription();
Observable<Playlist> mostPlayedPlaylistObservable = Observable.fromCallable(Playlist::mostPlayedPlaylist).subscribeOn(Schedulers.io()).cache();
Observable<List<Song>> mostPlayedSongsObservable = mostPlayedPlaylistObservable.filter(playlist -> playlist != null).flatMap(playlist -> playlist.getSongsObservable(getContext())).cache();
Observable<List<AdaptableItem>> mostPlayedItemsObservable = mostPlayedPlaylistObservable.flatMap(playlist -> {
SuggestedHeader mostPlayedHeader = new SuggestedHeader(getString(R.string.mostplayed), getString(R.string.suggested_most_played_songs_subtitle), playlist);
SuggestedHeaderView mostPlayedHeaderView = new SuggestedHeaderView(mostPlayedHeader);
return mostPlayedSongsObservable.map(songs -> {
List<AdaptableItem> items = new ArrayList<>();
if (!songs.isEmpty()) {
items.add(mostPlayedHeaderView);
items.add(mostPlayedRecyclerView);
}
return items;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable<List<AdaptableItem>> recentlyPlayedAlbums = Observable.fromCallable(Playlist::recentlyPlayedPlaylist).subscribeOn(Schedulers.io()).filter(playlist -> playlist != null).flatMap(playlist -> {
SuggestedHeader recentlyPlayedHeader = new SuggestedHeader(getString(R.string.suggested_recent_title), getString(R.string.suggested_recent_subtitle), playlist);
SuggestedHeaderView recentlyPlayedHeaderView = new SuggestedHeaderView(recentlyPlayedHeader);
return playlist.getSongsObservable(getContext()).flatMap(songs -> Observable.just(Operators.songsToAlbums(songs))).flatMap(Observable::from).sorted((a, b) -> ComparisonUtils.compareLong(b.lastPlayed, a.lastPlayed)).limit(6).flatMap(album -> album.getSongsObservable().map(songs -> {
album.numSongs = songs.size();
return album;
})).sorted((a, b) -> ComparisonUtils.compareLong(b.lastPlayed, a.lastPlayed)).filter(album -> album.numSongs > 0).map(album -> (AdaptableItem) new AlbumView(album, ViewType.ALBUM_LIST_SMALL, requestManager)).toList().map(adaptableItems -> {
if (!adaptableItems.isEmpty()) {
adaptableItems.add(0, recentlyPlayedHeaderView);
}
return adaptableItems;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable<Playlist> favouritesPlaylistObservable = Observable.fromCallable(Playlist::favoritesPlaylist).subscribeOn(Schedulers.io()).cache();
Observable<List<Song>> favouritesSongsObservable = favouritesPlaylistObservable.filter(playlist -> playlist != null).flatMap(playlist -> playlist.getSongsObservable(getContext())).cache();
Observable<List<AdaptableItem>> favoriteSongsItemsObservable = favouritesPlaylistObservable.flatMap(playlist -> {
SuggestedHeader favoriteHeader = new SuggestedHeader(getString(R.string.fav_title), getString(R.string.suggested_favorite_subtitle), playlist);
SuggestedHeaderView favoriteHeaderView = new SuggestedHeaderView(favoriteHeader);
return favouritesSongsObservable.map(songs -> {
List<AdaptableItem> items = new ArrayList<>();
if (!songs.isEmpty()) {
items.add(favoriteHeaderView);
items.add(favoriteRecyclerView);
}
return items;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable<List<AdaptableItem>> recentlyAddedAlbums = Observable.fromCallable(Playlist::recentlyAddedPlaylist).subscribeOn(Schedulers.io()).filter(playlist -> playlist != null).flatMap(playlist -> {
SuggestedHeader recentlyAddedHeader = new SuggestedHeader(getString(R.string.recentlyadded), getString(R.string.suggested_recently_added_subtitle), playlist);
SuggestedHeaderView recentlyAddedHeaderView = new SuggestedHeaderView(recentlyAddedHeader);
return playlist.getSongsObservable(getContext()).flatMap(songs -> Observable.just(Operators.songsToAlbums(songs))).flatMap(Observable::from).sorted((a, b) -> ComparisonUtils.compareLong(b.dateAdded, a.dateAdded)).limit(4).flatMap(album -> album.getSongsObservable().map(songs -> {
album.numSongs = songs.size();
return album;
})).sorted((a, b) -> ComparisonUtils.compareLong(b.dateAdded, a.dateAdded)).filter(album -> album.numSongs > 0).map(album -> (AdaptableItem) new AlbumView(album, ViewType.ALBUM_LIST_SMALL, requestManager)).toList().map(adaptableItems -> {
if (!adaptableItems.isEmpty()) {
adaptableItems.add(0, recentlyAddedHeaderView);
}
return adaptableItems;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable.merge(mostPlayedItemsObservable, recentlyPlayedAlbums, favoriteSongsItemsObservable, recentlyAddedAlbums);
subscription.add(Observable.combineLatest(mostPlayedItemsObservable, recentlyPlayedAlbums, favoriteSongsItemsObservable, recentlyAddedAlbums, (mostPlayedSongs1, recentlyPlayedAlbums1, favoriteSongs1, recentlyAddedAlbums1) -> {
List<AdaptableItem> items = new ArrayList<>();
items.addAll(mostPlayedSongs1);
items.addAll(recentlyPlayedAlbums1);
items.addAll(favoriteSongs1);
items.addAll(recentlyAddedAlbums1);
return items;
}).debounce(250, TimeUnit.MILLISECONDS).switchIfEmpty(Observable.just(new ArrayList<>())).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
if (adaptableItems.isEmpty()) {
suggestedAdapter.setEmpty(new EmptyView(R.string.empty_suggested));
} else {
suggestedAdapter.setItems(adaptableItems);
}
}));
subscription.add(mostPlayedSongsObservable.map(songs -> {
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(b.playCount, a.playCount));
return Stream.of(songs).map(song -> (AdaptableItem) new SuggestedSongView(song, requestManager)).limit(20).collect(Collectors.toList());
}).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
mostPlayedRecyclerView.itemAdapter.setItems(adaptableItems);
}));
subscription.add(favouritesSongsObservable.map(songs -> Stream.of(songs).map(song -> (AdaptableItem) new SuggestedSongView(song, requestManager)).limit(20).collect(Collectors.toList())).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
favoriteRecyclerView.itemAdapter.setItems(adaptableItems);
}));
}
});
}
use of com.simplecity.amp_library.model.SuggestedHeader in project Shuttle by timusus.
the class SuggestedFragment method getMostPlayedViewModels.
Observable<List<ViewModel>> getMostPlayedViewModels() {
return Playlist.mostPlayedPlaylist.getSongsObservable().map(songs -> {
if (!songs.isEmpty()) {
List<ViewModel> viewModels = new ArrayList<>();
SuggestedHeader mostPlayedHeader = new SuggestedHeader(getString(R.string.mostplayed), getString(R.string.suggested_most_played_songs_subtitle), Playlist.mostPlayedPlaylist);
SuggestedHeaderView mostPlayedHeaderView = new SuggestedHeaderView(mostPlayedHeader);
mostPlayedHeaderView.setClickListener(this);
viewModels.add(mostPlayedHeaderView);
viewModels.add(mostPlayedRecyclerView);
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(b.playCount, a.playCount));
SongClickListener songClickListener = new SongClickListener(songs);
mostPlayedRecyclerView.viewModelAdapter.setItems(Stream.of(songs).map(song -> {
SuggestedSongView suggestedSongView = new SuggestedSongView(song, requestManager);
suggestedSongView.setClickListener(songClickListener);
return (ViewModel) suggestedSongView;
}).limit(20).toList());
return viewModels;
} else {
return Collections.emptyList();
}
});
}
use of com.simplecity.amp_library.model.SuggestedHeader in project Shuttle by timusus.
the class SuggestedFragment method getRecentlyPlayedViewModels.
Observable<List<ViewModel>> getRecentlyPlayedViewModels() {
return Playlist.recentlyPlayedPlaylist.getSongsObservable().flatMap(songs -> Observable.just(Operators.songsToAlbums(songs))).flatMapSingle(albums -> Observable.fromIterable(albums).sorted((a, b) -> ComparisonUtils.compareLong(b.lastPlayed, a.lastPlayed)).flatMapSingle(album -> album.getSongsSingle().map(songs -> {
album.numSongs = songs.size();
return album;
}).filter(a -> a.numSongs > 0).toSingle()).sorted((a, b) -> ComparisonUtils.compareLong(b.lastPlayed, a.lastPlayed)).take(6).toList()).map(albums -> {
if (!albums.isEmpty()) {
List<ViewModel> viewModels = new ArrayList<>();
SuggestedHeader recentlyPlayedHeader = new SuggestedHeader(getString(R.string.suggested_recent_title), getString(R.string.suggested_recent_subtitle), Playlist.recentlyPlayedPlaylist);
SuggestedHeaderView recentlyPlayedHeaderView = new SuggestedHeaderView(recentlyPlayedHeader);
recentlyPlayedHeaderView.setClickListener(this);
viewModels.add(recentlyPlayedHeaderView);
viewModels.addAll(Stream.of(albums).map(album -> {
AlbumView albumView = new AlbumView(album, ViewType.ALBUM_LIST_SMALL, requestManager);
albumView.setClickListener(this);
return albumView;
}).toList());
return viewModels;
} else {
return Collections.emptyList();
}
});
}
Aggregations