Pass context to get WorkManager instance

This commit is contained in:
arkon 2020-03-07 13:00:58 -05:00
parent 52434819c3
commit 8b6268966e
7 changed files with 19 additions and 19 deletions

View file

@ -23,11 +23,11 @@ object Migrations {
if (oldVersion == 0) return false if (oldVersion == 0) return false
if (oldVersion < 14) { if (oldVersion < 14) {
// Restore jobs after upgrading to evernote's job scheduler. // Restore jobs after upgrading to Evernote's job scheduler.
if (BuildConfig.INCLUDE_UPDATER && preferences.automaticUpdates()) { if (BuildConfig.INCLUDE_UPDATER && preferences.automaticUpdates()) {
UpdaterJob.setupTask() UpdaterJob.setupTask(context)
} }
LibraryUpdateJob.setupTask() LibraryUpdateJob.setupTask(context)
} }
if (oldVersion < 15) { if (oldVersion < 15) {
// Delete internal chapter cache dir. // Delete internal chapter cache dir.
@ -39,7 +39,7 @@ object Migrations {
if (oldDir.exists()) { if (oldDir.exists()) {
val destDir = context.getExternalFilesDir("covers") val destDir = context.getExternalFilesDir("covers")
if (destDir != null) { if (destDir != null) {
oldDir.listFiles().forEach { oldDir.listFiles()?.forEach {
it.renameTo(File(destDir, it.name)) it.renameTo(File(destDir, it.name))
} }
} }

View file

@ -28,7 +28,7 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
companion object { companion object {
const val TAG = "BackupCreator" const val TAG = "BackupCreator"
fun setupTask(prefInterval: Int? = null) { fun setupTask(context: Context, prefInterval: Int? = null) {
val preferences = Injekt.get<PreferencesHelper>() val preferences = Injekt.get<PreferencesHelper>()
val interval = prefInterval ?: preferences.backupInterval().getOrDefault() val interval = prefInterval ?: preferences.backupInterval().getOrDefault()
if (interval > 0) { if (interval > 0) {
@ -38,9 +38,9 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
.addTag(TAG) .addTag(TAG)
.build() .build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} else { } else {
WorkManager.getInstance().cancelAllWorkByTag(TAG) WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
} }
} }
} }

View file

@ -25,7 +25,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
companion object { companion object {
const val TAG = "LibraryUpdate" const val TAG = "LibraryUpdate"
fun setupTask(prefInterval: Int? = null) { fun setupTask(context: Context, prefInterval: Int? = null) {
val preferences = Injekt.get<PreferencesHelper>() val preferences = Injekt.get<PreferencesHelper>()
val interval = prefInterval ?: preferences.libraryUpdateInterval().getOrDefault() val interval = prefInterval ?: preferences.libraryUpdateInterval().getOrDefault()
if (interval > 0) { if (interval > 0) {
@ -48,9 +48,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
.setConstraints(constraints) .setConstraints(constraints)
.build() .build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} else { } else {
WorkManager.getInstance().cancelAllWorkByTag(TAG) WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
} }
} }
} }

View file

@ -57,7 +57,7 @@ class UpdaterJob(private val context: Context, workerParams: WorkerParameters) :
companion object { companion object {
const val TAG = "UpdateChecker" const val TAG = "UpdateChecker"
fun setupTask() { fun setupTask(context: Context) {
val constraints = Constraints.Builder() val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED) .setRequiredNetworkType(NetworkType.CONNECTED)
.build() .build()
@ -69,11 +69,11 @@ class UpdaterJob(private val context: Context, workerParams: WorkerParameters) :
.setConstraints(constraints) .setConstraints(constraints)
.build() .build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} }
fun cancelTask() { fun cancelTask(context: Context) {
WorkManager.getInstance().cancelAllWorkByTag(TAG) WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
} }
} }
} }

View file

@ -62,9 +62,9 @@ class AboutController : SettingsController() {
onChange { newValue -> onChange { newValue ->
val checked = newValue as Boolean val checked = newValue as Boolean
if (checked) { if (checked) {
UpdaterJob.setupTask() UpdaterJob.setupTask(context)
} else { } else {
UpdaterJob.cancelTask() UpdaterJob.cancelTask(context)
} }
true true
} }

View file

@ -106,7 +106,7 @@ class SettingsBackupController : SettingsController() {
onChange { newValue -> onChange { newValue ->
val interval = (newValue as String).toInt() val interval = (newValue as String).toInt()
BackupCreatorJob.setupTask(interval) BackupCreatorJob.setupTask(context, interval)
true true
} }
} }

View file

@ -87,7 +87,7 @@ class SettingsLibraryController : SettingsController() {
onChange { newValue -> onChange { newValue ->
val interval = (newValue as String).toInt() val interval = (newValue as String).toInt()
LibraryUpdateJob.setupTask(interval) LibraryUpdateJob.setupTask(context, interval)
true true
} }
} }
@ -103,7 +103,7 @@ class SettingsLibraryController : SettingsController() {
onChange { onChange {
// Post to event looper to allow the preference to be updated. // Post to event looper to allow the preference to be updated.
Handler().post { LibraryUpdateJob.setupTask() } Handler().post { LibraryUpdateJob.setupTask(context) }
true true
} }
} }