Fixed issues

This commit is contained in:
Yedidya Kennard 2016-08-16 15:26:28 +03:00
parent 1cf2cc129b
commit e3e7d48939
2 changed files with 80 additions and 35 deletions

View File

@ -3,9 +3,7 @@ package com.wix.RNCameraKit.gallery;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -20,7 +18,7 @@ import java.util.concurrent.TimeUnit;
/** /**
* Created by yedidyak on 30/06/2016. * Created by yedidyak on 30/06/2016.
*/ */
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHolder> { public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ImageHolder> {
private String overlayColor; private String overlayColor;
private Drawable unsupportedFinalImage; private Drawable unsupportedFinalImage;
@ -45,8 +43,6 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
MediaStore.Images.Media.MIME_TYPE MediaStore.Images.Media.MIME_TYPE
}; };
// private ArrayList<String> uris = new ArrayList<>();
// private ArrayList<Integer> ids = new ArrayList<>();
private ArrayList<Image> images = new ArrayList<>(); private ArrayList<Image> images = new ArrayList<>();
private ArrayList<String> selectedUris = new ArrayList<>(); private ArrayList<String> selectedUris = new ArrayList<>();
@ -67,23 +63,18 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
public void setUnselectedDrawable(Drawable unselectedDrawable) { public void setUnselectedDrawable(Drawable unselectedDrawable) {
this.unselectedDrawable = unselectedDrawable; this.unselectedDrawable = unselectedDrawable;
} notifyView();
public Drawable getUnselectedDrawable() {
return unselectedDrawable;
} }
public void setSupportedFileTypes(ArrayList<String> supportedFileTypes) { public void setSupportedFileTypes(ArrayList<String> supportedFileTypes) {
this.supportedFileTypes = supportedFileTypes; this.supportedFileTypes = supportedFileTypes;
notifyView();
} }
public class StupidHolder extends RecyclerView.ViewHolder { public class ImageHolder extends RecyclerView.ViewHolder {
public StupidHolder(View itemView) { public ImageHolder(View itemView) {
super(itemView); super(itemView);
} }
// int id;
// String uri;
Image image; Image image;
} }
@ -92,6 +83,7 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
public GalleryAdapter(GalleryView context) { public GalleryAdapter(GalleryView context) {
this.view = context; this.view = context;
setHasStableIds(true);
int cores = Runtime.getRuntime().availableProcessors(); int cores = Runtime.getRuntime().availableProcessors();
executor = new ThreadPoolExecutor(cores, cores, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>()); executor = new ThreadPoolExecutor(cores, cores, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
setAlbum(albumName); setAlbum(albumName);
@ -114,8 +106,6 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
// ids.clear();
// uris.clear();
images.clear(); images.clear();
String selection = ""; String selection = "";
@ -138,32 +128,31 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
int idIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID); int idIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int mimeIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE); int mimeIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
do { do {
// uris.add(cursor.getString(dataIndex));
// ids.add(cursor.getInt(idIndex));
images.add(new Image(cursor.getString(dataIndex), cursor.getInt(idIndex), cursor.getString(mimeIndex))); images.add(new Image(cursor.getString(dataIndex), cursor.getInt(idIndex), cursor.getString(mimeIndex)));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
//
// Collections.reverse(uris);
// Collections.reverse(ids);
Collections.reverse(images); Collections.reverse(images);
cursor.close(); cursor.close();
refreshing = false; refreshing = false;
notifyView(true);
notifyView();
} }
}).start(); }).start();
} }
public void notifyView() { public void notifyView() {
notifyView(false);
}
public void notifyView(final boolean refreshAll) {
view.post(new Runnable() { view.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (!view.isComputingLayout()) { if (!view.isComputingLayout()) {
notifyDataSetChanged(); view.swapAdapter(GalleryAdapter.this, false);
if(refreshAll) {
notifyItemRangeChanged(0, getItemCount());
}
} else { } else {
notifyView(); notifyView();
} }
@ -180,19 +169,18 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
@Override @Override
public StupidHolder onCreateViewHolder(ViewGroup parent, int viewType) { public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
SelectableImage v = new SelectableImage(view.getContext()); SelectableImage v = new SelectableImage(view.getContext());
v.setScaleType(ImageView.ScaleType.CENTER_CROP); v.setScaleType(ImageView.ScaleType.CENTER_CROP);
v.setBackgroundColor(Color.LTGRAY); v.setBackgroundColor(Color.LTGRAY);
return new StupidHolder(v); return new ImageHolder(v);
} }
@Override @Override
public void onBindViewHolder(final StupidHolder holder, final int position) { public void onBindViewHolder(final ImageHolder holder, final int position) {
final SelectableImage selectableImageView = (SelectableImage)holder.itemView; final SelectableImage selectableImageView = (SelectableImage)holder.itemView;
// holder.id = ids.get(position);
// holder.uri = uris.get(position);
// holder.supported =
holder.image = images.get(position); holder.image = images.get(position);
boolean selected = (selectedUris.indexOf(holder.image.uri) + 1) > 0; boolean selected = (selectedUris.indexOf(holder.image.uri) + 1) > 0;
final boolean supported = isSupported(holder.image); final boolean supported = isSupported(holder.image);
@ -204,13 +192,19 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
public void onClick(View v) { public void onClick(View v) {
if(supported) { if(supported) {
view.onTapImage(holder.image.uri); view.onTapImage(holder.image.uri);
v.setSelected(!v.isSelected());
} else { } else {
//
} }
} }
}); });
} }
@Override
public long getItemId(int position) {
return images.get(position).id;
}
private boolean isSupported(Image image) { private boolean isSupported(Image image) {
if(supportedFileTypes.isEmpty()) { if(supportedFileTypes.isEmpty()) {
return true; return true;

View File

@ -8,6 +8,12 @@ import android.graphics.drawable.Drawable;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -34,6 +40,7 @@ public class SelectableImage extends FrameLayout {
private LinearLayout unsupportedLayout; private LinearLayout unsupportedLayout;
private ImageView unsupportedImage; private ImageView unsupportedImage;
private TextView unsupportedTextView; private TextView unsupportedTextView;
private boolean selected;
public SelectableImage(Context context) { public SelectableImage(Context context) {
super(context); super(context);
@ -84,7 +91,7 @@ public class SelectableImage extends FrameLayout {
} }
public void bind(ThreadPoolExecutor executor, boolean selected, final Integer id, boolean supported) { public void bind(ThreadPoolExecutor executor, boolean selected, final Integer id, boolean supported) {
// selectedView.setVisibility(selected ? VISIBLE : INVISIBLE); this.selected = selected;
selectedView.setImageDrawable(selected ? selectedDrawable : unselectedDrawable); selectedView.setImageDrawable(selected ? selectedDrawable : unselectedDrawable);
if (this.id != id) { if (this.id != id) {
this.id = id; this.id = id;
@ -117,6 +124,50 @@ public class SelectableImage extends FrameLayout {
unsupportedLayout.setVisibility(supported ? GONE : VISIBLE); unsupportedLayout.setVisibility(supported ? GONE : VISIBLE);
} }
@Override
public void setOnClickListener(final OnClickListener l) {
super.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
animateBounce();
l.onClick(v);
}
});
}
private void animateBounce() {
// final float growTo = 0.9f;
// final long duration = 100;
//
// ScaleAnimation grow = new ScaleAnimation(1, growTo, 1, growTo,
// Animation.RELATIVE_TO_SELF, 0.5f,
// Animation.RELATIVE_TO_SELF, 0.5f);
// grow.setDuration(duration / 2);
// ScaleAnimation shrink = new ScaleAnimation(growTo, 1, growTo, 1,
// Animation.RELATIVE_TO_SELF, 0.5f,
// Animation.RELATIVE_TO_SELF, 0.5f);
// shrink.setDuration(duration / 2);
// shrink.setStartOffset(duration / 2);
// AnimationSet growAndShrink = new AnimationSet(true);
// growAndShrink.setInterpolator(new BounceInterpolator());
// growAndShrink.addAnimation(shrink);
// growAndShrink.addAnimation(grow);
// this.startAnimation(growAndShrink);
}
@Override
public boolean isSelected() {
return selected;
}
@Override
public void setSelected(boolean selected) {
this.selected = selected;
selectedView.setImageDrawable(selected ? selectedDrawable : unselectedDrawable);
}
public void setDrawables(Drawable selectedDrawable, Drawable unselectedDrawable) { public void setDrawables(Drawable selectedDrawable, Drawable unselectedDrawable) {
this.selectedDrawable = selectedDrawable; this.selectedDrawable = selectedDrawable;
this.unselectedDrawable = unselectedDrawable; this.unselectedDrawable = unselectedDrawable;