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.

Implement WorkManager in Complex Apps

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

Implement a similar example as in the first part of the course, in a more complex application.

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.

Now that you’ve set up the projects and everything you need to work through this course, you can implement the work manager! :] Let’s move to the project!

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

  override fun doWork(): Result {

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

  override fun doWork(): Result {
    val imageDownloadPath = inputData.getString("image_path") ?: return Result.failure()
    val imageUrl = URL(imageDownloadPath)

    val connection = imageUrl.openConnection() as HttpURLConnection
    connection.doInput = true
    connection.connect()

    val imagePath = "${System.currentTimeMillis()}.jpg"
    val inputStream = connection.inputStream
    val file = File(applicationContext.externalMediaDirs.first(), imagePath)
  }
}
class DownloadImageWorker(context: Context, workerParameters: WorkerParameters) :
    Worker(context, workerParameters) {

  override fun doWork(): Result {
    val imageDownloadPath = inputData.getString("image_path") ?: return Result.failure()
    val imageUrl = URL(imageDownloadPath)

    val connection = imageUrl.openConnection() as HttpURLConnection
    connection.doInput = true
    connection.connect()

    val imagePath = "${System.currentTimeMillis()}.jpg"
    val inputStream = connection.inputStream
    val file = File(applicationContext.externalMediaDirs.first(), imagePath)

    val outputStream = FileOutputStream(file)
    outputStream.use { output ->
      val buffer = ByteArray(4 * 1024)

      var byteCount = inputStream.read(buffer)
      while (byteCount > 0) {
        output.write(buffer, 0, byteCount)

        byteCount = inputStream.read(buffer)
      }

      output.flush()
    }

    val output = workDataOf("image_path" to file.absolutePath)
    return Result.success(output)
  }
}
    val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.NOT_ROAMING)
        .setRequiresBatteryNotLow(true)
        .setRequiresStorageNotLow(true)
        .build()

    val downloadImageWorker = OneTimeWorkRequestBuilder<DownloadImageWorker>()
        .setInputData(workDataOf("image_path" to imageUrl))
        .setConstraints(constraints)
        .build()
    val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.NOT_ROAMING)
        .setRequiresBatteryNotLow(true)
        .setRequiresStorageNotLow(true)
        .build()

    val downloadImageWorker = OneTimeWorkRequestBuilder<DownloadImageWorker>()
        .setInputData(workDataOf("image_path" to imageUrl))
        .setConstraints(constraints)
        .build()

    val workManager = WorkManager.getInstance(requireActivity())
    workManager.enqueue(downloadImageWorker)

    workManager.getWorkInfoByIdLiveData(downloadImageWorker.id)
        .observe(this, Observer { workInfo ->
          if (workInfo?.state?.isFinished == true) {
            activity?.toast("Image downloaded!")
          }
        })

Reviews

Comments