Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Challenge - Workers

The student materials have been reviewed and are updated as of SEPTEMBER 2022.

Implement another worker, to apply a filter to the image, practicing what you’ve learned so far.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

To master using the WorkManager API, before implementing it in a more complex application, you first have to solve a fun challenge! :] In this challenge you have to build a worker to apply a sepia filter to your bitmap. Here’s a hint: the Sepia Filter code is prepared for you, in the ImageUtils class!

class SepiaFilterWorker(context: Context, workerParameters: WorkerParameters) :
    Worker(context, workerParameters) {

  override fun doWork(): Result {

  }
}
class SepiaFilterWorker(context: Context, workerParameters: WorkerParameters) :
    Worker(context, workerParameters) {

  override fun doWork(): Result {
    val imagePath = inputData.getString("image_path") ?: return Result.failure()
    val bitmap = BitmapFactory.decodeFile(imagePath)
    val newBitmap = ImageUtils.applySepiaFilter(bitmap)

    val outputStream = FileOutputStream(imagePath)
    outputStream.use { output ->
      newBitmap.compress(Bitmap.CompressFormat.PNG, 100, output)

      output.flush()
    }

    val output = workDataOf("image_path" to imagePath)
    return Result.success(output)
  }
}
val sepiaFilterWorker = OneTimeWorkRequestBuilder<SepiaFilterWorker>()
    .setConstraints(constraints)
    .build()

workManager.beginWith(clearFilesRequest)
  .then(downloadRequest)
  .then(sepiaFilterWorker)
  .enqueue()
workManager.getWorkInfoByIdLiveData(sepiaFilterWorker.id)

Reviews

Comments