Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
feat: added error types
Browse files Browse the repository at this point in the history
  • Loading branch information
killi8n committed Nov 4, 2020
1 parent a47b73a commit e236cbd
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@ class ImageSelectorModule(reactContext: ReactApplicationContext) : ReactContextB
val PICK_IMAGE_REQUEST_CODE: Int = 9898
var cameraCaptureURI: Uri? = null
var cameraCaptureFile: File? = null

object ErrorCode {
val cameraPermissionDenied: Int = 100
val libraryPermissionDenied: Int = 101
}

object ErrorMessage {
val cameraPermissionDenied: String = "CAMERA_PERMISSION_DENIED"
val libraryPermissionDenied: String = "LIBRARY_PERMISSION_DENIED"
}

}



private var globalCallback: Callback? = null
private var options: ReadableMap? = null

Expand Down Expand Up @@ -163,7 +176,7 @@ class ImageSelectorModule(reactContext: ReactApplicationContext) : ReactContextB
.setNeutralButton(cancelButtonTitle) { _, _ ->
val response = Arguments.createMap()
response.putBoolean("didCancel", true)
callback(null, response)
callback.invoke(null, response)
this.globalCallback = null
}
dialogBuilder.show()
Expand All @@ -179,9 +192,10 @@ class ImageSelectorModule(reactContext: ReactApplicationContext) : ReactContextB
} else {
this.globalCallback.let { callback ->
if (callback != null) {
val response = Arguments.createMap()
response.putBoolean("didCancel", true)
callback(null, response)
val error = Arguments.createMap()
error.putInt("code", ErrorCode.cameraPermissionDenied)
error.putString("message", ErrorMessage.cameraPermissionDenied)
callback.invoke(error)
this.globalCallback = null
}
}
Expand All @@ -194,9 +208,10 @@ class ImageSelectorModule(reactContext: ReactApplicationContext) : ReactContextB
} else {
this.globalCallback.let { callback ->
if (callback != null) {
val response = Arguments.createMap()
response.putBoolean("didCancel", true)
callback(null, response)
val error = Arguments.createMap()
error.putInt("code", ErrorCode.libraryPermissionDenied)
error.putString("message", ErrorMessage.libraryPermissionDenied)
callback.invoke(error)
this.globalCallback = null
}
}
Expand Down
4 changes: 4 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { StyleSheet, Button, SafeAreaView, Image, Text } from 'react-native';
import ImageSelector, {
ImageSelectorCallbackResponse,
ImageSelectorErrorType,
ImageSelectorOptions,
} from 'react-native-image-selector';

Expand Down Expand Up @@ -33,6 +34,9 @@ export default function App() {
ImageSelector.launchPicker(options, (error, response) => {
if (error) {
console.log(error);
if (error.code === ImageSelectorErrorType.CAMERA_PERMISSION_DENIED) {
console.error('camera permission denied');
}
return;
}
if (response) {
Expand Down
4 changes: 2 additions & 2 deletions ios/ImageSelector-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTUtils.h>
#import <React/RCTEventEmitter.h>
#import <React/RCTEventDispatcher.h>
//#import <React/RCTEventEmitter.h>
//#import <React/RCTEventDispatcher.h>
7 changes: 4 additions & 3 deletions ios/ImageSelector.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
//#import <React/RCTEventEmitter.h>

@interface RCT_EXTERN_MODULE(ImageSelector, RCTEventEmitter)
//@interface RCT_EXTERN_MODULE(ImageSelector, RCTEventEmitter)
@interface RCT_EXTERN_MODULE(ImageSelector, NSObject)

RCT_EXTERN_METHOD(launchPicker: (NSDictionary)options responseCallback: (RCTResponseSenderBlock)callback)

RCT_EXTERN_METHOD(supportedEvents)
//RCT_EXTERN_METHOD(supportedEvents)

+ (BOOL)requiresMainQueueSetup {
return YES;
Expand Down
91 changes: 54 additions & 37 deletions ios/ImageSelector.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
import AVKit
import Photos

class EventEmitter {
//class EventEmitter {
//
// /// Shared Instance.
// public static var sharedInstance = EventEmitter()
//
// // ReactNativeEventEmitter is instantiated by React Native with the bridge.
// private static var eventEmitter: RCTEventEmitter!
//
// private init() {}
//
// // When React Native instantiates the emitter it is registered here.
// func registerEventEmitter(eventEmitter: RCTEventEmitter) {
// EventEmitter.eventEmitter = eventEmitter
// }
//
// func dispatch(name: String, body: Any?) {
// EventEmitter.eventEmitter.sendEvent(withName: name, body: body)
// }
//
// /// All Events which must be support by React Native.
// lazy var allEvents: [String] = {
// var allEventNames: [String] = ["DidSelectItem"]
//
// // Append all events here
//
// return allEventNames
// }()
//
//}

/// Shared Instance.
public static var sharedInstance = EventEmitter()

// ReactNativeEventEmitter is instantiated by React Native with the bridge.
private static var eventEmitter: RCTEventEmitter!

private init() {}

// When React Native instantiates the emitter it is registered here.
func registerEventEmitter(eventEmitter: RCTEventEmitter) {
EventEmitter.eventEmitter = eventEmitter
}

func dispatch(name: String, body: Any?) {
EventEmitter.eventEmitter.sendEvent(withName: name, body: body)
}

/// All Events which must be support by React Native.
lazy var allEvents: [String] = {
var allEventNames: [String] = ["DidSelectItem"]

// Append all events here

return allEventNames
}()
struct ErrorCode {
static let cameraPermissionDenied: Int = 100
static let libraryPermissionDenied: Int = 101
static let simulatorError: Int = 102
static let sourceTypeMismatch: Int = 103
static let fileCreateError: Int = 104
}

struct ErrorMessage {
static let cameraPermissionDenied: String = "CAMERA_PERMISSION_DENIED"
static let libraryPermissionDenied: String = "LIBRARY_PERMISSION_DENIED"
static let simulatorError: String = "SIMULATOR_ERROR"
static let sourceTypeMismatch: String = "SOURCE_TYPE_MISMATCH"
static let fileCreateError: String = "FILE_CREATE_ERROR"
}

@objc(ImageSelector)
class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {
class ImageSelector: NSObject, UINavigationControllerDelegate {

let imagePickerController: UIImagePickerController = UIImagePickerController()

Expand All @@ -43,12 +59,12 @@ class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {

override init() {
super.init()
EventEmitter.sharedInstance.registerEventEmitter(eventEmitter: self)
// EventEmitter.sharedInstance.registerEventEmitter(eventEmitter: self)
}

override func supportedEvents() -> [String]! {
return EventEmitter.sharedInstance.allEvents
}
// override func supportedEvents() -> [String]! {
// return EventEmitter.sharedInstance.allEvents
// }

func requestCameraAuthorization() -> Void {
AVCaptureDevice.requestAccess(for: .video) { [weak self] (isAuthorized: Bool) in
Expand All @@ -57,7 +73,7 @@ class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {
if isAuthorized {
self.launchCamera()
} else {
let error = ["error": "CAMERA_PERMISSION_DENIED"]
let error: [String: Any] = ["code": ErrorCode.cameraPermissionDenied, "message": ErrorMessage.cameraPermissionDenied]
callback([
error
])
Expand All @@ -75,7 +91,7 @@ class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {
self.launchLibrary()
break
default:
let error = ["error": "LIBRARY_PERMISSION_DENIED"]
let error: [String: Any] = ["code": ErrorCode.libraryPermissionDenied, "message": ErrorMessage.libraryPermissionDenied]
callback([
error
])
Expand All @@ -96,7 +112,7 @@ class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {
self.requestCameraAuthorization()
break
default:
let error = ["error": "CAMERA_PERMISSION_DENIED"]
let error: [String: Any] = ["code": ErrorCode.cameraPermissionDenied, "message": ErrorMessage.cameraPermissionDenied]
callback([
error
])
Expand All @@ -116,7 +132,7 @@ class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {
self.requestLibraryAuthorization()
break
default:
let error = ["error": "LIBRARY_PERMISSION_DENIED"]
let error: [String: Any] = ["code": ErrorCode.libraryPermissionDenied, "message": ErrorMessage.libraryPermissionDenied]
callback([
error
])
Expand All @@ -129,6 +145,7 @@ class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {
guard let callback = self.globalCallback else { return }
let fetchOptions = PHFetchOptions()
fetchOptions.includeAssetSourceTypes = .typeUserLibrary
fetchOptions.sortDescriptors = [.init(key: "creationDate", ascending: false)]
self.fetchedAssets = PHAsset.fetchAssets(with: .image, options: fetchOptions)
self.imageShowerViewController = ImageShowerViewController(fetchedAssets: self.fetchedAssets, options: self.options, callback: callback)
guard let imageShowerViewController = self.imageShowerViewController else { return }
Expand Down Expand Up @@ -185,7 +202,7 @@ class ImageSelector: RCTEventEmitter, UINavigationControllerDelegate {
guard let `self` = self else { return }
#if targetEnvironment(simulator)
callback([
["error": "SIMULATOR_ERROR"]
["code": ErrorCode.simulatorError, "message": ErrorMessage.simulatorError]
])
self.globalCallback = nil
#else
Expand Down Expand Up @@ -250,7 +267,7 @@ extension ImageSelector: UIImagePickerControllerDelegate {
picker.dismiss(animated: true) {
guard let response = response else {
callback([
["error": "SOURCE_TYPE_MISMATCH"]
["code": ErrorCode.sourceTypeMismatch, "message": ErrorMessage.sourceTypeMismatch]
])
self.globalCallback = nil
return
Expand Down
2 changes: 1 addition & 1 deletion ios/ImageUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ImageUtil: NSObject {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
} catch {
if let callback = callback {
let callbackResponse: [[String: Any]?] = [["error": "FILE_CREATE_ERROR"]]
let callbackResponse: [[String: Any]?] = [["code": ErrorCode.fileCreateError, "message": ErrorMessage.fileCreateError]]
callback(callbackResponse as [Any])
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-image-selector",
"version": "0.1.4",
"version": "0.1.5",
"description": "image picker native module",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
15 changes: 14 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,24 @@ export interface ImageSelectorOptions {
};
}

interface ImageSelectorError {
code: number;
message: string;
}

export enum ImageSelectorErrorType {
CAMERA_PERMISSION_DENIED = 100,
LIBRARY_PERMISSION_DENIED = 101,
SIMULATOR_ERROR = 102,
SOURCE_TYPE_MISMATCH = 103,
FILE_CREATE_ERROR = 104,
}

type ImageSelectorType = {
launchPicker: (
options: ImageSelectorOptions,
callback: (
error: { error: string },
error: ImageSelectorError,
response?: ImageSelectorCallbackResponse
) => void
) => void;
Expand Down

0 comments on commit e236cbd

Please sign in to comment.