Skip to content

Instantly share code, notes, and snippets.

@yenerm

yenerm/ImageOperations.kt

Last active May 7, 2021
Embed
What would you like to do?
/*
* 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