-
-
Notifications
You must be signed in to change notification settings - Fork 777
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pass worker inputs without serialization (#44)
Because androidx.work.Data sucks and causes our app to crash.
- Loading branch information
Showing
9 changed files
with
104 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/src/main/java/app/revanced/manager/domain/worker/Worker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package app.revanced.manager.domain.worker | ||
|
||
import android.content.Context | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.WorkerParameters | ||
|
||
abstract class Worker<ARGS>(context: Context, parameters: WorkerParameters) : CoroutineWorker(context, parameters) |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/app/revanced/manager/domain/worker/WorkerRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package app.revanced.manager.domain.worker | ||
|
||
import android.app.Application | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.OneTimeWorkRequest | ||
import androidx.work.OutOfQuotaPolicy | ||
import androidx.work.WorkManager | ||
import java.util.UUID | ||
|
||
class WorkerRepository(app: Application) { | ||
val workManager = WorkManager.getInstance(app) | ||
|
||
/** | ||
* The standard WorkManager communication APIs use [androidx.work.Data], which has too many limitations. | ||
* We can get around those limits by passing inputs using global variables instead. | ||
*/ | ||
val workerInputs = mutableMapOf<UUID, Any>() | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <A : Any, W : Worker<A>> claimInput(worker: W): A { | ||
val data = workerInputs[worker.id] ?: throw IllegalStateException("Worker was not launched via WorkerRepository") | ||
workerInputs.remove(worker.id) | ||
|
||
return data as A | ||
} | ||
|
||
inline fun <reified W : Worker<A>, A : Any> launchExpedited(name: String, input: A): UUID { | ||
val request = | ||
OneTimeWorkRequest.Builder(W::class.java) // create Worker | ||
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) | ||
.build() | ||
workerInputs[request.id] = input | ||
workManager.enqueueUniqueWork(name, ExistingWorkPolicy.REPLACE, request) | ||
return request.id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.