From 086aa0d4ddc06feca4dfda08772f0569f629fb7c Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Thu, 19 Oct 2023 18:14:10 +0200 Subject: [PATCH] refactor(storage): progress is now a percentage between 0 and 1 --- packages/storage/README.md | 2 +- .../storage/classes/events/UploadFileCallbackEvent.java | 2 +- .../ios/Plugin/Classes/Events/UploadFileCallbackEvent.swift | 2 +- packages/storage/src/definitions.ts | 5 +++-- packages/storage/src/web.ts | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/storage/README.md b/packages/storage/README.md index 7ef24d0e..89477d15 100644 --- a/packages/storage/README.md +++ b/packages/storage/README.md @@ -337,7 +337,7 @@ Upload a file. | Prop | Type | Description | Since | | ---------------------- | -------------------- | ----------------------------------------------------------------------------------- | ----- | -| **`progress`** | number | The upload progress, as a percentage from 0 to 100. | 5.3.0 | +| **`progress`** | number | The upload progress, as a percentage between 0 and 1. | 5.3.0 | | **`bytesTransferred`** | number | The number of bytes that have been transferred. Only available for Android and Web. | 5.3.0 | | **`totalBytes`** | number | The total number of bytes to be transferred. Only available for Android and Web. | 5.3.0 | | **`completed`** | boolean | Whether the upload is completed or not. | 5.3.0 | diff --git a/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/classes/events/UploadFileCallbackEvent.java b/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/classes/events/UploadFileCallbackEvent.java index e3db47f7..ebd26ac9 100644 --- a/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/classes/events/UploadFileCallbackEvent.java +++ b/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/classes/events/UploadFileCallbackEvent.java @@ -20,7 +20,7 @@ public UploadFileCallbackEvent(UploadTask.TaskSnapshot taskSnapshot, UploadFileS public JSObject toJSObject() { JSObject result = new JSObject(); - result.put("progress", (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount()); + result.put("progress", taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount()); result.put("bytesTransferred", taskSnapshot.getBytesTransferred()); result.put("totalBytes", taskSnapshot.getTotalByteCount()); result.put("completed", state == UploadFileState.SUCCESS); diff --git a/packages/storage/ios/Plugin/Classes/Events/UploadFileCallbackEvent.swift b/packages/storage/ios/Plugin/Classes/Events/UploadFileCallbackEvent.swift index 9f1c4595..c47e0429 100644 --- a/packages/storage/ios/Plugin/Classes/Events/UploadFileCallbackEvent.swift +++ b/packages/storage/ios/Plugin/Classes/Events/UploadFileCallbackEvent.swift @@ -11,7 +11,7 @@ import FirebaseStorage public func toJSObject() -> AnyObject { var result = JSObject() - result["progress"] = 100.0 * Double(snapshot.progress!.completedUnitCount) + result["progress"] = Double(snapshot.progress!.completedUnitCount) / Double(snapshot.progress!.totalUnitCount) result["completed"] = snapshot.status == .success ? true : false return result as AnyObject diff --git a/packages/storage/src/definitions.ts b/packages/storage/src/definitions.ts index 31077e21..7b615a0c 100644 --- a/packages/storage/src/definitions.ts +++ b/packages/storage/src/definitions.ts @@ -336,10 +336,10 @@ export type UploadFileCallback = ( */ export interface UploadFileCallbackEvent { /** - * The upload progress, as a percentage from 0 to 100. + * The upload progress, as a percentage between 0 and 1. * * @since 5.3.0 - * @example 50 + * @example 0.5 */ progress: number; /** @@ -364,6 +364,7 @@ export interface UploadFileCallbackEvent { * Whether the upload is completed or not. * * @since 5.3.0 + * @example true */ completed: boolean; } diff --git a/packages/storage/src/web.ts b/packages/storage/src/web.ts index 80cd987c..2703f24b 100644 --- a/packages/storage/src/web.ts +++ b/packages/storage/src/web.ts @@ -138,7 +138,7 @@ export class FirebaseStorageWeb snapshot: UploadTaskSnapshot, ): UploadFileCallbackEvent { const result: UploadFileCallbackEvent = { - progress: (snapshot.bytesTransferred / snapshot.totalBytes) * 100, + progress: snapshot.bytesTransferred / snapshot.totalBytes, bytesTransferred: snapshot.bytesTransferred, totalBytes: snapshot.totalBytes, completed: snapshot.state === 'success',