Search in sources :

Example 1 with Optional

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();
        }
    });
}
Also used : Optional(com.annimon.stream.Optional) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) ArrayList(java.util.ArrayList) SuggestedSongView(com.simplecity.amp_library.ui.modelviews.SuggestedSongView) List(java.util.List) ArrayList(java.util.ArrayList) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader) SuppressLint(android.annotation.SuppressLint)

Example 2 with Optional

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;
    }
}
Also used : Optional(com.annimon.stream.Optional) Intent(android.content.Intent)

Example 3 with Optional

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));
}
Also used : ContentValues(android.content.ContentValues) Optional(com.annimon.stream.Optional) List(java.util.List) ArrayList(java.util.ArrayList) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint)

Aggregations

Optional (com.annimon.stream.Optional)3 SuppressLint (android.annotation.SuppressLint)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ContentValues (android.content.ContentValues)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 SuggestedHeader (com.simplecity.amp_library.model.SuggestedHeader)1 SuggestedHeaderView (com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView)1 SuggestedSongView (com.simplecity.amp_library.ui.modelviews.SuggestedSongView)1 ViewModel (com.simplecityapps.recycler_adapter.model.ViewModel)1