Search in sources :

Example 1 with AlbumView

use of com.simplecity.amp_library.ui.modelviews.AlbumView in project Shuttle by timusus.

the class SearchActivity method refreshAdapterItems.

private void refreshAdapterItems() {
    subscriptions.add(DataManager.getInstance().getSongsRelay().first().map(songs -> {
        char[] prefix = filterString.toUpperCase().toCharArray();
        List<Album> albums = Operators.songsToAlbums(songs);
        Collections.sort(albums, Album::compareTo);
        List<AlbumArtist> albumArtists = Operators.albumsToAlbumArtists(albums);
        Collections.sort(albumArtists, AlbumArtist::compareTo);
        List<AdaptableItem> adaptableItems = Stream.of(albumArtists).filter(album -> album.name != null).map(albumArtist -> new SearchUtils.JaroWinklerObject<>(albumArtist, filterString, albumArtist.name)).filter(jaroWinklerObject -> jaroWinklerObject.score > SCORE_THRESHOLD || TextUtils.isEmpty(filterString)).sorted((a, b) -> a.object.compareTo(b.object)).sorted((a, b) -> Double.compare(b.score, a.score)).map(jaroWinklerObject -> jaroWinklerObject.object).map(albumArtist -> {
            AlbumArtistView albumArtistView = new AlbumArtistView(albumArtist, ViewType.ARTIST_LIST, requestManager);
            albumArtistView.setPrefix(prefixHighlighter, prefix);
            return (AdaptableItem) albumArtistView;
        }).collect(Collectors.toList());
        if (!adaptableItems.isEmpty()) {
            adaptableItems.add(0, artistsHeader);
        }
        List<AdaptableItem> albumItems = Stream.of(albums).filter(album -> album.name != null).map(album -> new SearchUtils.JaroWinklerObject<>(album, filterString, album.name, album.albumArtistName)).filter(jaroWinklerObject -> jaroWinklerObject.score > SCORE_THRESHOLD || TextUtils.isEmpty(filterString)).sorted((a, b) -> a.object.compareTo(b.object)).sorted((a, b) -> Double.compare(b.score, a.score)).map(jaroWinklerObject -> jaroWinklerObject.object).map(album -> {
            AlbumView albumView = new AlbumView(album, ViewType.ALBUM_LIST, requestManager);
            albumView.setPrefix(prefixHighlighter, prefix);
            return albumView;
        }).collect(Collectors.toList());
        if (!albumItems.isEmpty()) {
            albumItems.add(0, albumsHeader);
        }
        adaptableItems.addAll(albumItems);
        songs = Stream.of(songs).filter(song -> song.name != null).map(song -> new SearchUtils.JaroWinklerObject<>(song, filterString, song.name, song.albumName, song.artistName, song.albumArtistName)).filter(jaroWinklerObject -> jaroWinklerObject.score > SCORE_THRESHOLD || TextUtils.isEmpty(filterString)).sorted((a, b) -> a.object.compareTo(b.object)).sorted((a, b) -> Double.compare(b.score, a.score)).map(jaroWinklerObject -> jaroWinklerObject.object).collect(Collectors.toList());
        List<AdaptableItem> songItems = Stream.of(songs).map(song -> {
            SongView songView = new SongView(song, dummySelector, requestManager);
            songView.setPrefix(prefixHighlighter, prefix);
            return songView;
        }).collect(Collectors.toList());
        if (!songItems.isEmpty()) {
            songItems.add(0, songsHeader);
        }
        adaptableItems.addAll(songItems);
        return adaptableItems;
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
        if (setItemsSubscription != null) {
            setItemsSubscription.unsubscribe();
        }
        setItemsSubscription = adapter.setItems(adaptableItems);
        recyclerView.scrollToPosition(0);
    }));
}
Also used : R(com.simplecity.amp_library.R) Bundle(android.os.Bundle) SongView(com.simplecity.amp_library.ui.modelviews.SongView) SearchView(android.support.v7.widget.SearchView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) WindowManager(android.view.WindowManager) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Song(com.simplecity.amp_library.model.Song) RequestManager(com.bumptech.glide.RequestManager) Schedulers(rx.schedulers.Schedulers) View(android.view.View) SearchAdapter(com.simplecity.amp_library.ui.adapters.SearchAdapter) Header(com.simplecity.amp_library.model.Header) Playlist(com.simplecity.amp_library.model.Playlist) MenuItemCompat(android.support.v4.view.MenuItemCompat) SearchableInfo(android.app.SearchableInfo) PopupMenu(android.support.v7.widget.PopupMenu) SubMenu(android.view.SubMenu) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) Serializable(java.io.Serializable) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) List(java.util.List) AdaptableItem(com.simplecity.amp_library.model.AdaptableItem) SearchManager(android.app.SearchManager) Subscription(rx.Subscription) Context(android.content.Context) Album(com.simplecity.amp_library.model.Album) Stream(com.annimon.stream.Stream) Intent(android.content.Intent) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Observable(rx.Observable) AlbumArtistView(com.simplecity.amp_library.ui.modelviews.AlbumArtistView) SuppressLint(android.annotation.SuppressLint) PrefixHighlighter(com.simplecity.amp_library.format.PrefixHighlighter) ThemeUtils(com.simplecity.amp_library.utils.ThemeUtils) ColorUtils(com.simplecity.amp_library.utils.ColorUtils) Toast(android.widget.Toast) Menu(android.view.Menu) SystemBarTintManager(com.readystatesoftware.systembartint.SystemBarTintManager) ActionBar(android.support.v7.app.ActionBar) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) TaggerDialog(com.simplecity.amp_library.tagger.TaggerDialog) ViewType(com.simplecity.amp_library.ui.modelviews.ViewType) Collectors(com.annimon.stream.Collectors) SearchUtils(com.simplecity.amp_library.utils.SearchUtils) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) TextUtils(android.text.TextUtils) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) FLAG_TRANSLUCENT_STATUS(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) DialogUtils(com.simplecity.amp_library.utils.DialogUtils) TimeUnit(java.util.concurrent.TimeUnit) CompositeSubscription(rx.subscriptions.CompositeSubscription) RecyclerView(android.support.v7.widget.RecyclerView) Glide(com.bumptech.glide.Glide) Toolbar(android.support.v7.widget.Toolbar) MultiSelector(com.bignerdranch.android.multiselector.MultiSelector) DataManager(com.simplecity.amp_library.utils.DataManager) RxSearchView(com.jakewharton.rxbinding.support.v7.widget.RxSearchView) AlbumArtist(com.simplecity.amp_library.model.AlbumArtist) SearchHeaderView(com.simplecity.amp_library.ui.modelviews.SearchHeaderView) Operators(com.simplecity.amp_library.utils.Operators) Activity(android.app.Activity) Collections(java.util.Collections) SongView(com.simplecity.amp_library.ui.modelviews.SongView) AlbumArtistView(com.simplecity.amp_library.ui.modelviews.AlbumArtistView) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) AdaptableItem(com.simplecity.amp_library.model.AdaptableItem) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with AlbumView

use of com.simplecity.amp_library.ui.modelviews.AlbumView in project Shuttle by timusus.

the class AlbumFragment method refreshAdapterItems.

void refreshAdapterItems(boolean force) {
    PermissionUtils.RequestStoragePermissions(() -> {
        if (getActivity() != null && isAdded()) {
            int albumDisplayType = SettingsManager.getInstance().getAlbumDisplayType();
            boolean ascending = SortManager.getInstance().getAlbumsAscending();
            subscription = DataManager.getInstance().getAlbumsRelay().skipWhile(albums -> !force && Stream.of(adapter.items).filter(viewModel -> viewModel instanceof AlbumView).count() == albums.size()).debounce(150, TimeUnit.MILLISECONDS).flatMapSingle(albums -> {
                // Sort
                SortManager.getInstance().sortAlbums(albums);
                // Reverse if required
                if (!ascending) {
                    Collections.reverse(albums);
                }
                return Observable.fromIterable(albums).map(album -> {
                    // Look for an existing AlbumView wrapping the album, we'll reuse it if it exists.
                    AlbumView albumView = (AlbumView) Stream.of(adapter.items).filter(viewModel -> viewModel instanceof AlbumView && (((AlbumView) viewModel).album.equals(album))).findFirst().orElse(null);
                    if (albumView == null) {
                        albumView = new AlbumView(album, albumDisplayType, requestManager);
                        albumView.setClickListener(this);
                    }
                    return (ViewModel) albumView;
                }).toList();
            }).observeOn(AndroidSchedulers.mainThread()).subscribe(items -> {
                if (items.isEmpty()) {
                    adapter.setItems(Collections.singletonList(new EmptyView(R.string.empty_albums)));
                } else {
                    items.add(0, shuffleView);
                    adapter.setItems(items);
                }
                // Move the RV back to the top if we've had a sort order change.
                if (sortOrderChanged) {
                    recyclerView.scrollToPosition(0);
                }
                sortOrderChanged = false;
            }, error -> LogUtils.logException(TAG, "Error refreshing adapter items", error));
        }
    });
}
Also used : R(com.simplecity.amp_library.R) GridLayoutManager(android.support.v7.widget.GridLayoutManager) Bundle(android.os.Bundle) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RequestManager(com.bumptech.glide.RequestManager) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) View(android.view.View) SectionedAdapter(com.simplecity.amp_library.ui.adapters.SectionedAdapter) LogUtils(com.simplecity.amp_library.utils.LogUtils) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) SortManager(com.simplecity.amp_library.utils.SortManager) Fragment(android.support.v4.app.Fragment) PopupMenu(android.support.v7.widget.PopupMenu) SubMenu(android.view.SubMenu) ViewGroup(android.view.ViewGroup) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) Disposable(io.reactivex.disposables.Disposable) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) Nullable(android.support.annotation.Nullable) SpanSizeLookup(com.simplecityapps.recycler_adapter.recyclerview.SpanSizeLookup) Context(android.content.Context) Album(com.simplecity.amp_library.model.Album) Stream(com.annimon.stream.Stream) ShuffleView(com.simplecity.amp_library.ui.modelviews.ShuffleView) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) ViewType(com.simplecity.amp_library.ui.adapters.ViewType) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) MenuItem(android.view.MenuItem) Inject(javax.inject.Inject) SuppressLint(android.annotation.SuppressLint) MenuInflater(android.view.MenuInflater) Toast(android.widget.Toast) Menu(android.view.Menu) Observable(io.reactivex.Observable) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) LayoutInflater(android.view.LayoutInflater) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) TimeUnit(java.util.concurrent.TimeUnit) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) MusicService(com.simplecity.amp_library.playback.MusicService) GridDividerDecoration(com.simplecity.amp_library.ui.recyclerview.GridDividerDecoration) DataManager(com.simplecity.amp_library.utils.DataManager) Operators(com.simplecity.amp_library.utils.Operators) Collections(java.util.Collections) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) SuppressLint(android.annotation.SuppressLint)

Example 3 with AlbumView

use of com.simplecity.amp_library.ui.modelviews.AlbumView 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();
        }
    });
}
Also used : R(com.simplecity.amp_library.R) GridLayoutManager(android.support.v7.widget.GridLayoutManager) Bundle(android.os.Bundle) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SuggestedDividerDecoration(com.simplecity.amp_library.ui.views.SuggestedDividerDecoration) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Song(com.simplecity.amp_library.model.Song) Optional(com.annimon.stream.Optional) RequestManager(com.bumptech.glide.RequestManager) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) View(android.view.View) LogUtils(com.simplecity.amp_library.utils.LogUtils) Playlist(com.simplecity.amp_library.model.Playlist) PopupMenu(android.support.v7.widget.PopupMenu) ViewGroup(android.view.ViewGroup) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) Nullable(android.support.annotation.Nullable) Context(android.content.Context) SuggestedSongView(com.simplecity.amp_library.ui.modelviews.SuggestedSongView) Album(com.simplecity.amp_library.model.Album) Stream(com.annimon.stream.Stream) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) ViewType(com.simplecity.amp_library.ui.adapters.ViewType) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) SuppressLint(android.annotation.SuppressLint) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader) Toast(android.widget.Toast) Observable(io.reactivex.Observable) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) LayoutInflater(android.view.LayoutInflater) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) HorizontalRecyclerView(com.simplecity.amp_library.ui.modelviews.HorizontalRecyclerView) PlaylistDetailFragment(com.simplecity.amp_library.ui.detail.PlaylistDetailFragment) ComparisonUtils(com.simplecity.amp_library.utils.ComparisonUtils) DataManager(com.simplecity.amp_library.utils.DataManager) AlbumArtist(com.simplecity.amp_library.model.AlbumArtist) Operators(com.simplecity.amp_library.utils.Operators) Collections(java.util.Collections) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) ArrayList(java.util.ArrayList) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader)

Example 4 with AlbumView

use of com.simplecity.amp_library.ui.modelviews.AlbumView in project Shuttle by timusus.

the class SuggestedFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (recyclerView == null) {
        recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_suggested, container, false);
        recyclerView.addItemDecoration(new SuggestedDividerDecoration(getResources()));
        recyclerView.setRecyclerListener(new RecyclerListener());
        int spanCount = ShuttleUtils.isTablet() ? 12 : 6;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), spanCount);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                if (!adapter.items.isEmpty() && position >= 0) {
                    ViewModel item = adapter.items.get(position);
                    if (item instanceof HorizontalRecyclerView || item instanceof SuggestedHeaderView || (item instanceof AlbumView && item.getViewType() == ViewType.ALBUM_LIST) || (item instanceof AlbumView && item.getViewType() == ViewType.ALBUM_LIST_SMALL) || item instanceof EmptyView) {
                        return spanCount;
                    }
                    if (item instanceof AlbumView && item.getViewType() == ViewType.ALBUM_CARD_LARGE) {
                        return 3;
                    }
                }
                return 2;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
    }
    if (recyclerView.getAdapter() != adapter) {
        recyclerView.setAdapter(adapter);
    }
    return recyclerView;
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SuggestedDividerDecoration(com.simplecity.amp_library.ui.views.SuggestedDividerDecoration) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) SuppressLint(android.annotation.SuppressLint) HorizontalRecyclerView(com.simplecity.amp_library.ui.modelviews.HorizontalRecyclerView)

Example 5 with AlbumView

use of com.simplecity.amp_library.ui.modelviews.AlbumView 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)

Aggregations

AlbumView (com.simplecity.amp_library.ui.modelviews.AlbumView)8 Album (com.simplecity.amp_library.model.Album)7 SuppressLint (android.annotation.SuppressLint)6 Context (android.content.Context)6 Bundle (android.os.Bundle)6 GridLayoutManager (android.support.v7.widget.GridLayoutManager)6 PopupMenu (android.support.v7.widget.PopupMenu)6 View (android.view.View)6 Stream (com.annimon.stream.Stream)6 RequestManager (com.bumptech.glide.RequestManager)6 R (com.simplecity.amp_library.R)6 EmptyView (com.simplecity.amp_library.ui.modelviews.EmptyView)6 MusicUtils (com.simplecity.amp_library.utils.MusicUtils)6 Collections (java.util.Collections)6 RecyclerView (android.support.v7.widget.RecyclerView)5 LayoutInflater (android.view.LayoutInflater)5 ViewGroup (android.view.ViewGroup)5 Toast (android.widget.Toast)5 Playlist (com.simplecity.amp_library.model.Playlist)5 Song (com.simplecity.amp_library.model.Song)5