mirror of
https://github.com/status-im/react-native-camera-kit.git
synced 2025-02-28 15:40:47 +00:00
Android selection state (working but buggy)
This commit is contained in:
parent
4b2465d30c
commit
a7a8702c6e
@ -11,6 +11,8 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
import com.facebook.react.bridge.ReactContext;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.concurrent.LinkedBlockingDeque;
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
@ -30,6 +32,12 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
|||||||
|
|
||||||
private ArrayList<String> uris = new ArrayList<>();
|
private ArrayList<String> uris = new ArrayList<>();
|
||||||
private ArrayList<Integer> ids = new ArrayList<>();
|
private ArrayList<Integer> ids = new ArrayList<>();
|
||||||
|
private ArrayList<String> selectedUris = new ArrayList<>();
|
||||||
|
private String albumName = "";
|
||||||
|
|
||||||
|
public void setSelectedUris(ArrayList<String> selectedUris) {
|
||||||
|
this.selectedUris = selectedUris;
|
||||||
|
}
|
||||||
|
|
||||||
public class StupidHolder extends RecyclerView.ViewHolder {
|
public class StupidHolder extends RecyclerView.ViewHolder {
|
||||||
public StupidHolder(View itemView) {
|
public StupidHolder(View itemView) {
|
||||||
@ -39,24 +47,30 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
|||||||
String uri;
|
String uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Context context;
|
private GalleryView view;
|
||||||
private ThreadPoolExecutor executor;
|
private ThreadPoolExecutor executor;
|
||||||
|
|
||||||
public GalleryAdapter(Context context) {
|
public GalleryAdapter(GalleryView context) {
|
||||||
this.context = context;
|
this.view = context;
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAlbum(String albumName) {
|
public void setAlbum(String albumName) {
|
||||||
|
this.albumName = albumName;
|
||||||
|
refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void refreshData() {
|
||||||
ids.clear();
|
ids.clear();
|
||||||
|
|
||||||
String selection = null;
|
String selection = "";
|
||||||
if(albumName != null || !albumName.equals("All Photos")) {
|
if(albumName == null || albumName.isEmpty() || !albumName.equals("All Photos")) {
|
||||||
selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
|
selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor cursor = context.getContentResolver().query(
|
Cursor cursor = view.getContext().getContentResolver().query(
|
||||||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
||||||
PROJECTION,
|
PROJECTION,
|
||||||
selection,
|
selection,
|
||||||
@ -81,12 +95,7 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StupidHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
public StupidHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
ImageView v = new ImageView(context) {
|
SelectableImage v = new SelectableImage(view.getContext());
|
||||||
@Override
|
|
||||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
||||||
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
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 StupidHolder(v);
|
||||||
@ -95,26 +104,35 @@ public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.StupidHo
|
|||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(final StupidHolder holder, final int position) {
|
public void onBindViewHolder(final StupidHolder holder, final int position) {
|
||||||
|
|
||||||
final ImageView imageView = (ImageView)holder.itemView;
|
final SelectableImage selectableImageView = (SelectableImage)holder.itemView;
|
||||||
imageView.setImageBitmap(null);
|
selectableImageView.setImageBitmap(null);
|
||||||
imageView.setBackgroundColor(Color.LTGRAY);
|
selectableImageView.setBackgroundColor(Color.LTGRAY);
|
||||||
holder.id = ids.get(position);
|
holder.id = ids.get(position);
|
||||||
holder.uri = uris.get(position);
|
holder.uri = uris.get(position);
|
||||||
|
|
||||||
|
selectableImageView.setSelected(selectedUris.indexOf(holder.uri) + 1);
|
||||||
|
|
||||||
|
selectableImageView.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
view.onTapImage(holder.uri);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
executor.execute(new Runnable() {
|
executor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
final Bitmap bmp = MediaStore.Images.Thumbnails.getThumbnail(
|
final Bitmap bmp = MediaStore.Images.Thumbnails.getThumbnail(
|
||||||
context.getContentResolver(),
|
view.getContext().getContentResolver(),
|
||||||
ids.get(holder.getAdapterPosition()),
|
ids.get(holder.getAdapterPosition()),
|
||||||
MediaStore.Images.Thumbnails.MINI_KIND,
|
MediaStore.Images.Thumbnails.MINI_KIND,
|
||||||
null);
|
null);
|
||||||
|
|
||||||
if (holder.id == ids.get(holder.getAdapterPosition())) {
|
if (holder.id == ids.get(holder.getAdapterPosition())) {
|
||||||
((Activity) context).runOnUiThread(new Runnable() {
|
((Activity) ((ReactContext)view.getContext()).getBaseContext()).runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
imageView.setImageBitmap(bmp);
|
selectableImageView.setImageBitmap(bmp);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,23 @@
|
|||||||
package com.wix.RNCameraKit;
|
package com.wix.RNCameraKit;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.support.v7.widget.GridLayoutManager;
|
import android.support.v7.widget.GridLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.FrameLayout;
|
|
||||||
import android.widget.Toast;
|
import com.facebook.react.bridge.Arguments;
|
||||||
|
import com.facebook.react.bridge.ReactContext;
|
||||||
|
import com.facebook.react.bridge.WritableMap;
|
||||||
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by yedidyak on 30/06/2016.
|
* Created by yedidyak on 30/06/2016.
|
||||||
*/
|
*/
|
||||||
public class GalleryView extends RecyclerView {
|
public class GalleryView extends RecyclerView {
|
||||||
|
|
||||||
private String albumName;
|
|
||||||
private GalleryAdapter adapter;
|
private GalleryAdapter adapter;
|
||||||
private int itemSpacing;
|
private int itemSpacing;
|
||||||
private int lineSpacing;
|
private int lineSpacing;
|
||||||
@ -23,7 +25,7 @@ public class GalleryView extends RecyclerView {
|
|||||||
public GalleryView(Context context) {
|
public GalleryView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
setHasFixedSize(true);
|
setHasFixedSize(true);
|
||||||
adapter = new GalleryAdapter(context);
|
adapter = new GalleryAdapter(this);
|
||||||
setAdapter(adapter);
|
setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,9 +42,6 @@ public class GalleryView extends RecyclerView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setAlbumName(String albumName) {
|
public void setAlbumName(String albumName) {
|
||||||
this.albumName = albumName;
|
|
||||||
Toast.makeText(this.getContext(), albumName, Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
adapter.setAlbum(albumName);
|
adapter.setAlbum(albumName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,4 +60,18 @@ public class GalleryView extends RecyclerView {
|
|||||||
layoutManager.setOrientation(GridLayoutManager.VERTICAL);
|
layoutManager.setOrientation(GridLayoutManager.VERTICAL);
|
||||||
setLayoutManager(layoutManager);
|
setLayoutManager(layoutManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onTapImage(String uri) {
|
||||||
|
ReactContext reactContext = ((ReactContext)getContext());
|
||||||
|
WritableMap event = Arguments.createMap();
|
||||||
|
event.putString("uri", uri);
|
||||||
|
event.putString("id", "onTapImage");
|
||||||
|
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("onTapImage", event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSelectedUris(ArrayList<String> selectedUris) {
|
||||||
|
adapter.setSelectedUris(selectedUris);
|
||||||
|
adapter.notifyDataSetChanged();
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package com.wix.RNCameraKit;
|
package com.wix.RNCameraKit;
|
||||||
|
|
||||||
|
import com.facebook.react.bridge.ReadableArray;
|
||||||
import com.facebook.react.uimanager.SimpleViewManager;
|
import com.facebook.react.uimanager.SimpleViewManager;
|
||||||
import com.facebook.react.uimanager.ThemedReactContext;
|
import com.facebook.react.uimanager.ThemedReactContext;
|
||||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by yedidyak on 30/06/2016.
|
* Created by yedidyak on 30/06/2016.
|
||||||
*/
|
*/
|
||||||
@ -16,7 +19,7 @@ public class GalleryViewManager extends SimpleViewManager<GalleryView> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GalleryView createViewInstance(ThemedReactContext reactContext) {
|
protected GalleryView createViewInstance(ThemedReactContext reactContext) {
|
||||||
return new GalleryView(reactContext.getBaseContext());
|
return new GalleryView(reactContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactProp(name = "albumName")
|
@ReactProp(name = "albumName")
|
||||||
@ -38,4 +41,13 @@ public class GalleryViewManager extends SimpleViewManager<GalleryView> {
|
|||||||
public void setColumnCount(GalleryView view, int columnCount) {
|
public void setColumnCount(GalleryView view, int columnCount) {
|
||||||
view.setColumnCount(columnCount);
|
view.setColumnCount(columnCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ReactProp(name = "selectedUris")
|
||||||
|
public void setSelectedUris(GalleryView view, ReadableArray uris) {
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
for(int i = 0; i < uris.size(); i++) {
|
||||||
|
list.add(uris.getString(i));
|
||||||
|
}
|
||||||
|
view.setSelectedUris(list);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.wix.RNCameraKit;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by yedidyak on 30/06/2016.
|
||||||
|
*/
|
||||||
|
public class SelectableImage extends FrameLayout {
|
||||||
|
|
||||||
|
private final ImageView imageView;
|
||||||
|
private final TextView selectedView;
|
||||||
|
|
||||||
|
public SelectableImage(Context context) {
|
||||||
|
super(context);
|
||||||
|
imageView = new ImageView(context);
|
||||||
|
addView(imageView, MATCH_PARENT, MATCH_PARENT);
|
||||||
|
selectedView = new TextView(context);
|
||||||
|
selectedView.setBackgroundColor(Color.BLUE);
|
||||||
|
LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
|
||||||
|
params.setMargins(30,30,30,30);
|
||||||
|
addView(selectedView, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSelected(int selectedPosition) {
|
||||||
|
selectedView.setVisibility(selectedPosition > 0 ? VISIBLE : INVISIBLE);
|
||||||
|
selectedView.setText("" + selectedPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
|
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScaleType(ImageView.ScaleType scaleType) {
|
||||||
|
imageView.setScaleType(scaleType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImageBitmap(Bitmap imageBitmap) {
|
||||||
|
imageView.setImageBitmap(imageBitmap);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
import {
|
import {
|
||||||
requireNativeComponent,
|
requireNativeComponent, DeviceEventEmitter
|
||||||
//NativeModules
|
//NativeModules
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
|
|
||||||
@ -10,13 +10,31 @@ const ALL_PHOTOS = 'All Photos';
|
|||||||
|
|
||||||
export default class CameraKitGalleryView extends Component {
|
export default class CameraKitGalleryView extends Component {
|
||||||
|
|
||||||
|
static propTypes = {
|
||||||
|
//TODO
|
||||||
|
};
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.onTapImage = this.onTapImage.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillMount() {
|
||||||
|
DeviceEventEmitter.addListener('onTapImage', this.onTapImage);
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const transformedProps = {...this.props};
|
const transformedProps = {...this.props};
|
||||||
transformedProps.albumName = this.props.albumName ? this.props.albumName : ALL_PHOTOS;
|
transformedProps.albumName = this.props.albumName ? this.props.albumName : ALL_PHOTOS;
|
||||||
|
|
||||||
return <GalleryView {...transformedProps}/>
|
return <GalleryView {...transformedProps}/>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onTapImage(event) {
|
||||||
|
if(this.props.onTapImage) {
|
||||||
|
this.props.onTapImage(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//async getSelectedImages() {
|
//async getSelectedImages() {
|
||||||
//
|
//
|
||||||
// const selectedImages = await GalleryViewManager.getSelectedImages();
|
// const selectedImages = await GalleryViewManager.getSelectedImages();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user