1- import { NativeModules , DeviceEventEmitter } from "react-native" ;
1+ import { NativeModules , NativeAppEventEmitter , DeviceEventEmitter , Platform } from "react-native" ;
22import store from "react-native-simple-store" ;
33
44const { RNS3TransferUtility } = NativeModules ;
@@ -12,16 +12,36 @@ const defaultCognitoOptions = {
1212 cognito_region : "eu-west-1"
1313} ;
1414const storeKey = "@_RNS3_Tasks_Extra" ;
15- let taskExtras ; // [id]: { bucket, key, bytes }
15+ /*
16+ * taskExtra:
17+ * [id]:
18+ * iOS: { bucket, key, state, bytes, totalBytes }
19+ * Android: { bucket, key, bytes }
20+ */
21+ let taskExtras ;
1622const subscribeCallbacks = { } ; // [id]: function
1723
18- DeviceEventEmitter . addListener ( "@_RNS3_Events" , async event => {
24+ let EventEmitter ;
25+ if ( Platform . OS === "ios" ) {
26+ EventEmitter = NativeAppEventEmitter ;
27+ } else if ( Platform . OS === "android" ) {
28+ EventEmitter = DeviceEventEmitter ;
29+ }
30+
31+ EventEmitter . addListener ( "@_RNS3_Events" , async event => {
1932 if ( ! taskExtras ) await getTaskExtras ( ) ;
2033 const { task, error } = event ;
21- const { bytes } = task ;
22- const finalTask = await setTaskExtra ( task , { bytes } ) ;
34+
35+ let finalTask = task ;
36+ if ( Platform . OS === "ios" ) {
37+ const { state, bytes, totalBytes } = task ;
38+ finalTask = await setTaskExtra ( task , { state, bytes, totalBytes } ) ;
39+ } else if ( Platform . OS === "android" ) {
40+ const { bytes } = task ;
41+ finalTask = await setTaskExtra ( task , { bytes } ) ;
42+ }
2343 if ( subscribeCallbacks [ task . id ] ) {
24- subscribeCallbacks [ task . id ] ( error , task ) ;
44+ subscribeCallbacks [ task . id ] ( error , finalTask ) ;
2545 }
2646} ) ;
2747
@@ -44,15 +64,23 @@ async function setTaskExtra(task, values, isNew) {
4464 if ( ! taskExtras [ id ] || isNew ) {
4565 taskExtras [ id ] = values ;
4666 } else {
47- if ( values . bytes ) {
48- taskExtras [ id ] = { ...taskExtras [ id ] , ...values } ;
67+ if ( Platform . OS === "ios" ) {
68+ if ( taskExtras [ id ] . bytes && ! values . bytes ) {
69+ taskExtras [ id ] = { ...taskExtras [ id ] , state : values . state } ;
70+ } else {
71+ taskExtras [ id ] = { ...taskExtras [ id ] , ...values } ;
72+ }
73+ } else if ( Platform . OS === "android" ) {
74+ if ( values . bytes ) {
75+ taskExtras [ id ] = { ...taskExtras [ id ] , ...values } ;
76+ }
4977 }
5078 }
5179 await saveTaskExtras ( ) ;
5280 return putExtra ( task ) ;
5381}
5482
55- class TransferUtility {
83+ export default class TransferUtility {
5684 async setupWithNative ( ) {
5785 const result = await RNS3TransferUtility . setupWithNative ( ) ;
5886 if ( result ) {
@@ -66,10 +94,10 @@ class TransferUtility {
6694 if ( ! options . access_key || ! options . secret_key ) {
6795 return false ;
6896 }
69- if ( ! options . session_token ) {
97+ if ( Platform . OS === "android" && ! options . session_token ) {
7098 options . session_token = null ;
7199 }
72- const result = await RNS3TransferUtility . setupWithBasic ( { ...defaultOptions , ...options } ) ;
100+ const result = await RNS3TransferUtility . setupWithBasic ( { ...defaultOptions , ...options } ) ;
73101 if ( result ) {
74102 await getTaskExtras ( ) ;
75103 RNS3TransferUtility . initializeRNS3 ( ) ;
@@ -81,10 +109,7 @@ class TransferUtility {
81109 if ( ! options . identity_pool_id ) {
82110 return false ;
83111 }
84- if ( ! options . caching ) {
85- options . caching = false ;
86- }
87- const result = await RNS3TransferUtility . setupWithCognito ( { ...defaultCognitoOptions , ...options } ) ;
112+ const result = await RNS3TransferUtility . setupWithBasic ( { ...defaultCognitoOptions , ...options } ) ;
88113 if ( result ) {
89114 await getTaskExtras ( ) ;
90115 RNS3TransferUtility . initializeRNS3 ( ) ;
@@ -97,20 +122,28 @@ class TransferUtility {
97122 options . meta = { } ;
98123 }
99124 const task = await RNS3TransferUtility . upload ( options ) ;
100- const finalTask = await setTaskExtra ( task , {
125+ const extra = {
101126 bucket : options . bucket ,
102127 key : options . key
103- } , true ) ;
104- return task ;
128+ } ;
129+ if ( Platform . OS === "ios" ) {
130+ extra . state = task . state ;
131+ }
132+ const finalTask = await setTaskExtra ( task , extra , true ) ;
133+ return finalTask ;
105134 }
106135
107136 async download ( options = { } ) {
108137 const task = await RNS3TransferUtility . download ( options ) ;
109- const finalTask = await setTaskExtra ( task , {
138+ const extra = {
110139 bucket : options . bucket ,
111140 key : options . key
112- } , true ) ;
113- return task ;
141+ } ;
142+ if ( Platform . OS === "ios" ) {
143+ extra . state = task . state ;
144+ }
145+ const finalTask = await setTaskExtra ( task , extra , true ) ;
146+ return finalTask ;
114147 }
115148
116149 pause ( id ) {
@@ -125,7 +158,11 @@ class TransferUtility {
125158 RNS3TransferUtility . cancel ( id ) ;
126159 }
127160
161+ // Android only
128162 async deleteRecord ( id ) {
163+ if ( Platform . OS === "ios" ) {
164+ throw new Error ( "Not implemented" ) ;
165+ }
129166 return RNS3TransferUtility . deleteRecord ( id ) ;
130167 }
131168
@@ -162,5 +199,3 @@ class TransferUtility {
162199 delete subscribeCallbacks [ id ] ;
163200 }
164201}
165-
166- export const transferUtility = new TransferUtility ( ) ;
0 commit comments