use of io.reactivex.Observable in project Shuttle by timusus.
the class PlaylistFragment method refreshAdapterItems.
private void refreshAdapterItems() {
PermissionUtils.RequestStoragePermissions(() -> {
if (getActivity() != null && isAdded()) {
Observable<List<Playlist>> defaultPlaylistsObservable = Observable.defer(() -> {
List<Playlist> playlists = new ArrayList<>();
Playlist podcastPlaylist = Playlist.podcastPlaylist();
if (podcastPlaylist != null) {
playlists.add(podcastPlaylist);
}
playlists.add(Playlist.recentlyAddedPlaylist);
playlists.add(Playlist.mostPlayedPlaylist);
return Observable.just(playlists);
});
Observable<List<Playlist>> playlistsObservable = DataManager.getInstance().getPlaylistsRelay();
disposable = Observable.combineLatest(defaultPlaylistsObservable, playlistsObservable, (defaultPlaylists, playlists) -> {
List<Playlist> list = new ArrayList<>();
list.addAll(defaultPlaylists);
list.addAll(playlists);
return list;
}).debounce(150, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).map(playlists -> {
PlaylistView.OnClickListener listener = new PlaylistView.OnClickListener() {
@Override
public void onPlaylistClick(int position, PlaylistView playlistView) {
if (playlistClickListener != null) {
playlistClickListener.onPlaylistClicked(playlistView.playlist);
}
}
@Override
public void onPlaylistOverflowClick(int position, View v, Playlist playlist) {
PopupMenu menu = new PopupMenu(PlaylistFragment.this.getActivity(), v);
MenuUtils.setupPlaylistMenu(menu, playlist);
menu.setOnMenuItemClickListener(MenuUtils.getPlaylistPopupMenuClickListener(getContext(), playlist, null));
menu.show();
}
};
return Stream.of(playlists).sorted((a, b) -> ComparisonUtils.compare(a.name, b.name)).sorted((a, b) -> ComparisonUtils.compareInt(a.type, b.type)).map(playlist -> {
PlaylistView playlistView = new PlaylistView(playlist);
playlistView.setListener(listener);
return (ViewModel) playlistView;
}).toList();
}).observeOn(AndroidSchedulers.mainThread()).subscribe(items -> {
if (items.isEmpty()) {
adapter.setItems(Collections.singletonList(new EmptyView(R.string.empty_playlist)));
} else {
adapter.setItems(items);
}
}, error -> LogUtils.logException(TAG, "Error refreshing adapter", error));
}
});
}
use of io.reactivex.Observable 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 io.reactivex.Observable in project Shuttle by timusus.
the class VoiceSearchActivity method searchAndPlaySongs.
private void searchAndPlaySongs() {
DataManager.getInstance().getAlbumArtistsRelay().first(Collections.emptyList()).flatMapObservable(Observable::fromIterable).filter(albumArtist -> albumArtist.name.toLowerCase(Locale.getDefault()).contains(filterString.toLowerCase())).flatMapSingle(AlbumArtist::getSongsSingle).map(songs -> {
Collections.sort(songs, (a, b) -> a.getAlbumArtist().compareTo(b.getAlbumArtist()));
Collections.sort(songs, (a, b) -> a.getAlbum().compareTo(b.getAlbum()));
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.track, b.track));
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.discNumber, b.discNumber));
return songs;
});
// Search for album-artists, albums & songs matching our filter. Then, create an Observable emitting List<Song> for each type of result.
// Then we concat the results, and return the first one which is non-empty. Order is important here, we want album-artist first, if it's
// available, then albums, then songs.
Observable.concat(// If we have an album artist matching our query, then play the songs by that album artist
DataManager.getInstance().getAlbumArtistsRelay().first(Collections.emptyList()).flatMapObservable(Observable::fromIterable).filter(albumArtist -> albumArtist.name.toLowerCase(Locale.getDefault()).contains(filterString.toLowerCase())).flatMapSingle(AlbumArtist::getSongsSingle).map(songs -> {
Collections.sort(songs, (a, b) -> a.getAlbumArtist().compareTo(b.getAlbumArtist()));
Collections.sort(songs, (a, b) -> a.getAlbum().compareTo(b.getAlbum()));
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.track, b.track));
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.discNumber, b.discNumber));
return songs;
}), // If we have an album matching our query, then play the songs from that album
DataManager.getInstance().getAlbumsRelay().first(Collections.emptyList()).flatMapObservable(Observable::fromIterable).filter(album -> containsIgnoreCase(album.name, filterString) || containsIgnoreCase(album.name, filterString) || (Stream.of(album.artists).anyMatch(artist -> containsIgnoreCase(artist.name, filterString))) || containsIgnoreCase(album.albumArtistName, filterString)).flatMapSingle(Album::getSongsSingle).map(songs -> {
Collections.sort(songs, (a, b) -> a.getAlbum().compareTo(b.getAlbum()));
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.track, b.track));
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.discNumber, b.discNumber));
return songs;
}), // If have a song, play that song, as well as others from the same album.
DataManager.getInstance().getSongsRelay().first(Collections.emptyList()).flatMapObservable(Observable::fromIterable).filter(song -> containsIgnoreCase(song.name, filterString) || containsIgnoreCase(song.albumName, filterString) || containsIgnoreCase(song.artistName, filterString) || containsIgnoreCase(song.albumArtistName, filterString)).flatMapSingle(song -> song.getAlbum().getSongsSingle().map(songs -> {
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.track, b.track));
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(a.discNumber, b.discNumber));
position = songs.indexOf(song);
return songs;
}))).filter(songs -> !songs.isEmpty()).firstOrError().observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> {
if (songs != null) {
MusicUtils.playAll(songs, position, true, (String message) -> Toast.makeText(this, message, Toast.LENGTH_SHORT).show());
startActivity(new Intent(this, MainActivity.class));
}
finish();
}, error -> {
LogUtils.logException(TAG, "Error attempting to playAll()", error);
startActivity(new Intent(this, MainActivity.class));
finish();
});
}
use of io.reactivex.Observable in project Shuttle by timusus.
the class ArtworkDownloadService method onCreate.
@Override
public void onCreate() {
super.onCreate();
notificationHelper = new NotificationHelper(this);
if (!ShuttleUtils.isOnline(false)) {
Toast toast = Toast.makeText(this, getResources().getString(R.string.connection_unavailable), Toast.LENGTH_SHORT);
toast.show();
stopSelf();
return;
}
notificationHelper.notify(NOTIFICATION_ID, getNotificationBuilder().build());
Single<List<ArtworkProvider>> sharedItemsSingle = DataManager.getInstance().getAlbumArtistsRelay().first(Collections.emptyList()).<ArtworkProvider>flatMapObservable(Observable::fromIterable).mergeWith(DataManager.getInstance().getAlbumsRelay().first(Collections.emptyList()).flatMapObservable(Observable::fromIterable)).toList();
disposables.add(sharedItemsSingle.observeOn(AndroidSchedulers.mainThread()).subscribe(list -> {
max = list.size();
updateProgress();
}, error -> LogUtils.logException(TAG, "Error determining max", error)));
disposables.add(sharedItemsSingle.flatMapObservable(Observable::fromIterable).flatMap(artworkProvider -> Observable.just(artworkProvider).map(artwork -> {
FutureTarget<File> futureTarget = Glide.with(ArtworkDownloadService.this).using(new ArtworkModelLoader(true), InputStream.class).load(artwork).as(InputStream.class).downloadOnly(SimpleTarget.SIZE_ORIGINAL, SimpleTarget.SIZE_ORIGINAL);
try {
futureTarget.get(30, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
Log.e(TAG, "Error downloading artworkProvider: " + e);
}
Glide.clear(futureTarget);
return artwork;
})).subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread()).subscribe(item -> updateProgress(), error -> LogUtils.logException(TAG, "Error downloading artwork", error)));
}
use of io.reactivex.Observable in project Offer by woshiyizhijiao.
the class RxJavaActivity method zip2.
/**
* 上游两个管道的其中一个在无线发送事件,而另一个管道只发送一个,最终导致oom
*/
public void zip2(View v) {
Observable observable1 = Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
for (int x = 0; ; x++) {
emitter.onNext(1);
}
}
}).subscribeOn(Schedulers.io());
Observable<String> observable2 = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("第二个管道");
}
}).subscribeOn(Schedulers.io());
Observable.zip(observable1, observable2, new BiFunction<Integer, String, String>() {
/**
* Calculate a value based on the input values.
*
* @param integer the first value
* @param s the second value
* @return the result value
* @throws Exception on error
*/
@Override
public String apply(Integer integer, String s) throws Exception {
return integer + s;
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Object value) {
LogUtils.e("onNext" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
Aggregations