Skip to content

Instantly share code, notes, and snippets.

@fornewid
Created June 8, 2019 06:12
Show Gist options
  • Save fornewid/22fbde5f08c5497ddfcb9b0cd1339df5 to your computer and use it in GitHub Desktop.
Save fornewid/22fbde5f08c5497ddfcb9b0cd1339df5 to your computer and use it in GitHub Desktop.
class BlurViewModel : ViewModel() {
...
internal fun applyBlur(blurLevel: Int) {
val chargingConstraints = Constraints.Builder()
.setRequiresCharging(true)
.build()
val cleanupRequest = OneTimeWorkRequest
.Builder(CleanupWorker::class.java)
.build()
val blurRequests = (0 until blurLevel).mapIndexed { index, _ ->
OneTimeWorkRequestBuilder<BlurWorker>()
.apply {
if (index == 0) {
setInputData(createInputDataForUri())
}
}
.build()
}
val saveRequest = OneTimeWorkRequest
.Builder(SaveImageToFileWorker::class.java)
.setConstraints(chargingConstraints)
.addTag(TAG_OUTPUT)
.build()
workManager
.beginUniqueWork(
IMAGE_MANIPULATION_WORK_NAME,
ExistingWorkPolicy.REPLACE,
cleanupRequest
)
.thenSequentially(blurRequests)
.then(saveRequest)
.enqueue()
}
// WorkContinuation#then(List<OneTimeWorkRequest>)는 'in any order'라서 임의로 sequential then을 추가함
private fun WorkContinuation.thenSequentially(
works: List<OneTimeWorkRequest>
): WorkContinuation {
var continuation = this
works.forEach {
continuation = continuation.then(it)
}
return continuation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment