From fd901d3bbec92c53e58668aad380b994ceb5939a Mon Sep 17 00:00:00 2001 From: Amit Davidi Date: Tue, 28 Feb 2017 15:00:21 +0200 Subject: [PATCH] Android: Add support for custom-button on-press JS callback + refactoring and cleanup --- .../RNCameraKit/gallery/GalleryAdapter.java | 126 ++++++++++-------- .../wix/RNCameraKit/gallery/GalleryView.java | 79 ----------- .../gallery/GalleryViewManager.java | 65 ++++----- .../gallery/TapCustomButtonEvent.java | 29 ++++ .../RNCameraKit/gallery/TapImageEvent.java | 31 +++++ src/CameraKitGalleryView.android.js | 4 +- 6 files changed, 169 insertions(+), 165 deletions(-) create mode 100644 android/src/main/java/com/wix/RNCameraKit/gallery/TapCustomButtonEvent.java create mode 100644 android/src/main/java/com/wix/RNCameraKit/gallery/TapImageEvent.java diff --git a/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryAdapter.java b/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryAdapter.java index 5f44836..5713f81 100644 --- a/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryAdapter.java +++ b/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryAdapter.java @@ -9,6 +9,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import com.facebook.react.bridge.ReactContext; +import com.facebook.react.uimanager.UIManagerModule; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -23,6 +26,24 @@ public class GalleryAdapter extends RecyclerView.Adapter dirtyUris = new ArrayList<>(); - private Drawable customButtonImage; - private String customButtonBackgroundColor = DEFAULT_CUSTOM_BUTTON_BACKGROUND_COLOR; - - private class Image { - String uri; - Integer id; - String mimeType; - - public Image(String uri, Integer id, String mimeType) { - this.uri = uri; - this.id = id; - this.mimeType = mimeType; - } - } - - public static final String[] PROJECTION = new String[]{ - MediaStore.Images.Media.DATA, - MediaStore.Images.Media._ID, - MediaStore.Images.Media.MIME_TYPE - }; - - private ArrayList images = new ArrayList<>(); - private ArrayList selectedUris = new ArrayList<>(); private ArrayList supportedFileTypes = new ArrayList<>(); private String albumName = ""; private Drawable selectedDrawable; private Drawable unselectedDrawable; + private Drawable customButtonImage; + private String customButtonBackgroundColor = DEFAULT_CUSTOM_BUTTON_BACKGROUND_COLOR; + + private ArrayList images = new ArrayList<>(); private boolean refreshing = false; + private GalleryView view; + private ThreadPoolExecutor executor; + + public GalleryAdapter(GalleryView view) { + this.view = view; + setHasStableIds(true); + int cores = Runtime.getRuntime().availableProcessors(); + executor = new ThreadPoolExecutor(cores, cores, 1, TimeUnit.SECONDS, new LinkedBlockingDeque()); + setAlbum(albumName); + } + + public void setAlbum(String albumName) { + this.albumName = albumName; + } public void setSelectedUris(ArrayList selectedUris) { this.selectedUris = selectedUris; @@ -162,6 +186,13 @@ public class GalleryAdapter extends RecyclerView.Adapter()); - setAlbum(albumName); - } - - public void setAlbum(String albumName) { - this.albumName = albumName; - } - @Override public int getItemViewType(int position) { if (shouldShowCustomButton() && position == 0) { @@ -265,14 +281,6 @@ public class GalleryAdapter extends RecyclerView.Adapter selectedUris) { - adapter.setSelectedUris(selectedUris); - } - - public void setDirtyImages(ArrayList dirtyUris) { - adapter.setDirtyUris(dirtyUris); - } - - public void setSelectedDrawable(Drawable drawable) { - adapter.setSelectedDrawable(drawable); - } - - public void setUnselectedDrawable(Drawable drawable) { - adapter.setUnselectedDrawable(drawable); - } - - public void setUnsupportedUIParams(String overlayColor, Drawable unsupportedFinalImage, String unsupportedText, String unsupportedTextColor) { - adapter.setUnsupportedUIParams(overlayColor, unsupportedFinalImage, unsupportedText, unsupportedTextColor); - } - - public void setCustomButtonImage(Drawable customButtonImage) { - adapter.setCustomButtonImage(customButtonImage); - } - - public void setCustomButtonBackgroundColor(String color) { - adapter.setCustomButtonBackgroundColor(color); - } - - public void refresh() { - adapter.refreshData(); - } - - private class TapImageEvent extends Event { - - private WritableMap event; - - TapImageEvent(String uri) { - event = Arguments.createMap(); - event.putString("selected", uri); - event.putString("id", "onTapImage"); - init(0); - } - - @Override - public String getEventName() { - return "onTapImage"; - } - - @Override - public void dispatch(RCTEventEmitter rctEventEmitter) { - rctEventEmitter.receiveEvent(getId(), "onTapImage", event); - } - } - - public void setSupportedFileTypes(ArrayList supportedFileTypes) { - adapter.setSupportedFileTypes(supportedFileTypes); - } } diff --git a/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryViewManager.java b/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryViewManager.java index 7cd741e..e275d94 100644 --- a/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryViewManager.java +++ b/android/src/main/java/com/wix/RNCameraKit/gallery/GalleryViewManager.java @@ -24,7 +24,7 @@ public class GalleryViewManager extends SimpleViewManager { private final String UNSUPPORTED_TEXT_COLOR_KEY = "unsupportedTextColor"; private final String SUPPORTED_TYPES_KEY = "supportedFileTypes"; private final String UNSUPPORTED_OVERLAY_KEY = "unsupportedOverlayColor"; - private final String CUSTOM_BUTTON_IMAGE_KEY = "customImage"; + private final String CUSTOM_BUTTON_IMAGE_KEY = "image"; private final String CUSTOM_BUTTON_BCK_COLOR_KEY = "backgroundColor"; private ThemedReactContext reactContext; @@ -37,17 +37,15 @@ public class GalleryViewManager extends SimpleViewManager { @Override protected GalleryView createViewInstance(ThemedReactContext reactContext) { this.reactContext = reactContext; - return new GalleryView(reactContext); + + GalleryView view = new GalleryView(reactContext); + view.setAdapter(new GalleryAdapter(view)); + return view; } @Override protected void onAfterUpdateTransaction(GalleryView view) { - view.refresh(); - } - - @ReactProp(name = "albumName") - public void setAlbumName(GalleryView view, String albumName) { - view.setAlbumName(albumName); + getViewAdapter(view).refreshData(); } @ReactProp(name = "minimumInteritemSpacing") @@ -60,6 +58,11 @@ public class GalleryViewManager extends SimpleViewManager { view.setLineSpacing(lineSpacing/2); } + @ReactProp(name = "albumName") + public void setAlbumName(GalleryView view, String albumName) { + getViewAdapter(view).setAlbum(albumName); + } + @ReactProp(name = "columnCount") public void setColumnCount(GalleryView view, int columnCount) { view.setColumnCount(columnCount); @@ -67,12 +70,12 @@ public class GalleryViewManager extends SimpleViewManager { @ReactProp(name = "selectedImages") public void setSelectedUris(GalleryView view, ReadableArray uris) { - view.setSelectedUris(readableArrayToList(uris)); + getViewAdapter(view).setSelectedUris(readableArrayToList(uris)); } @ReactProp(name = "dirtyImages") public void setDirtyImages(GalleryView view, final ReadableArray uris) { - view.setDirtyImages(readableArrayToList(uris)); + getViewAdapter(view).setDirtyUris(readableArrayToList(uris)); } @ReactProp(name = "selectedImageIcon") @@ -84,7 +87,7 @@ public class GalleryViewManager extends SimpleViewManager { reactContext.runOnUiQueueThread(new Runnable() { @Override public void run() { - view.setSelectedDrawable(drawable); + getViewAdapter(view).setSelectedDrawable(drawable); } }); } @@ -100,21 +103,13 @@ public class GalleryViewManager extends SimpleViewManager { reactContext.runOnUiQueueThread(new Runnable() { @Override public void run() { - view.setUnselectedDrawable(drawable); + getViewAdapter(view).setUnselectedDrawable(drawable); } }); } }).start(); } -// fileTypeSupport={{ -// supportedFileTypes: ['image/jpeg', 'image/png'], -// unsupportedOverlayColor: "#00000055", -// unsupportedImage: require('./images/unsupportedImage.png'), -// unsupportedText: 'Unsupported', -// unsupportedTextColor: '#ffffff' -// }} - @ReactProp(name = "fileTypeSupport") public void setFileTypeSupport(final GalleryView view, final ReadableMap fileTypeSupport) { final ReadableArray supportedFileTypes = fileTypeSupport.getArray(SUPPORTED_TYPES_KEY); @@ -141,19 +136,20 @@ public class GalleryViewManager extends SimpleViewManager { reactContext.runOnUiQueueThread(new Runnable() { @Override public void run() { - view.setUnsupportedUIParams( - unsupportedOverlayColor, - unsupportedFinalImage, - unsupportedText, - unsupportedTextColor); - view.setSupportedFileTypes(supportedFileTypesList); + getViewAdapter(view) + .setUnsupportedUIParams( + unsupportedOverlayColor, + unsupportedFinalImage, + unsupportedText, + unsupportedTextColor); + getViewAdapter(view).setSupportedFileTypes(supportedFileTypesList); } }); } }).start(); } - @ReactProp(name = "customButton") + @ReactProp(name = "customButtonStyle") public void setCustomButton(final GalleryView view, final ReadableMap props) { new Thread(new Runnable() { @Override @@ -164,9 +160,9 @@ public class GalleryViewManager extends SimpleViewManager { reactContext.runOnUiQueueThread(new Runnable() { @Override public void run() { - view.setCustomButtonImage(drawable); + getViewAdapter(view).setCustomButtonImage(drawable); if (backgroundColor != null) { - view.setCustomButtonBackgroundColor(backgroundColor); + getViewAdapter(view).setCustomButtonBackgroundColor(backgroundColor); } } }); @@ -179,6 +175,7 @@ public class GalleryViewManager extends SimpleViewManager { public Map getExportedCustomDirectEventTypeConstants() { return MapBuilder.builder() .put("onTapImage", MapBuilder.of("registrationName", "onTapImage")) + .put("onCustomButtonPress", MapBuilder.of("registrationName", "onCustomButtonPress")) .build(); } @@ -189,14 +186,14 @@ public class GalleryViewManager extends SimpleViewManager { } @Override - public void receiveCommand(GalleryView root, int commandId, @Nullable ReadableArray args) { + public void receiveCommand(GalleryView view, int commandId, @Nullable ReadableArray args) { if (commandId == COMMAND_REFRESH_GALLERY) { - root.refresh(); + getViewAdapter(view).refreshData(); } } private @Nullable String getStringSafe(ReadableMap map, String key) { - if(map.hasKey(key)) { + if (map.hasKey(key)) { return map.getString(key); } return null; @@ -209,4 +206,8 @@ public class GalleryViewManager extends SimpleViewManager { } return list; } + + private GalleryAdapter getViewAdapter(GalleryView view) { + return ((GalleryAdapter) view.getAdapter()); + } } diff --git a/android/src/main/java/com/wix/RNCameraKit/gallery/TapCustomButtonEvent.java b/android/src/main/java/com/wix/RNCameraKit/gallery/TapCustomButtonEvent.java new file mode 100644 index 0000000..8a1a565 --- /dev/null +++ b/android/src/main/java/com/wix/RNCameraKit/gallery/TapCustomButtonEvent.java @@ -0,0 +1,29 @@ +package com.wix.RNCameraKit.gallery; + +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.uimanager.events.Event; +import com.facebook.react.uimanager.events.RCTEventEmitter; + +public class TapCustomButtonEvent extends Event { + + private final int targetTag; + private WritableMap event; + + TapCustomButtonEvent(int targetTag) { + this.targetTag = targetTag; + event = Arguments.createMap(); + event.putString("id", "onCustomButtonPress"); + init(0); + } + + @Override + public String getEventName() { + return "onCustomButtonPress"; + } + + @Override + public void dispatch(RCTEventEmitter rctEventEmitter) { + rctEventEmitter.receiveEvent(targetTag, "onCustomButtonPress", event); + } +} diff --git a/android/src/main/java/com/wix/RNCameraKit/gallery/TapImageEvent.java b/android/src/main/java/com/wix/RNCameraKit/gallery/TapImageEvent.java new file mode 100644 index 0000000..5530a6a --- /dev/null +++ b/android/src/main/java/com/wix/RNCameraKit/gallery/TapImageEvent.java @@ -0,0 +1,31 @@ +package com.wix.RNCameraKit.gallery; + +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.uimanager.events.Event; +import com.facebook.react.uimanager.events.RCTEventEmitter; + +public class TapImageEvent extends Event { + + private final int targetTag; + private WritableMap event; + + TapImageEvent(int targetTag, String uri) { + this.targetTag = targetTag; + event = Arguments.createMap(); + event.putString("selected", uri); + event.putString("id", "onTapImage"); + init(0); + } + + @Override + public String getEventName() { + return "onTapImage"; + } + + @Override + public void dispatch(RCTEventEmitter rctEventEmitter) { + rctEventEmitter.receiveEvent(targetTag, "onTapImage", event); + } +} + diff --git a/src/CameraKitGalleryView.android.js b/src/CameraKitGalleryView.android.js index ef5e58b..f98893d 100644 --- a/src/CameraKitGalleryView.android.js +++ b/src/CameraKitGalleryView.android.js @@ -32,8 +32,8 @@ export default class CameraKitGalleryView extends Component { if (transformedProps.fileTypeSupport && transformedProps.fileTypeSupport.unsupportedImage) { _.update(transformedProps, 'fileTypeSupport.unsupportedImage', (image) => resolveAssetSource(image).uri); } - if (_.get(transformedProps, 'customButton.customImage')) { - _.update(transformedProps, 'customButton.customImage', (image) => resolveAssetSource(image).uri); + if (_.get(transformedProps, 'customButtonStyle.image')) { + _.update(transformedProps, 'customButtonStyle.image', (image) => resolveAssetSource(image).uri); } if (transformedProps.selectedImageIcon) { transformedProps.selectedImageIcon = resolveAssetSource(transformedProps.selectedImageIcon).uri;