From bb1b65716cead0ba9501a2937af4925b0df29906 Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Wed, 27 Jul 2016 11:44:59 -0700 Subject: [PATCH 1/9] UI revamp as well as update to code to allow unsubscribing from progress events being emitted --- .../mybigday/rns3/RNS3TransferUtility.java | 34 ++- example/app.js | 223 +++++++++--------- src/TransferUtility.js | 13 +- 3 files changed, 144 insertions(+), 126 deletions(-) diff --git a/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java b/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java index 30fda61..aa59908 100644 --- a/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java +++ b/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java @@ -39,7 +39,7 @@ public static enum CredentialType { static { // default options - nativeCredentialsOptions.put("region", "eu-west-1"); + nativeCredentialsOptions.put("region", "us-east-1"); nativeCredentialsOptions.put("cognito_region", "eu-west-1"); } @@ -47,6 +47,7 @@ public static enum CredentialType { private Context context; private AmazonS3 s3; private TransferUtility transferUtility; + private Boolean subscribeProgress; public RNS3TransferUtility(ReactApplicationContext reactContext) { super(reactContext); @@ -94,19 +95,25 @@ public void onStateChanged(int id, TransferState state) { TransferObserver task = transferUtility.getTransferById(id); WritableMap result = Arguments.createMap(); result.putMap("task", convertTransferObserver(task)); - sendEvent("@_RNS3_Events", result); + getReactApplicationContext() + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("State_Changed", result); } @Override public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { - TransferObserver task = transferUtility.getTransferById(id); - WritableMap result = Arguments.createMap(); - WritableMap taskMap = convertTransferObserver(task); - if (taskMap.getDouble("bytes") <= bytesTotal) { - taskMap.putDouble("bytes", bytesCurrent); - } - result.putMap("task", taskMap); - sendEvent("@_RNS3_Events", result); + if (subscribeProgress == true) { + TransferObserver task = transferUtility.getTransferById(id); + WritableMap result = Arguments.createMap(); + WritableMap taskMap = convertTransferObserver(task); + if (taskMap.getDouble("bytes") <= bytesTotal) { + taskMap.putDouble("bytes", bytesCurrent); + } + result.putMap("task", taskMap); + getReactApplicationContext() + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("Progress_Changed", result); + } } @Override @@ -115,7 +122,9 @@ public void onError(int id, Exception ex) { WritableMap result = Arguments.createMap(); result.putMap("task", convertTransferObserver(task)); result.putString("error", ex.getMessage()); - sendEvent("@_RNS3_Events", result); + getReactApplicationContext() + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("Error", result); } }); } @@ -182,9 +191,10 @@ private boolean setup(Map credentialsOptions) { } @ReactMethod - public void initializeRNS3() { + public void initializeRNS3(Boolean subscribeProgress) { if (alreadyInitialize) return; alreadyInitialize = true; + this.subscribeProgress = subscribeProgress; subscribeList(transferUtility.getTransfersWithType(TransferType.UPLOAD)); subscribeList(transferUtility.getTransfersWithType(TransferType.DOWNLOAD)); } diff --git a/example/app.js b/example/app.js index 79a4a76..c4cbb30 100644 --- a/example/app.js +++ b/example/app.js @@ -5,68 +5,63 @@ import React, { Text, View, ScrollView, - TouchableHighlight + TouchableHighlight, + DeviceEventEmitter } from "react-native"; import { transferUtility } from "react-native-s3"; import fs from "react-native-fs"; -console.log(fs.DocumentDirectoryPath); +const bucketName = "database-versame"; // name of bucket +const uploadFileKey = "ReactNativeTest/test.mp4"; // path to file in s3, excluding bucket +const contentType = "image/jpeg"; // type of file +const uploadFilePath = fs.DocumentDirectoryPath + "/test.mp4"; // file to be uploaded +const downloadFileKey = "ReactNativeTest/hello_world.png"; // path to file in s3, excluding bucket +const downloadFilePath = fs.DocumentDirectoryPath + "/blah.png"; // path to where file should be downloaded to -const bucketName = ""; -const uploadFileKey = "test.mp4"; -const contentType = "image/jpeg"; -const uploadFilePath = fs.DocumentDirectoryPath + "/test.mp4"; -const downloadFileKey = "test.mp4"; -const downloadFilePath = fs.DocumentDirectoryPath + "/test_download.mp4"; +const subscribeProgress = true; // Change to false if you don't want to subscribe to progress events +var transferAction = ""; -const sampleVideoURL = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"; -const styles = StyleSheet.create({ - container: { - flex: 1, - marginTop: 20, - backgroundColor: "#F5FCFF" - }, - title: { - fontSize: 20, - textAlign: "center", - margin: 10 - }, - task: { - flexDirection: "row", - justifyContent: "center" - }, - text: { - fontSize: 12, - textAlign: "center", - margin: 10 - }, - btn: { - fontSize: 15, - textAlign: "center", - margin: 10 - } -}); +// aws access keys and region +const options = { + "access_key": "xxxx", + "secret_key": "xxxxxxxxxxxxx", + "region": "us-east-1", +} + +const sampleVideoURL = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"; class S3Sample extends Component { constructor(props) { super(props); this.state = { - initLoaded: false + initLoaded: false, + logText: "", }; } async componentDidMount() { if (!this.state.initLoaded) { if (!await fs.exists(uploadFilePath)) { - await fs.downloadFile(sampleVideoURL, uploadFilePath); + await fs.downloadFile(sampleVideoURL, uploadFilePath).then(res => { + fs.readDir(fs.DocumentDirectoryPath) + .then((result) => { + // Confirm that the file was written + }); + }); } - await transferUtility.setupWithNative(); + + // Set up with basic options set, or with options set in the native code + await transferUtility.setupWithBasic(options, subscribeProgress); const uploadTasks = await transferUtility.getTasks("upload", true); const downloadTasks = await transferUtility.getTasks("download", true); + DeviceEventEmitter.addListener('State_Changed', result => this.handleEvent('State_Changed', result)); + DeviceEventEmitter.addListener('Progress_Changed', result => this.handleEvent('Progress_Changed', result)); + DeviceEventEmitter.addListener('Error', result => this.handleEvent('Error', result)); + for (const id in uploadTasks) { this.subscribeWithUpdateState(id, "uploadTasks"); } @@ -74,10 +69,31 @@ class S3Sample extends Component { this.subscribeWithUpdateState(id, "downloadTasks"); } - this.setState({ initLoaded: true, uploadTasks, downloadTasks }); + this.setState({ initLoaded: true, logText: "Press Download or Upload to begin \n" }); } } + handleEvent = (eventType, result) => { + switch(eventType) { + case 'State_Changed': + if (result.task.state == 'completed' && transferAction == 'Download') { + this.setState({ logText: `${ this.state.logText }State_Changed: ${ result.task.state } \nDownload complete \nFile location: ${ fs.DocumentDirectoryPath }` }); + } else if (result.task.state == 'completed' && transferAction == 'Upload') { + this.setState({ logText: `${ this.state.logText }State_Changed: ${ result.task.state } \nUpload complete \ns3 file location: ${ bucketName }/${ uploadFileKey }` }); + } else { this.setState({ logText: `${ this.state.logText }State_Changed: ${ result.task.state } \n` }); } + break; + case 'Progress_Changed': + this.setState({ logText: `${ this.state.logText }Progress_Changed: ${ result.task.bytes/result.task.totalBytes * 100 }% \n` }); + break; + case 'Error': + this.setState({ logText: `${ this.state.logText }Error: ${ result.error } \n\n` }); + break; + default: + console.warn("Receiving event that doesn't match case"); + break; + } + }; + subscribeWithUpdateState = (id, typeKey) => { transferUtility.subscribe(id, (err, task) => { if (err) task.errMessage = err; @@ -91,6 +107,7 @@ class S3Sample extends Component { }; handleUploadFile = async () => { + transferAction = "Upload"; const task = await transferUtility.upload({ bucket: bucketName, key: uploadFileKey, @@ -109,6 +126,7 @@ class S3Sample extends Component { }; handleDownloadFile = async () => { + transferAction = "Download"; const task = await transferUtility.download({ bucket: bucketName, key: downloadFileKey, @@ -146,81 +164,70 @@ class S3Sample extends Component { transferUtility.resume(id); } - renderTasks(tasks) { - return Object.keys(tasks).map(id => { - let progress; - if (tasks[id].totalBytes) { - progress = {(tasks[id].bytes / tasks[id].totalBytes) * 100 + "%"}; - } - return ( - - {id} - {tasks[id].state} - {progress} - - ); - }); - } - - renderUploadTask() { - const { uploadTasks } = this.state; - return ( - - {"Upload Tasks"} - {this.renderTasks(uploadTasks)} - - {"New Upload"} - - - ); - } - - renderDownloadTask() { - const { downloadTasks } = this.state; + render() { return ( - - {"Download Tasks"} - {this.renderTasks(downloadTasks)} + - {"New Download"} + Download Designated File - - ); - } - - renderLoading() { - return ( - - {"Loading..."} - - ); - } - - render() { - return ( - - - { - (() => { - if (!this.state.initLoaded) { - return this.renderLoading(); - } else { - return ( - - {this.renderUploadTask()} - {this.renderDownloadTask()} - - ); - } - })() - } - + + Upload Designated File + + { this.refs.scrollView.scrollTo({ y: height }) } }> + + { this.state.logText } + + ); } } +const styles = StyleSheet.create({ + container: { + flex: 1, + marginTop: 20, + alignItems: 'center', + backgroundColor: "#F5FCFF" + }, + title: { + fontSize: 20, + textAlign: "center", + margin: 10 + }, + task: { + flexDirection: "row", + justifyContent: "center" + }, + logText: { + alignItems: 'center', + paddingBottom: 20, + }, + text: { + fontSize: 12, + textAlign: "center", + margin: 10 + }, + btn: { + fontSize: 15, + textAlign: "center", + margin: 10 + }, + logContainer: { + flex: 1, + width: 350, + marginBottom: 10, + borderWidth: 2, + borderRadius: 5, + borderColor: 'black', + paddingHorizontal: 10, + borderStyle: 'solid', + backgroundColor: 'lavender', + }, +}); + AppRegistry.registerComponent("S3Sample", () => S3Sample); diff --git a/src/TransferUtility.js b/src/TransferUtility.js index b87653a..ff0cbf9 100644 --- a/src/TransferUtility.js +++ b/src/TransferUtility.js @@ -81,16 +81,17 @@ async function setTaskExtra(task, values, isNew) { } export default class TransferUtility { - async setupWithNative() { + async setupWithNative(subscribeProgress = true) { const result = await RNS3TransferUtility.setupWithNative(); if (result) { await getTaskExtras(); - RNS3TransferUtility.initializeRNS3(); + RNS3TransferUtility.initializeRNS3(subscribeProgress); } return result; } - async setupWithBasic(options = {}) { + async setupWithBasic(options = {}, subscribeProgress = true) { + console.log(subscribeProgress); if (!options.access_key || !options.secret_key) { return false; } @@ -100,19 +101,19 @@ export default class TransferUtility { const result = await RNS3TransferUtility.setupWithBasic({ ...defaultOptions, ...options}); if (result) { await getTaskExtras(); - RNS3TransferUtility.initializeRNS3(); + RNS3TransferUtility.initializeRNS3(subscribeProgress); } return result; } - async setupWithCognito(options = {}) { + async setupWithCognito(options = {}, subscribeProgress = true) { if (!options.identity_pool_id) { return false; } const result = await RNS3TransferUtility.setupWithBasic({ ...defaultCognitoOptions, ...options }); if (result) { await getTaskExtras(); - RNS3TransferUtility.initializeRNS3(); + RNS3TransferUtility.initializeRNS3(subscribeProgress); } return result; } From 2cd2a6ff37dc382d70a29b9211a467a87c7dc5f5 Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Wed, 27 Jul 2016 11:46:55 -0700 Subject: [PATCH 2/9] Update --- example/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/app.js b/example/app.js index c4cbb30..d85546d 100644 --- a/example/app.js +++ b/example/app.js @@ -11,7 +11,7 @@ import React, { import { transferUtility } from "react-native-s3"; import fs from "react-native-fs"; -const bucketName = "database-versame"; // name of bucket +const bucketName = ""; // name of bucket const uploadFileKey = "ReactNativeTest/test.mp4"; // path to file in s3, excluding bucket const contentType = "image/jpeg"; // type of file const uploadFilePath = fs.DocumentDirectoryPath + "/test.mp4"; // file to be uploaded From 54070a2ee762b5a36ae5ab82216f07845764c0c2 Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Wed, 27 Jul 2016 11:53:11 -0700 Subject: [PATCH 3/9] Removed console.log --- src/TransferUtility.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TransferUtility.js b/src/TransferUtility.js index ff0cbf9..4277d9d 100644 --- a/src/TransferUtility.js +++ b/src/TransferUtility.js @@ -91,7 +91,6 @@ export default class TransferUtility { } async setupWithBasic(options = {}, subscribeProgress = true) { - console.log(subscribeProgress); if (!options.access_key || !options.secret_key) { return false; } From 8b001f7f0b602358ba77fbfcf543722fd56181eb Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Wed, 27 Jul 2016 16:04:36 -0700 Subject: [PATCH 4/9] Updated transfer utility for iOS progress disable --- ios/RNS3/RNS3TransferUtility.m | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/ios/RNS3/RNS3TransferUtility.m b/ios/RNS3/RNS3TransferUtility.m index 077022f..c144442 100644 --- a/ios/RNS3/RNS3TransferUtility.m +++ b/ios/RNS3/RNS3TransferUtility.m @@ -3,6 +3,7 @@ static NSMutableDictionary *nativeCredentialsOptions; static bool alreadyInitialize = false; +static bool subscribeProgress; @interface RNS3TransferUtility () @@ -156,17 +157,20 @@ - (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state: }]; } -RCT_EXPORT_METHOD(initializeRNS3) { +RCT_EXPORT_METHOD(initializeRNS3: (bool)subscribeProgressValue) { if (alreadyInitialize) return; alreadyInitialize = true; + subscribeProgress = subscribeProgressValue; self.uploadProgress = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) { NSLog(@"update"); - [self sendEvent:task - type:@"upload" - state:@"in_progress" - bytes:progress.completedUnitCount - totalBytes:progress.totalUnitCount - error:nil]; + if (subscribeProgress == true) { + [self sendEvent:task + type:@"upload" + state:@"in_progress" + bytes:progress.completedUnitCount + totalBytes:progress.totalUnitCount + error:nil]; + } }; self.completionUploadHandler = ^(AWSS3TransferUtilityUploadTask *task, NSError *error) { NSString *state; @@ -180,12 +184,14 @@ - (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state: }; self.downloadProgress = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) { - [self sendEvent:task - type:@"download" - state:@"in_progress" - bytes:progress.completedUnitCount - totalBytes:progress.totalUnitCount - error:nil]; + if (subscribeProgress == true) { + [self sendEvent:task + type:@"download" + state:@"in_progress" + bytes:progress.completedUnitCount + totalBytes:progress.totalUnitCount + error:nil]; + } }; self.completionDownloadHandler = ^(AWSS3TransferUtilityDownloadTask *task, NSURL *location, NSData *data, NSError *error) { NSString *state; From 450bc37d3c62cf66bd9f5b0634a37d6241e0725c Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Thu, 28 Jul 2016 10:18:28 -0700 Subject: [PATCH 5/9] Went back to sendEvents and designated 3 different event labels --- .../mybigday/rns3/RNS3TransferUtility.java | 19 +++---- example/app.js | 51 +++++++++++-------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java b/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java index aa59908..e0940be 100644 --- a/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java +++ b/android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java @@ -47,7 +47,7 @@ public static enum CredentialType { private Context context; private AmazonS3 s3; private TransferUtility transferUtility; - private Boolean subscribeProgress; + private Boolean subscribeProgress = true; public RNS3TransferUtility(ReactApplicationContext reactContext) { super(reactContext); @@ -95,9 +95,7 @@ public void onStateChanged(int id, TransferState state) { TransferObserver task = transferUtility.getTransferById(id); WritableMap result = Arguments.createMap(); result.putMap("task", convertTransferObserver(task)); - getReactApplicationContext() - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("State_Changed", result); + sendEvent("@_RNS3_State_Changed", result); } @Override @@ -110,9 +108,7 @@ public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { taskMap.putDouble("bytes", bytesCurrent); } result.putMap("task", taskMap); - getReactApplicationContext() - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("Progress_Changed", result); + sendEvent("@_RNS3_Progress_Changed", result); } } @@ -122,9 +118,7 @@ public void onError(int id, Exception ex) { WritableMap result = Arguments.createMap(); result.putMap("task", convertTransferObserver(task)); result.putString("error", ex.getMessage()); - getReactApplicationContext() - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("Error", result); + sendEvent("@_RNS3_Error", result); } }); } @@ -160,14 +154,14 @@ private boolean setup(Map credentialsOptions) { } break; // TODO: support accountId, unauthRoleArn, authRoleArn - case COGNITO: + case COGNITO: String cognitoRegion = (String) credentialsOptions.get("cognito_region"); if (!(Boolean) credentialsOptions.get("caching")) { credentialsProvider = new CognitoCredentialsProvider( (String) credentialsOptions.get("identity_pool_id"), Regions.fromName(cognitoRegion) ); - } else { + } else { credentialsProvider = new CognitoCachingCredentialsProvider( context, (String) credentialsOptions.get("identity_pool_id"), @@ -262,7 +256,6 @@ public void download(ReadableMap options, Promise promise) { String bucket = options.getString("bucket"); String key = options.getString("key"); File file = new File(options.getString("file")); - TransferObserver task = transferUtility.download(bucket, key, file); subscribe(task); promise.resolve(convertTransferObserver(task)); diff --git a/example/app.js b/example/app.js index d85546d..ad3f0e9 100644 --- a/example/app.js +++ b/example/app.js @@ -23,7 +23,14 @@ var transferAction = ""; // aws access keys and region -const options = { +const cognitoOptions = { + "region": "us-east-1", + "identity_pool_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "cognito_region": "us-east-1", + "caching": true +} + +const keySecretOptions = { "access_key": "xxxx", "secret_key": "xxxxxxxxxxxxx", "region": "us-east-1", @@ -52,16 +59,15 @@ class S3Sample extends Component { }); } - // Set up with basic options set, or with options set in the native code - await transferUtility.setupWithBasic(options, subscribeProgress); + // Set up with cognito options + await transferUtility.setupWithCognito(cognitoOptions, subscribeProgress); + + // Set up with basic (standard) options + // await transferUtility.setupWithBasic(keySecretOptions, subscribeProgress); const uploadTasks = await transferUtility.getTasks("upload", true); const downloadTasks = await transferUtility.getTasks("download", true); - DeviceEventEmitter.addListener('State_Changed', result => this.handleEvent('State_Changed', result)); - DeviceEventEmitter.addListener('Progress_Changed', result => this.handleEvent('Progress_Changed', result)); - DeviceEventEmitter.addListener('Error', result => this.handleEvent('Error', result)); - for (const id in uploadTasks) { this.subscribeWithUpdateState(id, "uploadTasks"); } @@ -73,20 +79,20 @@ class S3Sample extends Component { } } - handleEvent = (eventType, result) => { + handleEvent = (eventType, task) => { switch(eventType) { - case 'State_Changed': - if (result.task.state == 'completed' && transferAction == 'Download') { - this.setState({ logText: `${ this.state.logText }State_Changed: ${ result.task.state } \nDownload complete \nFile location: ${ fs.DocumentDirectoryPath }` }); - } else if (result.task.state == 'completed' && transferAction == 'Upload') { - this.setState({ logText: `${ this.state.logText }State_Changed: ${ result.task.state } \nUpload complete \ns3 file location: ${ bucketName }/${ uploadFileKey }` }); - } else { this.setState({ logText: `${ this.state.logText }State_Changed: ${ result.task.state } \n` }); } + case '@_RNS3_State_Changed': + if (task.state == 'completed' && transferAction == 'Download') { + this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \nDownload complete \nFile location: ${ fs.DocumentDirectoryPath }` }); + } else if (task.state == 'completed' && transferAction == 'Upload') { + this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \nUpload complete \ns3 file location: ${ bucketName }/${ uploadFileKey }` }); + } else { this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \n` }); } break; - case 'Progress_Changed': - this.setState({ logText: `${ this.state.logText }Progress_Changed: ${ result.task.bytes/result.task.totalBytes * 100 }% \n` }); + case '@_RNS3_Progress_Changed': + this.setState({ logText: `${ this.state.logText }Progress_Changed: ${ task.bytes/task.totalBytes * 100 }% \n` }); break; - case 'Error': - this.setState({ logText: `${ this.state.logText }Error: ${ result.error } \n\n` }); + case '@_RNS3_Error': + this.setState({ logText: `${ this.state.logText }Error: ${ task.errMessage } \n\n` }); break; default: console.warn("Receiving event that doesn't match case"); @@ -96,7 +102,8 @@ class S3Sample extends Component { subscribeWithUpdateState = (id, typeKey) => { transferUtility.subscribe(id, (err, task) => { - if (err) task.errMessage = err; + if (err != undefined) task.errMessage = err; + this.handleEvent(task.eventIdentifier, task) this.setState({ [typeKey]: { ...this.state[typeKey], @@ -120,7 +127,8 @@ class S3Sample extends Component { uploadTasks: { ...this.state.uploadTasks, ...{ [task.id]: task } - } + }, + logText: `${ this.state.logText }\nUpload Started To s3 Location:\n${ bucketName }/${ uploadFileKey } \n\n` }); this.subscribeWithUpdateState(task.id, "uploadTasks"); }; @@ -136,7 +144,8 @@ class S3Sample extends Component { downloadTasks: { ...this.state.downloadTasks, ...{ [task.id]: task } - } + }, + logText: `${ this.state.logText }\nDownload Started For File At s3 Location:\n${ bucketName }/${ downloadFileKey } \n\n` }); this.subscribeWithUpdateState(task.id, "downloadTasks"); }; From 128985a4d17bbb805afbf78595332a4f9b920464 Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Thu, 28 Jul 2016 10:36:43 -0700 Subject: [PATCH 6/9] First pass at iOS sendEvents update with diff event labels --- ios/RNS3/RNS3TransferUtility.m | 44 ++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/ios/RNS3/RNS3TransferUtility.m b/ios/RNS3/RNS3TransferUtility.m index c144442..ab9a5ce 100644 --- a/ios/RNS3/RNS3TransferUtility.m +++ b/ios/RNS3/RNS3TransferUtility.m @@ -131,18 +131,12 @@ - (BOOL)setup:(NSDictionary *)options { resolve(@([self setup:options])); } -- (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state:(NSString *)state bytes:(int64_t)bytes totalBytes:(int64_t)totalBytes error:(NSError *)error { +- (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state:(NSString *)state bytes:(int64_t)bytes totalBytes:(int64_t)totalBytes error:(NSError *)error label:(NSString *)label { NSDictionary *errorObj = nil; if (error) { - errorObj = @{ - @"domain":[error domain], - @"code": @([error code]), - @"description": [error localizedDescription] - }; - } - - [self.bridge.eventDispatcher - sendAppEventWithName:@"@_RNS3_Events" + errorObj = [error localizedDescription]; + [self.bridge.eventDispatcher + sendAppEventWithName:@"@_RNS3_Error" body:@{ @"task":@{ @"id":@([task taskIdentifier]), @@ -154,7 +148,23 @@ - (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state: }, @"type":type, @"error":errorObj ? errorObj : [NSNull null] - }]; + }]; + } else { + [self.bridge.eventDispatcher + sendAppEventWithName:label + body:@{ + @"task":@{ + @"id":@([task taskIdentifier]), + // @"bucket":[task bucket], + // @"key":[task key], + @"state":state, + @"bytes":@(bytes), + @"totalBytes":@(totalBytes) + }, + @"type":type, + @"error":errorObj ? errorObj : [NSNull null] + }]; + } } RCT_EXPORT_METHOD(initializeRNS3: (bool)subscribeProgressValue) { @@ -169,7 +179,8 @@ - (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state: state:@"in_progress" bytes:progress.completedUnitCount totalBytes:progress.totalUnitCount - error:nil]; + error:nil + label:@"@_RNS3_Progress_Changed"]; } }; self.completionUploadHandler = ^(AWSS3TransferUtilityUploadTask *task, NSError *error) { @@ -180,7 +191,8 @@ - (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state: state:state bytes:0 totalBytes:0 - error:error]; + error:error + label:@"@_RNS3_State_Changed"]; }; self.downloadProgress = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) { @@ -190,7 +202,8 @@ - (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state: state:@"in_progress" bytes:progress.completedUnitCount totalBytes:progress.totalUnitCount - error:nil]; + error:nil + label:@"@_RNS3_Progress_Changed"]; } }; self.completionDownloadHandler = ^(AWSS3TransferUtilityDownloadTask *task, NSURL *location, NSData *data, NSError *error) { @@ -201,7 +214,8 @@ - (void) sendEvent:(AWSS3TransferUtilityTask *)task type:(NSString *)type state: state:state bytes:0 totalBytes:0 - error:error]; + error:error + label:@"@_RNS3_State_Changed"]; }; AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility S3TransferUtilityForKey:@"RNS3TransferUtility"]; From 7822f85afa5b5b17a67a0dd715ef0d1147c1661f Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Thu, 28 Jul 2016 10:55:43 -0700 Subject: [PATCH 7/9] Fixed syntax for Travis CI --- example/app.js | 100 ++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/example/app.js b/example/app.js index ad3f0e9..758dc5c 100644 --- a/example/app.js +++ b/example/app.js @@ -5,8 +5,7 @@ import React, { Text, View, ScrollView, - TouchableHighlight, - DeviceEventEmitter + TouchableHighlight } from "react-native"; import { transferUtility } from "react-native-s3"; import fs from "react-native-fs"; @@ -28,13 +27,13 @@ const cognitoOptions = { "identity_pool_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "cognito_region": "us-east-1", "caching": true -} +}; const keySecretOptions = { "access_key": "xxxx", "secret_key": "xxxxxxxxxxxxx", - "region": "us-east-1", -} + "region": "us-east-1" +}; const sampleVideoURL = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"; @@ -44,17 +43,18 @@ class S3Sample extends Component { this.state = { initLoaded: false, - logText: "", + logText: "" }; } async componentDidMount() { if (!this.state.initLoaded) { if (!await fs.exists(uploadFilePath)) { - await fs.downloadFile(sampleVideoURL, uploadFilePath).then(res => { + await fs.downloadFile(sampleVideoURL, uploadFilePath).then(() => { fs.readDir(fs.DocumentDirectoryPath) - .then((result) => { - // Confirm that the file was written + .then((result) => { + // Confirm that the file was written + console.log(result); }); }); } @@ -81,29 +81,29 @@ class S3Sample extends Component { handleEvent = (eventType, task) => { switch(eventType) { - case '@_RNS3_State_Changed': - if (task.state == 'completed' && transferAction == 'Download') { - this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \nDownload complete \nFile location: ${ fs.DocumentDirectoryPath }` }); - } else if (task.state == 'completed' && transferAction == 'Upload') { - this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \nUpload complete \ns3 file location: ${ bucketName }/${ uploadFileKey }` }); - } else { this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \n` }); } - break; - case '@_RNS3_Progress_Changed': - this.setState({ logText: `${ this.state.logText }Progress_Changed: ${ task.bytes/task.totalBytes * 100 }% \n` }); - break; - case '@_RNS3_Error': - this.setState({ logText: `${ this.state.logText }Error: ${ task.errMessage } \n\n` }); - break; - default: - console.warn("Receiving event that doesn't match case"); - break; + case "@_RNS3_State_Changed": + if (task.state == "completed" && transferAction == "Download") { + this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \nDownload complete \nFile location: ${ fs.DocumentDirectoryPath }` }); + } else if (task.state == "completed" && transferAction == "Upload") { + this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \nUpload complete \ns3 file location: ${ bucketName }/${ uploadFileKey }` }); + } else { this.setState({ logText: `${ this.state.logText }State_Changed: ${ task.state } \n` }); } + break; + case "@_RNS3_Progress_Changed": + this.setState({ logText: `${ this.state.logText }Progress_Changed: ${ task.bytes/task.totalBytes * 100 }% \n` }); + break; + case "@_RNS3_Error": + this.setState({ logText: `${ this.state.logText }Error: ${ task.errMessage } \n\n` }); + break; + default: + console.warn("Receiving event that doesn't match case"); + break; } }; subscribeWithUpdateState = (id, typeKey) => { transferUtility.subscribe(id, (err, task) => { if (err != undefined) task.errMessage = err; - this.handleEvent(task.eventIdentifier, task) + this.handleEvent(task.eventIdentifier, task); this.setState({ [typeKey]: { ...this.state[typeKey], @@ -175,22 +175,22 @@ class S3Sample extends Component { render() { return ( - + - Download Designated File + {"Download Designated File"} - Upload Designated File + {"Upload Designated File"} { this.refs.scrollView.scrollTo({ y: height }) } }> - - { this.state.logText } - - + ref="scrollView" + style={styles.logContainer} + onContentSizeChange={(width, height) => {this.refs.scrollView.scrollTo({ y: height })};}> + + {this.state.logText} + + ); } @@ -200,7 +200,7 @@ const styles = StyleSheet.create({ container: { flex: 1, marginTop: 20, - alignItems: 'center', + alignItems: "center", backgroundColor: "#F5FCFF" }, title: { @@ -213,8 +213,8 @@ const styles = StyleSheet.create({ justifyContent: "center" }, logText: { - alignItems: 'center', - paddingBottom: 20, + alignItems: "center", + paddingBottom: 20 }, text: { fontSize: 12, @@ -227,16 +227,16 @@ const styles = StyleSheet.create({ margin: 10 }, logContainer: { - flex: 1, - width: 350, - marginBottom: 10, - borderWidth: 2, - borderRadius: 5, - borderColor: 'black', - paddingHorizontal: 10, - borderStyle: 'solid', - backgroundColor: 'lavender', - }, + flex: 1, + width: 350, + marginBottom: 10, + borderWidth: 2, + borderRadius: 5, + borderColor: "black", + paddingHorizontal: 10, + borderStyle: 'solid', + backgroundColor: "lavender" + } }); AppRegistry.registerComponent("S3Sample", () => S3Sample); From 64f367b0251da5c14f4c811c435d0604a7dce624 Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Thu, 28 Jul 2016 11:10:24 -0700 Subject: [PATCH 8/9] Fixed semicolon error --- example/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/app.js b/example/app.js index 758dc5c..239461d 100644 --- a/example/app.js +++ b/example/app.js @@ -185,7 +185,7 @@ class S3Sample extends Component { {this.refs.scrollView.scrollTo({ y: height })};}> + onContentSizeChange={(width, height) => {this.refs.scrollView.scrollTo({ y: height })}}> {this.state.logText} From b8a4e586ebd680e58ca0edd707532c42e68dcbaf Mon Sep 17 00:00:00 2001 From: Joel Wasserman Date: Thu, 28 Jul 2016 11:21:39 -0700 Subject: [PATCH 9/9] Trying to please Travis --- example/app.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/example/app.js b/example/app.js index 239461d..6006cab 100644 --- a/example/app.js +++ b/example/app.js @@ -21,7 +21,7 @@ const subscribeProgress = true; // Change to false if you don't want to subscrib var transferAction = ""; -// aws access keys and region +// aws cognito options const cognitoOptions = { "region": "us-east-1", "identity_pool_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", @@ -29,11 +29,12 @@ const cognitoOptions = { "caching": true }; -const keySecretOptions = { - "access_key": "xxxx", - "secret_key": "xxxxxxxxxxxxx", - "region": "us-east-1" -}; +// aws access keys and region options for basic s3 use +// const keySecretOptions = { +// "access_key": "xxxx", +// "secret_key": "xxxxxxxxxxxxx", +// "region": "us-east-1" +// }; const sampleVideoURL = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"; @@ -51,7 +52,7 @@ class S3Sample extends Component { if (!this.state.initLoaded) { if (!await fs.exists(uploadFilePath)) { await fs.downloadFile(sampleVideoURL, uploadFilePath).then(() => { - fs.readDir(fs.DocumentDirectoryPath) + fs.readDir(fs.DocumentDirectoryPath) .then((result) => { // Confirm that the file was written console.log(result); @@ -185,7 +186,7 @@ class S3Sample extends Component { {this.refs.scrollView.scrollTo({ y: height })}}> + onContentSizeChange={(width, height) => {this.refs.scrollView.scrollTo({ y: height });}}> {this.state.logText} @@ -234,7 +235,7 @@ const styles = StyleSheet.create({ borderRadius: 5, borderColor: "black", paddingHorizontal: 10, - borderStyle: 'solid', + borderStyle: "solid", backgroundColor: "lavender" } });