Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

JobScheduler helper function #554

Open
RamV13 opened this issue May 23, 2018 · 1 comment
Open

JobScheduler helper function #554

RamV13 opened this issue May 23, 2018 · 1 comment

Comments

@RamV13
Copy link
Contributor

RamV13 commented May 23, 2018

I'd like to propose a helper function for the JobScheduler API which is fairly cumbersome in Kotlin right now even with current KTX features. The signature of the function would be something like

inline fun scheduleJob(context: Context, jobId: Int, componentName: ComponentName, buildSequence: JobInfo.Builder.() -> Unit)

Before

val builder = JobInfo.Builder(jobId++, serviceComponent)

if (wiFiConnectivityRadioButton.isChecked) {
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
} else if (anyConnectivityRadioButton.isChecked) {
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
}

systemService<JobScheduler>().schedule(builder.build())

After

scheduleJob(this, jobId++, serviceComponent) {
    if (wiFiConnectivityRadioButton.isChecked) {
        setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
    } else if (anyConnectivityRadioButton.isChecked) {
        setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
    }
}

Please let me know if this sounds like a good idea and I'll get a PR ready!

* code snippet adapted from Google Samples

@romainguy
Copy link
Collaborator

Looks like a good idea to me!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants