Skip to content

Instantly share code, notes, and snippets.

@yenerm
Last active August 10, 2023 14:26
Show Gist options
  • Save yenerm/b9744b24614fb29cedf47e36df3248b4 to your computer and use it in GitHub Desktop.
Save yenerm/b9744b24614fb29cedf47e36df3248b4 to your computer and use it in GitHub Desktop.
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.background
/**
* Builds and holds WorkContinuation based on supplied filters.
*/
@SuppressLint("EnqueueWork")
class ImageOperations(
/* params removed */
) {
//..
//Add some DummyWorkers to continuation in init()
init {
continuation = WorkManager.getInstance(context)
.beginUniqueWork(
Constants.IMAGE_MANIPULATION_WORK_NAME,
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest.from(CleanupWorker::class.java)
)
//Add 2 DummyWorker
.then(
listOf(
OneTimeWorkRequest.from(DummyWorker::class.java),
OneTimeWorkRequest.from(DummyWorker::class.java))
)
.thenMaybe<WaterColorFilterWorker>(waterColor)
//Add 4 more workers here
.then(listOf(
OneTimeWorkRequest.from(DummyWorker::class.java),
OneTimeWorkRequest.from(DummyWorker::class.java),
OneTimeWorkRequest.from(DummyWorker::class.java),
OneTimeWorkRequest.from(DummyWorker::class.java)))
.thenMaybe<GrayScaleFilterWorker>(grayScale)
.thenMaybe<BlurEffectFilterWorker>(blur)
.then(
if (save) {
workRequest<SaveImageToGalleryWorker>(tag = Constants.TAG_OUTPUT)
} else /* upload */ {
workRequest<UploadWorker>(tag = Constants.TAG_OUTPUT)
}
)
//Another 2 DummyWorker here
.then(listOf(
OneTimeWorkRequest.from(DummyWorker::class.java),
OneTimeWorkRequest.from(DummyWorker::class.java)))
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment