Search in sources :

Example 1 with ViewModel

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

the class InclExclDialog method getItems.

private static Observable<List<ViewModel>> getItems(@InclExclItem.Type int type, InclExclView.ClickListener listener) {
    Observable<List<InclExclItem>> items = type == InclExclItem.Type.INCLUDE ? DataManager.getInstance().getIncludeItems() : DataManager.getInstance().getExcludeItems();
    return items.map(inclExclItems -> Stream.of(inclExclItems).map(inclExclItem -> {
        InclExclView inclExclView = new InclExclView(inclExclItem);
        inclExclView.setClickListener(listener);
        return (ViewModel) inclExclView;
    }).toList());
}
Also used : InclExclView(com.simplecity.amp_library.ui.modelviews.InclExclView) List(java.util.List) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel)

Example 2 with ViewModel

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

the class TabChooserDialog method getDialog.

public static MaterialDialog getDialog(Activity activity) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity);
    ViewModelAdapter adapter = new ViewModelAdapter();
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelperCallback(adapter::moveItem, (fromPosition, toPosition) -> {
    }, () -> {
    }));
    TabViewModel.Listener listener = new TabViewModel.Listener() {

        @Override
        public void onStartDrag(TabViewModel.ViewHolder holder) {
            itemTouchHelper.startDrag(holder);
        }

        @Override
        public void onFolderChecked(TabViewModel tabViewModel, TabViewModel.ViewHolder viewHolder) {
            if (!ShuttleUtils.isUpgraded()) {
                viewHolder.checkBox.setChecked(false);
                tabViewModel.categoryItem.isChecked = false;
                UpgradeDialog.getUpgradeDialog(activity).show();
            }
        }
    };
    List<ViewModel> items = Stream.of(CategoryItem.getCategoryItems(sharedPreferences)).map(categoryItem -> {
        TabViewModel tabViewModel = new TabViewModel(categoryItem);
        tabViewModel.setListener(listener);
        return tabViewModel;
    }).collect(Collectors.toList());
    adapter.setItems(items);
    RecyclerView recyclerView = new RecyclerView(activity);
    recyclerView.setLayoutManager(new LinearLayoutManager(activity));
    recyclerView.setAdapter(adapter);
    itemTouchHelper.attachToRecyclerView(recyclerView);
    return new MaterialDialog.Builder(activity).title(R.string.pref_title_choose_tabs).customView(recyclerView, false).positiveText(R.string.button_done).onPositive((dialog, which) -> {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Stream.of(adapter.items).indexed().forEach(viewModelIntPair -> {
            ((TabViewModel) viewModelIntPair.getSecond()).categoryItem.sortOrder = viewModelIntPair.getFirst();
            ((TabViewModel) viewModelIntPair.getSecond()).categoryItem.savePrefs(editor);
        });
        LocalBroadcastManager.getInstance(activity).sendBroadcast(new Intent(LibraryController.EVENT_TABS_CHANGED));
    }).negativeText(R.string.close).build();
}
Also used : R(com.simplecity.amp_library.R) ItemTouchHelperCallback(com.simplecity.amp_library.ui.recyclerview.ItemTouchHelperCallback) Collectors(com.annimon.stream.Collectors) Stream(com.annimon.stream.Stream) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) Intent(android.content.Intent) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) CategoryItem(com.simplecity.amp_library.model.CategoryItem) TabViewModel(com.simplecity.amp_library.ui.modelviews.TabViewModel) RecyclerView(android.support.v7.widget.RecyclerView) List(java.util.List) SharedPreferences(android.content.SharedPreferences) LibraryController(com.simplecity.amp_library.ui.fragments.LibraryController) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) PreferenceManager(android.preference.PreferenceManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Activity(android.app.Activity) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) TabViewModel(com.simplecity.amp_library.ui.modelviews.TabViewModel) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ItemTouchHelperCallback(com.simplecity.amp_library.ui.recyclerview.ItemTouchHelperCallback) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) TabViewModel(com.simplecity.amp_library.ui.modelviews.TabViewModel) RecyclerView(android.support.v7.widget.RecyclerView) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)

Example 3 with ViewModel

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

the class AlbumFragment 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();
        if (playlistMenuDisposable != null) {
            playlistMenuDisposable.dispose();
        }
        playlistMenuDisposable = PlaylistUtils.createUpdatingPlaylistMenu(sub).subscribe();
        contextualToolbar.setOnMenuItemClickListener(MenuUtils.getAlbumMenuClickListener(getContext(), () -> contextualToolbarHelper.getItems(), deleteDialog -> deleteDialog.show(getChildFragmentManager()), () -> contextualToolbarHelper.finish()));
        contextualToolbarHelper = new ContextualToolbarHelper<>(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);
            }
        });
    }
}
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) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) SubMenu(android.view.SubMenu) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar)

Example 4 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel 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 5 with ViewModel

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

the class FolderFragment method changeDir.

@SuppressLint("CheckResult")
public void changeDir(File newDir) {
    if (setItemsDisposable != null) {
        setItemsDisposable.dispose();
    }
    disposables.add(Single.zip(DataManager.getInstance().getIncludeItems().first(Collections.emptyList()), DataManager.getInstance().getExcludeItems().first(Collections.emptyList()), Single.fromCallable(() -> {
        final String path = FileHelper.getPath(newDir);
        if (TextUtils.isEmpty(path)) {
            return new ArrayList<>();
        }
        currentDir = path;
        return fileBrowser.loadDir(new File(path));
    }), (Function3<List<InclExclItem>, List<InclExclItem>, List<BaseFileObject>, List<ViewModel>>) (whitelist, blacklist, baseFileObjects) -> {
        List<ViewModel> items = Stream.of(baseFileObjects).map(baseFileObject -> {
            // Look for an existing FolderView wrapping the BaseFileObject, we'll reuse it if it exists.
            FolderView folderView = (FolderView) Stream.of(adapter.items).filter(viewModel -> viewModel instanceof FolderView && (((FolderView) viewModel).baseFileObject.equals(baseFileObject))).findFirst().orElse(null);
            if (folderView == null) {
                folderView = new FolderView(baseFileObject, Stream.of(whitelist).anyMatch(inclExclItem -> inclExclItem.path.equals(baseFileObject.path)), Stream.of(blacklist).anyMatch(inclExclItem -> inclExclItem.path.equals(baseFileObject.path)));
                folderView.setShowWhitelist(isShowingWhitelist);
                folderView.setShowBlacklist(isShowingBlacklist);
                folderView.setClickListener(FolderFragment.this);
            }
            return folderView;
        }).collect(Collectors.toList());
        if (showBreadcrumbsInList && breadcrumbsView != null) {
            breadcrumbsView.setBreadcrumbsPath(currentDir);
            breadcrumbsView.setListener(FolderFragment.this);
            items.add(0, breadcrumbsView);
        }
        return items;
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
        if (adapter != null) {
            setItemsDisposable = adapter.setItems(adaptableItems);
        }
        if (breadcrumb != null) {
            breadcrumb.changeBreadcrumbPath(currentDir);
        }
        if (adapter != null) {
            changeBreadcrumbPath();
        }
        updateMenuItems();
    }, error -> LogUtils.logException(TAG, "Error changing dir", error)));
}
Also used : R(com.simplecity.amp_library.R) Bundle(android.os.Bundle) Rx.onErrorLogAndRethrow(com.afollestad.aesthetic.Rx.onErrorLogAndRethrow) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) BindView(butterknife.BindView) Song(com.simplecity.amp_library.model.Song) Aesthetic(com.afollestad.aesthetic.Aesthetic) CheckBox(android.widget.CheckBox) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) Unbinder(butterknife.Unbinder) BackPressListener(test.com.androidnavigation.fragment.BackPressListener) LogUtils(com.simplecity.amp_library.utils.LogUtils) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) SortManager(com.simplecity.amp_library.utils.SortManager) FileHelper(com.simplecity.amp_library.utils.FileHelper) PopupMenu(android.support.v7.widget.PopupMenu) ViewGroup(android.view.ViewGroup) Breadcrumb(com.simplecity.amp_library.interfaces.Breadcrumb) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) InclExclItem(com.simplecity.amp_library.model.InclExclItem) IntStream(com.annimon.stream.IntStream) BreadcrumbListener(com.simplecity.amp_library.interfaces.BreadcrumbListener) FileType(com.simplecity.amp_library.interfaces.FileType) AppBarLayout(android.support.design.widget.AppBarLayout) Stream(com.annimon.stream.Stream) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) ButterKnife(butterknife.ButterKnife) DrawerLockManager(com.simplecity.amp_library.ui.drawer.DrawerLockManager) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) Single(io.reactivex.Single) FolderView(com.simplecity.amp_library.ui.modelviews.FolderView) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) MenuInflater(android.view.MenuInflater) Toast(android.widget.Toast) Menu(android.view.Menu) ViewBackgroundAction(com.afollestad.aesthetic.ViewBackgroundAction) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) Observable(io.reactivex.Observable) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) Rx.distinctToMainThread(com.afollestad.aesthetic.Rx.distinctToMainThread) Nullable(io.reactivex.annotations.Nullable) Collectors(com.annimon.stream.Collectors) FileBrowser(com.simplecity.amp_library.utils.FileBrowser) LayoutInflater(android.view.LayoutInflater) Function3(io.reactivex.functions.Function3) ThemedStatusBarView(com.simplecity.amp_library.ui.views.ThemedStatusBarView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) TextUtils(android.text.TextUtils) File(java.io.File) RecyclerView(android.support.v7.widget.RecyclerView) Toolbar(android.support.v7.widget.Toolbar) BreadcrumbItem(com.simplecity.amp_library.ui.views.BreadcrumbItem) BreadcrumbsView(com.simplecity.amp_library.ui.modelviews.BreadcrumbsView) DataManager(com.simplecity.amp_library.utils.DataManager) Collections(java.util.Collections) FolderView(com.simplecity.amp_library.ui.modelviews.FolderView) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) SuppressLint(android.annotation.SuppressLint)

Aggregations

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