Search in sources :

Example 36 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project Shuttle by timusus.

the class PlayerFragment method onResume.

@Override
public void onResume() {
    super.onResume();
    subscriptions = new CompositeSubscription();
    Observable<SeekBarChangeEvent> sharedSeekBarEvents = RxSeekBar.changeEvents(seekBar).onBackpressureLatest().ofType(SeekBarChangeEvent.class).observeOn(AndroidSchedulers.mainThread()).share();
    subscriptions.add(sharedSeekBarEvents.subscribe(seekBarChangeEvent -> {
        if (seekBarChangeEvent instanceof SeekBarStartChangeEvent) {
            isSeeking = true;
        } else if (seekBarChangeEvent instanceof SeekBarStopChangeEvent) {
            isSeeking = false;
        }
    }));
    subscriptions.add(sharedSeekBarEvents.ofType(SeekBarProgressChangeEvent.class).filter(SeekBarProgressChangeEvent::fromUser).debounce(15, TimeUnit.MILLISECONDS).subscribe(seekBarChangeEvent -> presenter.seekTo(seekBarChangeEvent.progress())));
}
Also used : R(com.simplecity.amp_library.R) ImageButton(android.widget.ImageButton) Bundle(android.os.Bundle) SizableSeekBar(com.simplecity.amp_library.ui.views.SizableSeekBar) DrawableUtils(com.simplecity.amp_library.utils.DrawableUtils) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) SeekBarStartChangeEvent(com.jakewharton.rxbinding.widget.SeekBarStartChangeEvent) Animator(android.animation.Animator) StringUtils(com.simplecity.amp_library.utils.StringUtils) Observable(rx.Observable) ColorStateList(android.content.res.ColorStateList) Song(com.simplecity.amp_library.model.Song) ThemeUtils(com.simplecity.amp_library.utils.ThemeUtils) ColorUtils(com.simplecity.amp_library.utils.ColorUtils) RepeatingImageButton(com.simplecity.amp_library.ui.views.RepeatingImageButton) AnimatorSet(android.animation.AnimatorSet) View(android.view.View) PreferenceManager(android.preference.PreferenceManager) SeekBarChangeEvent(com.jakewharton.rxbinding.widget.SeekBarChangeEvent) SeekBarProgressChangeEvent(com.jakewharton.rxbinding.widget.SeekBarProgressChangeEvent) FloatingActionButton(android.support.design.widget.FloatingActionButton) LayoutInflater(android.view.LayoutInflater) MainActivity(com.simplecity.amp_library.ui.activities.MainActivity) ObjectAnimator(android.animation.ObjectAnimator) Fragment(android.support.v4.app.Fragment) PlayPauseView(com.simplecity.amp_library.ui.views.PlayPauseView) TextUtils(android.text.TextUtils) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) ViewGroup(android.view.ViewGroup) RxSeekBar(com.jakewharton.rxbinding.widget.RxSeekBar) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) CompositeSubscription(rx.subscriptions.CompositeSubscription) PlayerView(com.simplecity.amp_library.ui.views.PlayerView) TextView(android.widget.TextView) SharedPreferences(android.content.SharedPreferences) SeekBarStopChangeEvent(com.jakewharton.rxbinding.widget.SeekBarStopChangeEvent) MusicService(com.simplecity.amp_library.playback.MusicService) PlayerPresenter(com.simplecity.amp_library.ui.presenters.PlayerPresenter) Nullable(android.support.annotation.Nullable) FragmentTransaction(android.support.v4.app.FragmentTransaction) SeekBarStopChangeEvent(com.jakewharton.rxbinding.widget.SeekBarStopChangeEvent) CompositeSubscription(rx.subscriptions.CompositeSubscription) SeekBarStartChangeEvent(com.jakewharton.rxbinding.widget.SeekBarStartChangeEvent) SeekBarChangeEvent(com.jakewharton.rxbinding.widget.SeekBarChangeEvent)

Example 37 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription 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);
            }));
        }
    });
}
Also used : R(com.simplecity.amp_library.R) GridLayoutManager(android.support.v7.widget.GridLayoutManager) Bundle(android.os.Bundle) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) SuggestedDividerDecoration(com.simplecity.amp_library.ui.views.SuggestedDividerDecoration) ItemAdapter(com.simplecity.amp_library.ui.adapters.ItemAdapter) Song(com.simplecity.amp_library.model.Song) RequestManager(com.bumptech.glide.RequestManager) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) Schedulers(rx.schedulers.Schedulers) View(android.view.View) PreferenceManager(android.preference.PreferenceManager) Playlist(com.simplecity.amp_library.model.Playlist) IntentFilter(android.content.IntentFilter) PopupMenu(android.support.v7.widget.PopupMenu) BroadcastReceiver(android.content.BroadcastReceiver) AppCompatActivity(android.support.v7.app.AppCompatActivity) ViewGroup(android.view.ViewGroup) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) Serializable(java.io.Serializable) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) List(java.util.List) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) AdaptableItem(com.simplecity.amp_library.model.AdaptableItem) Context(android.content.Context) SuggestedSongView(com.simplecity.amp_library.ui.modelviews.SuggestedSongView) Album(com.simplecity.amp_library.model.Album) Stream(com.annimon.stream.Stream) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) Intent(android.content.Intent) ArrayList(java.util.ArrayList) Observable(rx.Observable) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader) ThemeUtils(com.simplecity.amp_library.utils.ThemeUtils) SuggestedAdapter(com.simplecity.amp_library.ui.adapters.SuggestedAdapter) Toast(android.widget.Toast) ViewType(com.simplecity.amp_library.ui.modelviews.ViewType) Collectors(com.annimon.stream.Collectors) LayoutInflater(android.view.LayoutInflater) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) TimeUnit(java.util.concurrent.TimeUnit) CompositeSubscription(rx.subscriptions.CompositeSubscription) RecyclerView(android.support.v7.widget.RecyclerView) BlacklistHelper(com.simplecity.amp_library.sql.databases.BlacklistHelper) Glide(com.bumptech.glide.Glide) SharedPreferences(android.content.SharedPreferences) HorizontalRecyclerView(com.simplecity.amp_library.ui.modelviews.HorizontalRecyclerView) ComparisonUtils(com.simplecity.amp_library.utils.ComparisonUtils) AlbumArtist(com.simplecity.amp_library.model.AlbumArtist) Operators(com.simplecity.amp_library.utils.Operators) Collections(java.util.Collections) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) AdaptableItem(com.simplecity.amp_library.model.AdaptableItem) SuggestedSongView(com.simplecity.amp_library.ui.modelviews.SuggestedSongView) Playlist(com.simplecity.amp_library.model.Playlist) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) CompositeSubscription(rx.subscriptions.CompositeSubscription) List(java.util.List) ArrayList(java.util.ArrayList) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView)

Example 38 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project sqlbrite by square.

the class ItemsFragment method onResume.

@Override
public void onResume() {
    super.onResume();
    String listId = String.valueOf(getListId());
    subscriptions = new CompositeSubscription();
    Observable<Integer> itemCount = //
    db.createQuery(TodoItem.TABLE, COUNT_QUERY, listId).map(new Func1<Query, Integer>() {

        @Override
        public Integer call(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getInt(0);
            } finally {
                cursor.close();
            }
        }
    });
    Observable<String> listName = db.createQuery(TodoList.TABLE, TITLE_QUERY, listId).map(new Func1<Query, String>() {

        @Override
        public String call(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getString(0);
            } finally {
                cursor.close();
            }
        }
    });
    subscriptions.add(Observable.combineLatest(listName, itemCount, new Func2<String, Integer, String>() {

        @Override
        public String call(String listName, Integer itemCount) {
            return listName + " (" + itemCount + ")";
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String title) {
            getActivity().setTitle(title);
        }
    }));
    subscriptions.add(db.createQuery(TodoItem.TABLE, LIST_QUERY, listId).mapToList(TodoItem.MAPPER).observeOn(AndroidSchedulers.mainThread()).subscribe(adapter));
}
Also used : Action1(rx.functions.Action1) Query(com.squareup.sqlbrite.SqlBrite.Query) Cursor(android.database.Cursor) CompositeSubscription(rx.subscriptions.CompositeSubscription) Func2(rx.functions.Func2)

Example 39 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project bilibili-android-client by HotBitmapGG.

the class BannerView method startScroll.

/**
   * 图片开始轮播
   */
private void startScroll() {
    compositeSubscription = new CompositeSubscription();
    isStopScroll = false;
    Subscription subscription = Observable.timer(delayTime, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(aLong -> {
        if (isStopScroll) {
            return;
        }
        isStopScroll = true;
        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
    });
    compositeSubscription.add(subscription);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription)

Example 40 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project SeeWeather by xcc3641.

the class AutoUpdateService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    mSharedPreferenceUtil = SharedPreferenceUtil.getInstance();
    mCompositeSubscription = new CompositeSubscription();
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription)

Aggregations

CompositeSubscription (rx.subscriptions.CompositeSubscription)49 ArrayList (java.util.ArrayList)9 View (android.view.View)7 Subscription (rx.Subscription)7 Context (android.content.Context)6 Bundle (android.os.Bundle)6 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)6 Intent (android.content.Intent)5 AppCompatActivity (android.support.v7.app.AppCompatActivity)5 RecyclerView (android.support.v7.widget.RecyclerView)5 ViewGroup (android.view.ViewGroup)5 List (java.util.List)5 Schedulers (rx.schedulers.Schedulers)5 MenuItem (android.view.MenuItem)4 Issue (com.kboyarshinov.realmrxjavaexample.model.Issue)4 ActionBar (android.support.v7.app.ActionBar)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3 Menu (android.view.Menu)3 TextView (android.widget.TextView)3 TwitterStringUtils (com.github.moko256.twicalico.text.TwitterStringUtils)3