Search in sources :

Example 26 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class SearchFragment method setupContextualToolbar.

private void setupContextualToolbar() {
    ContextualToolbar contextualToolbar = ContextualToolbar.findContextualToolbar(this);
    if (contextualToolbar != null) {
        contextualToolbar.getMenu().clear();
        contextualToolbar.inflateMenu(R.menu.context_menu_general);
        SubMenu sub = contextualToolbar.getMenu().findItem(R.id.addToPlaylist).getSubMenu();
        disposables.add(PlaylistUtils.createUpdatingPlaylistMenu(sub).subscribe());
        contextualToolbar.setOnMenuItemClickListener(MenuUtils.getSongMenuClickListener(getContext(), Single.defer(() -> Operators.reduceSongSingles(contextualToolbarHelper.getItems())), deleteDialog -> deleteDialog.show(getChildFragmentManager()), () -> contextualToolbarHelper.finish()));
        contextualToolbarHelper = new ContextualToolbarHelper<Single<List<Song>>>(contextualToolbar, new ContextualToolbarHelper.Callback() {

            @Override
            public void notifyItemChanged(int position, SelectableViewModel viewModel) {
                adapter.notifyItemChanged(position, 0);
            }

            @Override
            public void notifyDatasetChanged() {
                adapter.notifyItemRangeChanged(0, adapter.items.size(), 0);
            }
        }) {

            @Override
            public void start() {
                super.start();
                toolbar.setVisibility(View.GONE);
            }

            @Override
            public void finish() {
                if (toolbar != null) {
                    toolbar.setVisibility(View.VISIBLE);
                }
                super.finish();
            }
        };
        searchPresenter.setContextualToolbarHelper(contextualToolbarHelper);
    }
}
Also used : R(com.simplecity.amp_library.R) BaseDetailFragment(com.simplecity.amp_library.ui.detail.BaseDetailFragment) Bundle(android.os.Bundle) SearchView(android.support.v7.widget.SearchView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) BindView(butterknife.BindView) Song(com.simplecity.amp_library.model.Song) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) View(android.view.View) ResourceUtils(com.simplecity.amp_library.utils.ResourceUtils) ViewCompat(android.support.v4.view.ViewCompat) Transition(android.transition.Transition) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) RxSearchView(com.jakewharton.rxbinding2.support.v7.widget.RxSearchView) SubMenu(android.view.SubMenu) ViewGroup(android.view.ViewGroup) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) ContextualToolbarHost(com.simplecity.amp_library.ui.views.ContextualToolbarHost) Nullable(android.support.annotation.Nullable) ArtistDetailFragment(com.simplecity.amp_library.ui.detail.ArtistDetailFragment) Context(android.content.Context) Album(com.simplecity.amp_library.model.Album) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) ButterKnife(butterknife.ButterKnife) AlbumDetailFragment(com.simplecity.amp_library.ui.detail.AlbumDetailFragment) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) NonNull(android.support.annotation.NonNull) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) Single(io.reactivex.Single) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) PrefixHighlighter(com.simplecity.amp_library.format.PrefixHighlighter) Toast(android.widget.Toast) LoadingView(com.simplecity.amp_library.ui.modelviews.LoadingView) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) DeleteDialog(com.simplecity.amp_library.ui.dialog.DeleteDialog) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) TaggerDialog(com.simplecity.amp_library.tagger.TaggerDialog) BackpressureStrategy(io.reactivex.BackpressureStrategy) LayoutInflater(android.view.LayoutInflater) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) TimeUnit(java.util.concurrent.TimeUnit) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Glide(com.bumptech.glide.Glide) Toolbar(android.support.v7.widget.Toolbar) Pair(android.support.v4.util.Pair) TransitionInflater(android.transition.TransitionInflater) BaseFragment(com.simplecity.amp_library.ui.fragments.BaseFragment) AlbumArtist(com.simplecity.amp_library.model.AlbumArtist) Operators(com.simplecity.amp_library.utils.Operators) Collections(java.util.Collections) Song(com.simplecity.amp_library.model.Song) Single(io.reactivex.Single) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) SubMenu(android.view.SubMenu) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar)

Example 27 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class QueuePresenter method loadData.

private void loadData() {
    QueueView queueView = getView();
    if (queueView != null) {
        List<ViewModel> data = Stream.of(MusicUtils.getQueue()).map(song -> {
            SongView songView = new SongView(song, requestManager) {

                @Override
                public boolean equals(Object o) {
                    // are considered to be the same. We lose some RV optimisations here, but at least we don't crash.
                    return false;
                }
            };
            songView.setClickListener(clickListener);
            songView.showAlbumArt(true);
            songView.setEditable(true);
            return songView;
        }).collect(Collectors.toList());
        queueView.loadData(data, MusicUtils.getQueuePosition());
    }
}
Also used : Context(android.content.Context) Stream(com.annimon.stream.Stream) SongView(com.simplecity.amp_library.ui.modelviews.SongView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) Intent(android.content.Intent) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) NonNull(android.support.annotation.NonNull) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) Song(com.simplecity.amp_library.model.Song) RequestManager(com.bumptech.glide.RequestManager) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) View(android.view.View) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) BackpressureStrategy(io.reactivex.BackpressureStrategy) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) Collectors(com.annimon.stream.Collectors) QueueView(com.simplecity.amp_library.ui.views.QueueView) Playlist(com.simplecity.amp_library.model.Playlist) IntentFilter(android.content.IntentFilter) PopupMenu(android.support.v7.widget.PopupMenu) RxBroadcast(com.cantrowitz.rxbroadcast.RxBroadcast) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) MusicService(com.simplecity.amp_library.playback.MusicService) QueueView(com.simplecity.amp_library.ui.views.QueueView) SongView(com.simplecity.amp_library.ui.modelviews.SongView) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel)

Example 28 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class ArtworkDialog method build.

public static MaterialDialog build(Context context, ArtworkProvider artworkProvider) {
    @SuppressLint("InflateParams") View customView = LayoutInflater.from(context).inflate(R.layout.dialog_artwork, null);
    ViewModelAdapter adapter = new ViewModelAdapter();
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
    RecyclerView recyclerView = customView.findViewById(R.id.recyclerView);
    recyclerView.addItemDecoration(new SpacesItemDecoration(16));
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemViewCacheSize(0);
    recyclerView.setRecyclerListener(new RecyclerListener());
    adapter.items.add(0, new ArtworkLoadingView());
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);
    ArtworkView.GlideListener glideListener = artworkView -> {
        int index = adapter.items.indexOf(artworkView);
        if (index != -1) {
            adapter.removeItem(index);
        }
    };
    List<ViewModel> viewModels = new ArrayList<>();
    UserSelectedArtwork userSelectedArtwork = ShuttleApplication.getInstance().userSelectedArtwork.get(artworkProvider.getArtworkKey());
    if (userSelectedArtwork != null) {
        File file = null;
        if (userSelectedArtwork.path != null) {
            file = new File(userSelectedArtwork.path);
        }
        ArtworkView artworkView = new ArtworkView(userSelectedArtwork.type, artworkProvider, glideListener, file, true);
        artworkView.setSelected(true);
        viewModels.add(artworkView);
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.MEDIA_STORE) {
        viewModels.add(new ArtworkView(ArtworkProvider.Type.MEDIA_STORE, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.TAG) {
        viewModels.add(new ArtworkView(ArtworkProvider.Type.TAG, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.LAST_FM) {
        viewModels.add(new ArtworkView(ArtworkProvider.Type.LAST_FM, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.ITUNES) {
        viewModels.add(new ArtworkView(ArtworkProvider.Type.ITUNES, artworkProvider, glideListener));
    }
    // Dummy Folder ArtworkView - will be replaced or removed depending on availability of folder images
    ArtworkView folderView = new ArtworkView(ArtworkProvider.Type.FOLDER, null, null);
    viewModels.add(folderView);
    ArtworkView.ClickListener listener = artworkView -> {
        Stream.of(viewModels).filter(viewModel -> viewModel instanceof ArtworkView).forEachIndexed((i, viewModel) -> ((ArtworkView) viewModel).setSelected(viewModel == artworkView));
        adapter.notifyItemRangeChanged(0, adapter.getItemCount(), 0);
    };
    Stream.of(viewModels).filter(viewModel -> viewModel instanceof ArtworkView).forEach(viewModel -> ((ArtworkView) viewModel).setListener(listener));
    adapter.setItems(viewModels);
    Observable.fromCallable(artworkProvider::getFolderArtworkFiles).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(files -> {
        adapter.removeItem(adapter.items.indexOf(folderView));
        if (files != null) {
            Stream.of(files).filter(file -> userSelectedArtwork == null || !file.getPath().equals(userSelectedArtwork.path)).forEach(file -> adapter.addItem(new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, false)));
        }
    }, error -> LogUtils.logException(TAG, "Error getting artwork files", error));
    return new MaterialDialog.Builder(context).title(R.string.artwork_edit).customView(customView, false).autoDismiss(false).positiveText(context.getString(R.string.save)).onPositive((dialog, which) -> {
        ArtworkView checkedView = ArtworkDialog.getCheckedView(adapter.items);
        if (checkedView != null) {
            ArtworkModel artworkModel = checkedView.getItem();
            ContentValues values = new ContentValues();
            values.put(CustomArtworkTable.COLUMN_KEY, artworkProvider.getArtworkKey());
            values.put(CustomArtworkTable.COLUMN_TYPE, artworkModel.type);
            values.put(CustomArtworkTable.COLUMN_PATH, artworkModel.file == null ? null : artworkModel.file.getPath());
            context.getContentResolver().insert(CustomArtworkTable.URI, values);
            ShuttleApplication.getInstance().userSelectedArtwork.put(artworkProvider.getArtworkKey(), new UserSelectedArtwork(artworkModel.type, artworkModel.file == null ? null : artworkModel.file.getPath()));
        } else {
            context.getContentResolver().delete(CustomArtworkTable.URI, CustomArtworkTable.COLUMN_KEY + "='" + artworkProvider.getArtworkKey().replaceAll("'", "\''") + "'", null);
            ShuttleApplication.getInstance().userSelectedArtwork.remove(artworkProvider.getArtworkKey());
        }
        dialog.dismiss();
    }).negativeText(context.getString(R.string.close)).onNegative((dialog, which) -> dialog.dismiss()).neutralText(context.getString(R.string.artwork_gallery)).onNeutral((dialog, which) -> RxImagePicker.with(context).requestImage(Sources.GALLERY).flatMap(uri -> {
        // The directory will be shuttle/custom_artwork/key_hashcode/currentSystemTime.artwork
        // We want the directory to be based on the key, so we can delete old artwork, and the
        // filename to be unique, because it's used for Glide caching.
        File dir = new File(ShuttleApplication.getInstance().getFilesDir() + "/shuttle/custom_artwork/" + artworkProvider.getArtworkKey().hashCode() + "/");
        // Create dir if necessary
        if (!dir.exists()) {
            dir.mkdirs();
        } else {
            // Delete any existing artwork for this key.
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (String child : children) {
                    new File(dir, child).delete();
                }
            }
        }
        File file = new File(dir.getPath() + System.currentTimeMillis() + ".artwork");
        try {
            file.createNewFile();
            if (file.exists()) {
                return RxImageConverters.uriToFile(context, uri, file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }).filter(file -> file != null && file.exists()).subscribe(file -> {
        // If we've already got user-selected artwork in the adapter, remove it.
        if (adapter.getItemCount() != 0) {
            File aFile = ((ArtworkView) adapter.items.get(0)).file;
            if (aFile != null && aFile.getPath().contains(artworkProvider.getArtworkKey())) {
                adapter.removeItem(0);
            }
        }
        ArtworkView artworkView = new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, true);
        artworkView.setSelected(true);
        adapter.addItem(0, artworkView);
        recyclerView.scrollToPosition(0);
    }, error -> LogUtils.logException(TAG, "Error picking from gallery", error))).cancelable(false).build();
}
Also used : R(com.simplecity.amp_library.R) Context(android.content.Context) SpacesItemDecoration(com.simplecity.amp_library.ui.recyclerview.SpacesItemDecoration) Stream(com.annimon.stream.Stream) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ArrayList(java.util.ArrayList) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) SuppressLint(android.annotation.SuppressLint) View(android.view.View) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) RxImageConverters(com.mlsdev.rximagepicker.RxImageConverters) RxImagePicker(com.mlsdev.rximagepicker.RxImagePicker) ArtworkModel(com.simplecity.amp_library.model.ArtworkModel) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) LayoutInflater(android.view.LayoutInflater) ArtworkLoadingView(com.simplecity.amp_library.ui.modelviews.ArtworkLoadingView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) IOException(java.io.IOException) ArtworkProvider(com.simplecity.amp_library.model.ArtworkProvider) CustomArtworkTable(com.simplecity.amp_library.sql.databases.CustomArtworkTable) File(java.io.File) RecyclerView(android.support.v7.widget.RecyclerView) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) ContentValues(android.content.ContentValues) Nullable(android.support.annotation.Nullable) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Sources(com.mlsdev.rximagepicker.Sources) ContentValues(android.content.ContentValues) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) ArrayList(java.util.ArrayList) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) IOException(java.io.IOException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ArtworkLoadingView(com.simplecity.amp_library.ui.modelviews.ArtworkLoadingView) ArtworkModel(com.simplecity.amp_library.model.ArtworkModel) View(android.view.View) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) ArtworkLoadingView(com.simplecity.amp_library.ui.modelviews.ArtworkLoadingView) RecyclerView(android.support.v7.widget.RecyclerView) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) SuppressLint(android.annotation.SuppressLint) RecyclerView(android.support.v7.widget.RecyclerView) SpacesItemDecoration(com.simplecity.amp_library.ui.recyclerview.SpacesItemDecoration) File(java.io.File) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)

Aggregations

ViewModel (com.simplecityapps.recycler_adapter.model.ViewModel)28 Stream (com.annimon.stream.Stream)18 R (com.simplecity.amp_library.R)18 List (java.util.List)18 Nullable (android.support.annotation.Nullable)17 View (android.view.View)17 Bundle (android.os.Bundle)16 LayoutInflater (android.view.LayoutInflater)16 MenuUtils (com.simplecity.amp_library.utils.MenuUtils)16 RecyclerListener (com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener)16 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)16 ViewGroup (android.view.ViewGroup)15 MusicUtils (com.simplecity.amp_library.utils.MusicUtils)15 Disposable (io.reactivex.disposables.Disposable)15 PopupMenu (android.support.v7.widget.PopupMenu)14 UpgradeDialog (com.simplecity.amp_library.ui.dialog.UpgradeDialog)14 EmptyView (com.simplecity.amp_library.ui.modelviews.EmptyView)14 Observable (io.reactivex.Observable)14 Collections (java.util.Collections)14 TimeUnit (java.util.concurrent.TimeUnit)14