Skip to content

Instantly share code, notes, and snippets.

@tunjid
Last active March 8, 2023 10:14
Show Gist options
  • Save tunjid/b064bba5969d428ebc44c8aaa90c5f37 to your computer and use it in GitHub Desktop.
Save tunjid/b064bba5969d428ebc44c8aaa90c5f37 to your computer and use it in GitHub Desktop.
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class DelegatingWorker(
appContext: Context,
workerParams: WorkerParameters,
) : CoroutineWorker(appContext, workerParams) {
private val workerClassName =
workerParams.inputData.getString(WORKER_CLASS_NAME) ?: ""
private val delegateWorker =
EntryPointAccessors.fromApplication<HiltWorkerFactoryEntryPoint>(appContext)
.hiltWorkerFactory()
.createWorker(appContext, workerClassName, workerParams)
as? CoroutineWorker
?: throw IllegalArgumentException("Unable to find appropriate worker")
override suspend fun getForegroundInfo(): ForegroundInfo =
delegateWorker.getForegroundInfo()
override suspend fun doWork(): Result =
delegateWorker.doWork()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment