use of com.annimon.stream.Optional 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.annimon.stream.Optional in project Shuttle by timusus.
the class ShortcutTrampolineActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String action = getIntent().getAction();
switch(action) {
case MusicService.ShortcutCommands.PLAY:
case MusicService.ShortcutCommands.SHUFFLE_ALL:
Intent intent = new Intent(this, MusicService.class);
intent.setAction(action);
startService(intent);
finish();
break;
case MusicService.ShortcutCommands.FOLDERS:
intent = new Intent(this, MainActivity.class);
intent.setAction(action);
startActivity(intent);
finish();
break;
case MusicService.ShortcutCommands.PLAYLIST:
intent = new Intent(this, MainActivity.class);
intent.setAction(action);
Playlist.favoritesPlaylist().filter(Optional::isPresent).map(Optional::get).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(playlist -> {
intent.putExtra(PlaylistUtils.ARG_PLAYLIST, playlist);
startActivity(intent);
finish();
}, error -> LogUtils.logException(TAG, "Error starting activity", error));
break;
}
}
use of com.annimon.stream.Optional in project Shuttle by timusus.
the class PlaylistUtils method addToFavorites.
/**
* Add a song to the favourites playlist
*/
@SuppressLint("CheckResult")
public static void addToFavorites(@NonNull Song song, UnsafeConsumer<Boolean> success) {
Single.zip(Playlist.favoritesPlaylist().filter(Optional::isPresent).map(Optional::get).toSingle(), DataManager.getInstance().getFavoriteSongsRelay().first(Collections.emptyList()).map(List::size), Pair::new).map(pair -> {
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", pair.first.id);
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, song.id);
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, pair.second + 1);
Uri newUri = ShuttleApplication.getInstance().getContentResolver().insert(uri, values);
ShuttleApplication.getInstance().getContentResolver().notifyChange(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, null);
return newUri != null;
}).delay(150, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(success, throwable -> LogUtils.logException(TAG, "Error adding to playlist", throwable));
}
Aggregations