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

Commit

Permalink
Issue #493: Use WorkManager in fretboard
Browse files Browse the repository at this point in the history
Closes #493: Use WorkManager in fretboard
  • Loading branch information
fercarcedo committed Aug 3, 2018
1 parent 20f5ba1 commit e9df3eb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -8,6 +8,7 @@ buildscript {
supportLibraries: '27.1.1',
fxa: '0.2.1',
jna: '4.5.1',
workmanager: '1.0.0-alpha06',
// Test only:
junit: '4.12',
robolectric: '3.8',
Expand Down
1 change: 1 addition & 0 deletions components/service/fretboard/build.gradle
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation project(':support-ktx')
implementation "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.dependencies['kotlin']}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${rootProject.ext.dependencies['coroutines']}"
implementation "android.arch.work:work-runtime:${rootProject.ext.dependencies['workmanager']}"

testImplementation "junit:junit:${rootProject.ext.dependencies['junit']}"
testImplementation "org.robolectric:robolectric:${rootProject.ext.dependencies['robolectric']}"
Expand Down
@@ -0,0 +1,23 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.fretboard.scheduler.workmanager

import androidx.work.Worker
import mozilla.components.service.fretboard.Fretboard

abstract class SyncWorker : Worker() {
override fun doWork(): Result {
getFretboard().updateExperiments()
return Result.SUCCESS
}

/**
* Used to provide the instance of Fretboard
* the app is using
*
* @return current Fretboard instance
*/
abstract fun getFretboard(): Fretboard
}
@@ -0,0 +1,43 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.fretboard.scheduler.workmanager

import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import androidx.work.WorkRequest
import java.util.concurrent.TimeUnit

/**
* Class used to schedule sync of experiment
* configuration from the server using WorkManager
*/
class WorkManagerSyncScheduler {
/**
* Schedule sync with the request specified
*
* @param request object with the sync request configuration
*/
fun schedule(request: WorkRequest) {
WorkManager.getInstance().enqueue(request)
}

/**
* Schedule sync with the default constraints
* (once a day and charging)
*
* @param worker worker class
*/
fun schedule(worker: Class<out SyncWorker>) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val syncWork = PeriodicWorkRequest.Builder(SyncWorker::class.java, 1, TimeUnit.DAYS)
.setConstraints(constraints)
.build()
schedule(syncWork)
}
}

0 comments on commit e9df3eb

Please sign in to comment.