@@ -231,56 +231,81 @@ protected virtual void DisplayUploadState(UploadState uploadState) {
231231 }
232232 }
233233
234- // Upload file text to Cloud Storage using a byte array.
235- protected IEnumerator UploadBytes ( ) {
234+ protected Task < StorageMetadata > UploadBytesAsync ( ) {
236235 var storageReference = GetStorageReference ( ) ;
237236 DebugLog ( String . Format ( "Uploading to {0} ..." , storageReference . Path ) ) ;
238- var task = storageReference . PutBytesAsync (
237+ return storageReference . PutBytesAsync (
239238 Encoding . UTF8 . GetBytes ( fileContents ) , StringToMetadataChange ( fileMetadataChangeString ) ,
240239 new StorageProgress < UploadState > ( DisplayUploadState ) ,
241- cancellationTokenSource . Token , null ) ;
240+ cancellationTokenSource . Token , null ) . ContinueWithOnMainThread ( task => {
241+ DisplayUploadComplete ( task ) ;
242+ return task ;
243+ } ) . Unwrap ( ) ;
244+ }
245+
246+ // Upload file text to Cloud Storage using a byte array.
247+ protected IEnumerator UploadBytes ( ) {
248+ var task = UploadBytesAsync ( ) ;
242249 yield return new WaitForTaskCompletion ( this , task ) ;
243- DisplayUploadComplete ( task ) ;
244250 }
245251
246- // Upload file to Cloud Storage using a stream.
247- protected IEnumerator UploadStream ( ) {
252+ protected Task < StorageMetadata > UploadStreamAsync ( ) {
248253 var storageReference = GetStorageReference ( ) ;
249254 DebugLog ( String . Format ( "Uploading to {0} using stream..." , storageReference . Path ) ) ;
250- var task = storageReference . PutStreamAsync (
255+ return storageReference . PutStreamAsync (
251256 new MemoryStream ( System . Text . Encoding . ASCII . GetBytes ( fileContents ) ) ,
252257 StringToMetadataChange ( fileMetadataChangeString ) ,
253258 new StorageProgress < UploadState > ( DisplayUploadState ) ,
254- cancellationTokenSource . Token , null ) ;
259+ cancellationTokenSource . Token , null ) . ContinueWithOnMainThread ( task => {
260+ DisplayUploadComplete ( task ) ;
261+ return task ;
262+ } ) . Unwrap ( ) ;
263+ }
264+
265+
266+ // Upload file to Cloud Storage using a stream.
267+ protected IEnumerator UploadStream ( ) {
268+ var task = UploadStreamAsync ( ) ;
255269 yield return new WaitForTaskCompletion ( this , task ) ;
256- DisplayUploadComplete ( task ) ;
257270 }
258271
259- // Upload a file from the local filesystem to Cloud Storage.
260- protected IEnumerator UploadFromFile ( ) {
272+ protected Task < StorageMetadata > UploadFromFileAsync ( ) {
261273 var localFilenameUriString = PathToPersistentDataPathUriString ( localFilename ) ;
262274 var storageReference = GetStorageReference ( ) ;
263275 DebugLog ( String . Format ( "Uploading '{0}' to '{1}'..." , localFilenameUriString ,
264276 storageReference . Path ) ) ;
265- var task = storageReference . PutFileAsync (
277+ return storageReference . PutFileAsync (
266278 localFilenameUriString , StringToMetadataChange ( fileMetadataChangeString ) ,
267279 new StorageProgress < UploadState > ( DisplayUploadState ) ,
268- cancellationTokenSource . Token , null ) ;
280+ cancellationTokenSource . Token , null ) . ContinueWithOnMainThread ( task => {
281+ DisplayUploadComplete ( task ) ;
282+ return task ;
283+ } ) . Unwrap ( ) ;
284+ }
285+
286+ // Upload a file from the local filesystem to Cloud Storage.
287+ protected IEnumerator UploadFromFile ( ) {
288+ var task = UploadFromFileAsync ( ) ;
269289 yield return new WaitForTaskCompletion ( this , task ) ;
270- DisplayUploadComplete ( task ) ;
271290 }
272291
273- // Update the metadata on the file in Cloud Storage.
274- protected IEnumerator UpdateMetadata ( ) {
292+ protected Task < StorageMetadata > UpdateMetadataAsync ( ) {
275293 var storageReference = GetStorageReference ( ) ;
276294 DebugLog ( String . Format ( "Updating metadata of {0} ..." , storageReference . Path ) ) ;
277- var task = storageReference . UpdateMetadataAsync ( StringToMetadataChange (
278- fileMetadataChangeString ) ) ;
295+ return storageReference . UpdateMetadataAsync ( StringToMetadataChange (
296+ fileMetadataChangeString ) ) . ContinueWithOnMainThread ( task => {
297+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
298+ DebugLog ( "Updated metadata" ) ;
299+ DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
300+ }
301+ return task ;
302+ } ) . Unwrap ( ) ;
303+ }
304+
305+ // Update the metadata on the file in Cloud Storage.
306+ protected IEnumerator UpdateMetadata ( ) {
307+ var task = UpdateMetadataAsync ( ) ;
279308 yield return new WaitForTaskCompletion ( this , task ) ;
280- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
281- DebugLog ( "Updated metadata" ) ;
282- DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
283- }
284309 }
285310
286311 // Write download state to the log.
@@ -291,28 +316,33 @@ protected virtual void DisplayDownloadState(DownloadState downloadState) {
291316 }
292317 }
293318
294- // Download from Cloud Storage into a byte array.
295- protected IEnumerator DownloadBytes ( ) {
319+ protected Task < byte [ ] > DownloadBytesAsync ( ) {
296320 var storageReference = GetStorageReference ( ) ;
297321 DebugLog ( String . Format ( "Downloading {0} ..." , storageReference . Path ) ) ;
298- var task = storageReference . GetBytesAsync (
322+ return storageReference . GetBytesAsync (
299323 0 , new StorageProgress < DownloadState > ( DisplayDownloadState ) ,
300- cancellationTokenSource . Token ) ;
324+ cancellationTokenSource . Token ) . ContinueWithOnMainThread ( task => {
325+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
326+ DebugLog ( "Finished downloading bytes" ) ;
327+ fileContents = System . Text . Encoding . Default . GetString ( task . Result ) ;
328+ DebugLog ( String . Format ( "File Size {0} bytes\n " , fileContents . Length ) ) ;
329+ }
330+ return task ;
331+ } ) . Unwrap ( ) ;
332+ }
333+
334+ // Download from Cloud Storage into a byte array.
335+ protected IEnumerator DownloadBytes ( ) {
336+ var task = DownloadBytesAsync ( ) ;
301337 yield return new WaitForTaskCompletion ( this , task ) ;
302- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
303- DebugLog ( "Finished downloading bytes" ) ;
304- fileContents = System . Text . Encoding . Default . GetString ( task . Result ) ;
305- DebugLog ( String . Format ( "File Size {0} bytes\n " , fileContents . Length ) ) ;
306- }
307338 }
308339
309- // Download from Cloud Storage using a stream.
310- protected IEnumerator DownloadStream ( ) {
340+ protected Task DownloadStreamAsync ( ) {
311341 // Download the file using a stream.
312342 fileContents = "" ;
313343 var storageReference = GetStorageReference ( ) ;
314344 DebugLog ( String . Format ( "Downloading {0} with stream ..." , storageReference . Path ) ) ;
315- var task = storageReference . GetStreamAsync ( ( stream ) => {
345+ return storageReference . GetStreamAsync ( ( stream ) => {
316346 var buffer = new byte [ 1024 ] ;
317347 int read ;
318348 // Read data to render in the text view.
@@ -321,52 +351,70 @@ protected IEnumerator DownloadStream() {
321351 }
322352 } ,
323353 new StorageProgress < DownloadState > ( DisplayDownloadState ) ,
324- cancellationTokenSource . Token ) ;
354+ cancellationTokenSource . Token ) . ContinueWithOnMainThread ( task => {
355+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
356+ DebugLog ( "Finished downloading stream\n " ) ;
357+ }
358+ return task ;
359+ } ) . Unwrap ( ) ;
360+ }
325361
362+ // Download from Cloud Storage using a stream.
363+ protected IEnumerator DownloadStream ( ) {
364+ var task = DownloadStreamAsync ( ) ;
326365 yield return new WaitForTaskCompletion ( this , task ) ;
327- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
328- DebugLog ( "Finished downloading stream\n " ) ;
329- }
366+
330367 }
331368
332369 // Get a local filesystem path from a file:// URI.
333370 protected string FileUriStringToPath ( string fileUriString ) {
334371 return Uri . UnescapeDataString ( ( new Uri ( fileUriString ) ) . PathAndQuery ) ;
335372 }
336373
337- // Download from Cloud Storage to a local file.
338- protected IEnumerator DownloadToFile ( ) {
374+ protected Task DownloadToFileAsync ( ) {
339375 var storageReference = GetStorageReference ( ) ;
340376 var localFilenameUriString = PathToPersistentDataPathUriString ( localFilename ) ;
341377 DebugLog ( String . Format ( "Downloading {0} to {1}..." , storageReference . Path ,
342378 localFilenameUriString ) ) ;
343- var task = storageReference . GetFileAsync (
379+ return storageReference . GetFileAsync (
344380 localFilenameUriString ,
345381 new StorageProgress < DownloadState > ( DisplayDownloadState ) ,
346- cancellationTokenSource . Token ) ;
347- yield return new WaitForTaskCompletion ( this , task ) ;
348- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
349- var filename = FileUriStringToPath ( localFilenameUriString ) ;
350- DebugLog ( String . Format ( "Finished downloading file {0} ({1})" , localFilenameUriString ,
351- filename ) ) ;
352- DebugLog ( String . Format ( "File Size {0} bytes\n " , ( new FileInfo ( filename ) ) . Length ) ) ;
353- fileContents = File . ReadAllText ( filename ) ;
354- }
382+ cancellationTokenSource . Token ) . ContinueWithOnMainThread ( task => {
383+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
384+ var filename = FileUriStringToPath ( localFilenameUriString ) ;
385+ DebugLog ( String . Format ( "Finished downloading file {0} ({1})" , localFilenameUriString ,
386+ filename ) ) ;
387+ DebugLog ( String . Format ( "File Size {0} bytes\n " , ( new FileInfo ( filename ) ) . Length ) ) ;
388+ fileContents = File . ReadAllText ( filename ) ;
389+ }
390+ return task ;
391+ } ) . Unwrap ( ) ;
355392 }
356393
357- // Delete a remote file.
358- protected IEnumerator Delete ( ) {
394+ // Download from Cloud Storage to a local file.
395+ protected IEnumerator DownloadToFile ( ) {
396+ var task = DownloadToFileAsync ( ) ;
397+ yield return new WaitForTaskCompletion ( this , task ) ;
398+ }
399+
400+ protected Task DeleteAsync ( ) {
359401 var storageReference = GetStorageReference ( ) ;
360402 DebugLog ( String . Format ( "Deleting {0}..." , storageReference . Path ) ) ;
361- var task = storageReference . DeleteAsync ( ) ;
403+ return storageReference . DeleteAsync ( ) . ContinueWithOnMainThread ( task => {
404+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
405+ DebugLog ( String . Format ( "{0} deleted" , storageReference . Path ) ) ;
406+ }
407+ return task ;
408+ } ) . Unwrap ( ) ;
409+ }
410+
411+ // Delete a remote file.
412+ protected IEnumerator Delete ( ) {
413+ var task = DeleteAsync ( ) ;
362414 yield return new WaitForTaskCompletion ( this , task ) ;
363- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
364- DebugLog ( String . Format ( "{0} deleted" , storageReference . Path ) ) ;
365- }
366415 }
367416
368- // Download and display Metadata for the storage reference.
369- protected IEnumerator GetMetadata ( ) {
417+ protected Task < StorageMetadata > GetMetadataAsync ( ) {
370418 var storageReference = GetStorageReference ( ) ;
371419 DebugLog ( String . Format ( "Bucket: {0}" , storageReference . Bucket ) ) ;
372420 DebugLog ( String . Format ( "Path: {0}" , storageReference . Path ) ) ;
@@ -375,10 +423,17 @@ protected IEnumerator GetMetadata() {
375423 storageReference . Parent . Path : "(root)" ) ) ;
376424 DebugLog ( String . Format ( "Root Path: {0}" , storageReference . Root . Path ) ) ;
377425 DebugLog ( String . Format ( "App: {0}" , storageReference . Storage . App . Name ) ) ;
378- var task = storageReference . GetMetadataAsync ( ) ;
426+ return storageReference . GetMetadataAsync ( ) . ContinueWithOnMainThread ( task => {
427+ if ( ! ( task . IsFaulted || task . IsCanceled ) )
428+ DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
429+ return task ;
430+ } ) . Unwrap ( ) ;
431+ }
432+
433+ // Download and display Metadata for the storage reference.
434+ protected IEnumerator GetMetadata ( ) {
435+ var task = GetMetadataAsync ( ) ;
379436 yield return new WaitForTaskCompletion ( this , task ) ;
380- if ( ! ( task . IsFaulted || task . IsCanceled ) )
381- DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
382437 }
383438
384439 // Display the download URL for a storage reference.
0 commit comments