Skip to content

storage-ktx: add callbackFlow for upload/download progress #4139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions firebase-storage/ktx/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,30 @@ package com.google.firebase.storage.ktx {
method @NonNull public static operator java.io.InputStream component3(@NonNull com.google.firebase.storage.StreamDownloadTask.TaskSnapshot);
method @Nullable public static operator String component3(@NonNull com.google.firebase.storage.ListResult);
method @Nullable public static operator android.net.Uri component4(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.storage.ktx.StorageProgress<com.google.firebase.storage.StreamDownloadTask.TaskSnapshot>> getProgress(@NonNull com.google.firebase.storage.StreamDownloadTask);
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.storage.ktx.StorageProgress<com.google.firebase.storage.FileDownloadTask.TaskSnapshot>> getProgress(@NonNull com.google.firebase.storage.FileDownloadTask);
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.storage.ktx.StorageProgress<com.google.firebase.storage.UploadTask.TaskSnapshot>> getProgress(@NonNull com.google.firebase.storage.UploadTask);
method @NonNull public static com.google.firebase.storage.FirebaseStorage getStorage(@NonNull com.google.firebase.ktx.Firebase);
method @NonNull public static com.google.firebase.storage.FirebaseStorage storage(@NonNull com.google.firebase.ktx.Firebase, @NonNull String url);
method @NonNull public static com.google.firebase.storage.FirebaseStorage storage(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
method @NonNull public static com.google.firebase.storage.FirebaseStorage storage(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app, @NonNull String url);
method @NonNull public static com.google.firebase.storage.StorageMetadata storageMetadata(@NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.storage.StorageMetadata.Builder,kotlin.Unit> init);
}

public abstract class StorageProgress<T> {
}

public static final class StorageProgress.InProgress<T> extends com.google.firebase.storage.ktx.StorageProgress<T> {
ctor public StorageProgress.InProgress(@Nullable T snapshot);
method public T getSnapshot();
property public final T snapshot;
}

public static final class StorageProgress.Paused<T> extends com.google.firebase.storage.ktx.StorageProgress<T> {
ctor public StorageProgress.Paused(@Nullable T snapshot);
method public T getSnapshot();
property public final T snapshot;
}

}

3 changes: 3 additions & 0 deletions firebase-storage/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ plugins {
id 'kotlin-android'
}

group = "com.google.firebase"

firebaseLibrary {
releaseWith project(':firebase-storage')
publishJavadoc = true
Expand Down Expand Up @@ -61,6 +63,7 @@ dependencies {
implementation project(':firebase-storage')
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.android.gms:play-services-tasks:18.0.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"

androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ import com.google.firebase.platforminfo.LibraryVersionComponent
import com.google.firebase.storage.FileDownloadTask
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.ListResult
import com.google.firebase.storage.OnPausedListener
import com.google.firebase.storage.OnProgressListener
import com.google.firebase.storage.StorageMetadata
import com.google.firebase.storage.StorageReference
import com.google.firebase.storage.StorageTaskScheduler
import com.google.firebase.storage.StreamDownloadTask
import com.google.firebase.storage.UploadTask
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow

/** Returns the [FirebaseStorage] instance of the default [FirebaseApp]. */
val Firebase.storage: FirebaseStorage
Expand Down Expand Up @@ -133,6 +140,93 @@ operator fun ListResult.component2(): List<StorageReference> = prefixes
*/
operator fun ListResult.component3(): String? = pageToken

/**
* Starts listening to this task's stream download progress and emits its values via a [Flow].
*
* - When the returned flow starts being collected, it attaches the following listeners:
* [OnPausedListener], [OnProgressListener].
* - When the flow completes the listeners will be removed.
*/
val StreamDownloadTask.progress: Flow<StorageProgress<StreamDownloadTask.TaskSnapshot>>
get() = callbackFlow {
val progressListener = OnProgressListener<StreamDownloadTask.TaskSnapshot> { snapshot ->
StorageTaskScheduler.getInstance().scheduleCallback {
trySendBlocking(StorageProgress.InProgress(snapshot))
}
}
val pauseListener = OnPausedListener<StreamDownloadTask.TaskSnapshot> { snapshot ->
StorageTaskScheduler.getInstance().scheduleCallback {
trySendBlocking(StorageProgress.Paused(snapshot))
}
}

addOnProgressListener(progressListener)
addOnPausedListener(pauseListener)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial idea was to pass the same executor to these listeners, but Storage's callback executor is marked as private:

private static final ThreadPoolExecutor CALLBACK_QUEUE_EXECUTOR =
new ThreadPoolExecutor(
1, 1, 5, TimeUnit.SECONDS, mCallbackQueue, new StorageThreadFactory("Callbacks-"));

So instead I'm using the StorageTaskScheduler singleton to schedule all callbacks in that same executor:

StorageTaskScheduler.getInstance().scheduleCallback {
trySendBlocking(StorageProgress.InProgress(snapshot))
}


awaitClose {
removeOnProgressListener(progressListener)
removeOnPausedListener(pauseListener)
}
}

/**
* Starts listening to this task's file download progress and emits its values via a [Flow].
*
* - When the returned flow starts being collected, it attaches the following listeners:
* [OnPausedListener], [OnProgressListener].
* - When the flow completes the listeners will be removed.
*/
val FileDownloadTask.progress: Flow<StorageProgress<FileDownloadTask.TaskSnapshot>>
get() = callbackFlow {
val progressListener = OnProgressListener<FileDownloadTask.TaskSnapshot> { snapshot ->
StorageTaskScheduler.getInstance().scheduleCallback {
trySendBlocking(StorageProgress.InProgress(snapshot))
}
}
val pauseListener = OnPausedListener<FileDownloadTask.TaskSnapshot> { snapshot ->
StorageTaskScheduler.getInstance().scheduleCallback {
trySendBlocking(StorageProgress.Paused(snapshot))
}
}

addOnProgressListener(progressListener)
addOnPausedListener(pauseListener)

awaitClose {
removeOnProgressListener(progressListener)
removeOnPausedListener(pauseListener)
}
}

/**
* Starts listening to this task's upload progress and emits its values via a [Flow].
*
* - When the returned flow starts being collected, it attaches the following listeners:
* [OnPausedListener], [OnProgressListener].
* - When the flow completes the listeners will be removed.
*/
val UploadTask.progress: Flow<StorageProgress<UploadTask.TaskSnapshot>>
get() = callbackFlow {
val progressListener = OnProgressListener<UploadTask.TaskSnapshot> { snapshot ->
StorageTaskScheduler.getInstance().scheduleCallback {
trySendBlocking(StorageProgress.InProgress(snapshot))
}
}
val pauseListener = OnPausedListener<UploadTask.TaskSnapshot> { snapshot ->
StorageTaskScheduler.getInstance().scheduleCallback {
trySendBlocking(StorageProgress.Paused(snapshot))
}
}

addOnProgressListener(progressListener)
addOnPausedListener(pauseListener)

awaitClose {
removeOnProgressListener(progressListener)
removeOnPausedListener(pauseListener)
}
}

internal const val LIBRARY_NAME: String = "fire-stg-ktx"

/** @suppress */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.google.firebase.storage.ktx

/**
* Used to emit events about the progress of storage tasks.
*/
abstract class StorageProgress<T> private constructor() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not using a sealed class here to avoid binary incompatibility in a future release if some day we potentially include a new listener/event as part of progress monitoring.

Binary incompatibility is well explained here: https://jakewharton.com/public-api-challenges-in-kotlin/#binary-compatibility

/**
* Called periodically as data is transferred and can be used to populate an upload/download indicator.
*/
class InProgress<T>(val snapshot: T) : StorageProgress<T>()

/**
* Called any time the upload/download is paused.
*/
class Paused<T>(val snapshot: T) : StorageProgress<T>()
}