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());
}
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();
}
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);
}
});
}
}
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));
}
});
}
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)));
}
Aggregations