use of com.simplecity.amp_library.ui.recyclerview.SpacesItemDecoration in project Shuttle by timusus.
the class ArtworkDialog method showDialog.
public static void showDialog(Context context, ArtworkProvider artworkProvider) {
View customView = LayoutInflater.from(context).inflate(R.layout.dialog_artwork, null);
ArtworkAdapter artworkAdapter = new ArtworkAdapter();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = (RecyclerView) customView.findViewById(R.id.recyclerView);
recyclerView.addItemDecoration(new SpacesItemDecoration(16));
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(0);
recyclerView.setRecyclerListener(holder -> {
if (holder.getAdapterPosition() != -1) {
artworkAdapter.items.get(holder.getAdapterPosition()).recycle(holder);
}
});
artworkAdapter.items.add(0, new LoadingView());
artworkAdapter.notifyDataSetChanged();
recyclerView.setAdapter(artworkAdapter);
ArtworkView.GlideListener glideListener = artworkView -> {
int index = artworkAdapter.items.indexOf(artworkView);
if (index != -1) {
artworkAdapter.removeItem(index);
}
};
List<AdaptableItem> adaptableItems = 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);
adaptableItems.add(artworkView);
}
if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.MEDIA_STORE) {
adaptableItems.add(new ArtworkView(ArtworkProvider.Type.MEDIA_STORE, artworkProvider, glideListener));
}
if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.TAG) {
adaptableItems.add(new ArtworkView(ArtworkProvider.Type.TAG, artworkProvider, glideListener));
}
if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.LAST_FM) {
adaptableItems.add(new ArtworkView(ArtworkProvider.Type.LAST_FM, artworkProvider, glideListener));
}
if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.ITUNES) {
adaptableItems.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);
adaptableItems.add(folderView);
artworkAdapter.setItems(adaptableItems);
Observable.fromCallable(artworkProvider::getFolderArtworkFiles).subscribeOn(Schedulers.io()).subscribeOn(AndroidSchedulers.mainThread()).subscribe(files -> {
artworkAdapter.removeItem(artworkAdapter.items.indexOf(folderView));
if (files != null) {
Stream.of(files).filter(file -> userSelectedArtwork == null || !file.getPath().equals(userSelectedArtwork.path)).forEach(file -> {
artworkAdapter.addItem(new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, false));
});
}
}, error -> {
Log.e(TAG, "Error getting folder artwork files.. " + error.toString());
});
DialogUtils.getBuilder(context).title(context.getString(R.string.artwork_edit)).customView(customView, false).autoDismiss(false).positiveText(context.getString(R.string.save)).onPositive((dialog, which) -> {
if (artworkAdapter.checkedItem != null) {
ContentValues values = new ContentValues();
values.put(CustomArtworkTable.COLUMN_KEY, artworkProvider.getArtworkKey());
values.put(CustomArtworkTable.COLUMN_TYPE, artworkAdapter.checkedItem.type);
values.put(CustomArtworkTable.COLUMN_PATH, artworkAdapter.checkedItem.file == null ? null : artworkAdapter.checkedItem.file.getPath());
context.getContentResolver().insert(CustomArtworkTable.URI, values);
ShuttleApplication.getInstance().userSelectedArtwork.put(artworkProvider.getArtworkKey(), new UserSelectedArtwork(artworkAdapter.checkedItem.type, artworkAdapter.checkedItem.file == null ? null : artworkAdapter.checkedItem.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 -> {
File dir = new File(ShuttleApplication.getInstance().getFilesDir() + "/shuttle/custom_artwork/" + artworkProvider.getArtworkKey().hashCode() + "/");
if (!dir.exists()) {
dir.mkdirs();
} else {
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 (artworkAdapter.getItemCount() != 0) {
File aFile = ((ArtworkView) artworkAdapter.items.get(0)).file;
if (aFile != null && aFile.getPath().contains(artworkProvider.getArtworkKey())) {
artworkAdapter.removeItem(0);
}
}
ArtworkView artworkView = new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, true);
artworkAdapter.addItem(0, artworkView);
artworkAdapter.selectItem(0);
recyclerView.scrollToPosition(0);
})).cancelable(false).show();
}
Aggregations