Skip to content

Commit

Permalink
feat(java): improve cancellation of WorkManager uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Aug 21, 2023
1 parent 36bee23 commit 62a80d7
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 83 deletions.
159 changes: 92 additions & 67 deletions templates/java/android/work/UploadWorkerHelper.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -209,78 +209,16 @@ object UploadWorkerHelper {
file
)
)
.addTag("{{artifactId}}")
.addTag(getTagForUpload(videoId, token))
.addTag(ARTIFACT_ID)
.apply {
videoId?.let { addTag(getTagForUpload(it)) }
token?.let { addTag(getTagForUploadWithUploadToken(it, videoId)) }
tags.forEach { addTag(it) }
}
.build()
return OperationWithRequest(workManager.enqueue(workRequest), workRequest)
}

/**
* Cancels all works related to a video id that was added with [upload].
* Works with upload token are not cancelled.
*
* @param context The application context
* @param videoId The video id
*/
@JvmStatic
fun cancel(context: Context, videoId: String) =
cancel(WorkManager.getInstance(context), videoId)

/**
* Cancels all works related to a video id that was added with [upload].
* Works with upload token are not cancelled.
*
* @param workManager The WorkManager instance
* @param videoId The video id
*/
@JvmStatic
fun cancel(workManager: WorkManager, videoId: String) =
workManager.cancelAllWorkByTag(getTagForUpload(videoId, null))

/**
* Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
* Works without upload token are not cancelled.
*
* @param context The application context
* @param token The upload token
* @param videoId The video id.Must be the same as the one used in [uploadWithUploadToken].
*/
@JvmStatic
fun cancelWithUploadToken(context: Context, token: String, videoId: String? = null) =
cancelWithUploadToken(WorkManager.getInstance(context), token, videoId)

/**
* Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
* Works without upload token are not cancelled.
*
* @param workManager The WorkManager instance
* @param token The upload token
* @param videoId The video id. Must be the same as the one used in [uploadWithUploadToken].
*/
@JvmStatic
fun cancelWithUploadToken(workManager: WorkManager, token: String, videoId: String? = null) =
workManager.cancelAllWorkByTag(getTagForUpload(videoId, token))

private const val PREFIX_VIDEO_ID = "videoId="
private const val PREFIX_TOKEN = "token="

/**
* Returns the tag used to identify works related to a video id or an upload token.
*
* @param videoId The video id
* @param token The upload token
* @return The tag
*/
fun getTagForUpload(videoId: String?, token: String?): String {
require((token != null) || (videoId != null)) {
"You must provide either a token or a videoId"
}
return "($PREFIX_VIDEO_ID$videoId, $PREFIX_TOKEN$token)"
}

/**
* Enqueues a work to upload a part of a file.
*
Expand Down Expand Up @@ -404,13 +342,100 @@ object UploadWorkerHelper {
partId
)
)
.addTag("{{artifactId}}")
.addTag(ARTIFACT_ID)
.addTag("progressive")
.addTag(getTagForUpload(session.videoId, session.token))
.apply {
session.videoId?.let { addTag(getTagForUpload(it)) }
session.token?.let { addTag(getTagForUploadWithUploadToken(it, videoId)) }
tags.forEach { addTag(it) }
}
.build()
return OperationWithRequest(workManager.enqueue(workRequest), workRequest)
}

/**
* Cancels all upload works.
*
* @param context The application context
*/
@JvmStatic
fun cancelAll(context: Context) =
cancelAll(WorkManager.getInstance(context))

/**
* Cancels all upload works.
*
* @param workManager The WorkManager instance
*/
@JvmStatic
fun cancelAll(workManager: WorkManager) =
workManager.cancelAllWorkByTag(ARTIFACT_ID)

/**
* Cancels all works related to a video id.
*
* @param context The application context
* @param videoId The video id
*/
@JvmStatic
fun cancel(context: Context, videoId: String) =
cancel(WorkManager.getInstance(context), videoId)

/**
* Cancels all works related to a video id.
*
* @param workManager The WorkManager instance
* @param videoId The video id
*/
@JvmStatic
fun cancel(workManager: WorkManager, videoId: String) =
workManager.cancelAllWorkByTag(getTagForUpload(videoId))

/**
* Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
*
* @param context The application context
* @param token The upload token
* @param videoId The video id.Must be the same as the one used in [uploadWithUploadToken].
*/
@JvmStatic
fun cancelWithUploadToken(context: Context, token: String, videoId: String? = null) =
cancelWithUploadToken(WorkManager.getInstance(context), token, videoId)

/**
* Cancels all works related to an upload token and possibly a video id that was added with [uploadWithUploadToken].
*
* @param workManager The WorkManager instance
* @param token The upload token
* @param videoId The video id. Must be the same as the one used in [uploadWithUploadToken].
*/
@JvmStatic
fun cancelWithUploadToken(workManager: WorkManager, token: String, videoId: String? = null) =
workManager.cancelAllWorkByTag(getTagForUploadWithUploadToken(token, videoId))

/**
* Returns the tag used to identify works related to a video id.
*
* @param token The upload token
* @param videoId The video id
* @return The tag
*/
fun getTagForUpload(videoId: String): String {
return "($PREFIX_VIDEO_ID$videoId)"
}

/**
* Returns the tag used to identify works related to an upload token and possibly a video id.
*
* @param token The upload token
* @param videoId The video id
* @return The tag
*/
fun getTagForUploadWithUploadToken(token: String, videoId: String?): String {
return "($PREFIX_TOKEN$token, $PREFIX_VIDEO_ID$videoId)"
}

private const val PREFIX_VIDEO_ID = "videoId="
private const val PREFIX_TOKEN = "token="
private const val ARTIFACT_ID = "{{artifactId}}"
}
37 changes: 21 additions & 16 deletions templates/java/android/work/WorkManagerExtensions.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,6 @@ fun WorkManager.uploadWithUploadToken(
workerClass
)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.upload].
*
* @param videoId The video id
*/
fun WorkManager.cancel(videoId: String) = UploadWorkerHelper.cancel(this, videoId)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.uploadWithUploadToken].
*
* @param token The upload token
* @param videoId The video id. Must be the same as the one used in [WorkManager.uploadWithUploadToken].
*/
fun WorkManager.cancelWithUploadToken(token: String, videoId: String? = null) =
UploadWorkerHelper.cancelWithUploadToken(this, token, videoId)

/**
* Extension functions for [WorkManager] to enqueue upload works for progressive upload.
*
Expand Down Expand Up @@ -147,3 +131,24 @@ fun WorkManager.uploadPart(
tags,
workerClass
)

/**
* Extension functions for [WorkManager] to cancel all upload works.
*/
fun WorkManager.cancelAllUploads() = UploadWorkerHelper.cancelAll(this)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.upload].
*
* @param videoId The video id
*/
fun WorkManager.cancel(videoId: String) = UploadWorkerHelper.cancel(this, videoId)

/**
* Extension functions for [WorkManager] to cancel upload works that was added with [WorkManager.uploadWithUploadToken].
*
* @param token The upload token
* @param videoId The video id. Must be the same as the one used in [WorkManager.uploadWithUploadToken].
*/
fun WorkManager.cancelWithUploadToken(token: String, videoId: String? = null) =
UploadWorkerHelper.cancelWithUploadToken(this, token, videoId)

0 comments on commit 62a80d7

Please sign in to comment.