Using context for workmanager

been needing to
This commit is contained in:
Jays2Kings 2021-07-03 20:13:50 -04:00
parent a3a4edbde0
commit bb3168cdb1
10 changed files with 32 additions and 32 deletions

View File

@ -34,19 +34,19 @@ object Migrations {
if (oldVersion == 0) { if (oldVersion == 0) {
if (BuildConfig.INCLUDE_UPDATER) { if (BuildConfig.INCLUDE_UPDATER) {
UpdaterJob.setupTask() UpdaterJob.setupTask(context)
} }
ExtensionUpdateJob.setupTask() ExtensionUpdateJob.setupTask(context)
LibraryUpdateJob.setupTask() LibraryUpdateJob.setupTask(context)
return BuildConfig.DEBUG return BuildConfig.DEBUG
} }
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) { if (BuildConfig.INCLUDE_UPDATER) {
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.
@ -81,11 +81,11 @@ object Migrations {
LibraryPresenter.updateDB() LibraryPresenter.updateDB()
// Restore jobs after migrating from Evernote's job scheduler to WorkManager. // Restore jobs after migrating from Evernote's job scheduler to WorkManager.
if (BuildConfig.INCLUDE_UPDATER) { if (BuildConfig.INCLUDE_UPDATER) {
UpdaterJob.setupTask() UpdaterJob.setupTask(context)
} }
LibraryUpdateJob.setupTask() LibraryUpdateJob.setupTask(context)
BackupCreatorJob.setupTask(context) BackupCreatorJob.setupTask(context)
ExtensionUpdateJob.setupTask() ExtensionUpdateJob.setupTask(context)
} }
if (oldVersion < 66) { if (oldVersion < 66) {
LibraryPresenter.updateCustoms() LibraryPresenter.updateCustoms()
@ -118,7 +118,7 @@ object Migrations {
if (oldVersion < 74) { if (oldVersion < 74) {
// Turn on auto updates for all users // Turn on auto updates for all users
if (BuildConfig.INCLUDE_UPDATER) { if (BuildConfig.INCLUDE_UPDATER) {
UpdaterJob.setupTask() UpdaterJob.setupTask(context)
} }
} }
if (oldVersion < 75) { if (oldVersion < 75) {
@ -138,7 +138,7 @@ object Migrations {
val updateInterval = preferences.libraryUpdateInterval().get() val updateInterval = preferences.libraryUpdateInterval().get()
if (updateInterval == 1 || updateInterval == 2) { if (updateInterval == 1 || updateInterval == 2) {
preferences.libraryUpdateInterval().set(3) preferences.libraryUpdateInterval().set(3)
LibraryUpdateJob.setupTask(3) LibraryUpdateJob.setupTask(context, 3)
} }
} }
return true return true

View File

@ -48,9 +48,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 {
private const val TAG = "LibraryUpdate" private 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) {
@ -52,9 +52,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

@ -26,16 +26,16 @@ class DelayedLibrarySuggestionsJob(context: Context, workerParams: WorkerParamet
companion object { companion object {
private const val TAG = "DelayedLibrarySuggestions" private const val TAG = "DelayedLibrarySuggestions"
fun setupTask(enabled: Boolean) { fun setupTask(context: Context, enabled: Boolean) {
if (enabled) { if (enabled) {
val request = OneTimeWorkRequestBuilder<DelayedLibrarySuggestionsJob>() val request = OneTimeWorkRequestBuilder<DelayedLibrarySuggestionsJob>()
.setInitialDelay(1, TimeUnit.DAYS) .setInitialDelay(1, TimeUnit.DAYS)
.addTag(TAG) .addTag(TAG)
.build() .build()
WorkManager.getInstance().enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, request) WorkManager.getInstance(context).enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, request)
} else { } else {
WorkManager.getInstance().cancelAllWorkByTag(TAG) WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
} }
} }
} }

View File

@ -41,7 +41,7 @@ class UpdaterJob(private val context: Context, workerParams: WorkerParameters) :
companion object { companion object {
private const val TAG = "UpdateChecker" private 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()
@ -56,11 +56,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

@ -75,7 +75,7 @@ class ExtensionUpdateJob(private val context: Context, workerParams: WorkerParam
companion object { companion object {
private const val TAG = "ExtensionUpdate" private const val TAG = "ExtensionUpdate"
fun setupTask(forceAutoUpdateJob: Boolean? = null) { fun setupTask(context: Context, forceAutoUpdateJob: Boolean? = null) {
val preferences = Injekt.get<PreferencesHelper>() val preferences = Injekt.get<PreferencesHelper>()
val autoUpdateJob = forceAutoUpdateJob ?: preferences.automaticExtUpdates().getOrDefault() val autoUpdateJob = forceAutoUpdateJob ?: preferences.automaticExtUpdates().getOrDefault()
if (autoUpdateJob) { if (autoUpdateJob) {
@ -93,9 +93,9 @@ class ExtensionUpdateJob(private val context: Context, workerParams: WorkerParam
.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

@ -106,7 +106,7 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
updatePref(preferences.libraryUpdateCategories(), binding.includeGlobal) == false updatePref(preferences.libraryUpdateCategories(), binding.includeGlobal) == false
) { ) {
preferences.libraryUpdateInterval().set(0) preferences.libraryUpdateInterval().set(0)
LibraryUpdateJob.setupTask(0) LibraryUpdateJob.setupTask(preferences.context, 0)
} }
updateLibrary?.invoke(category.id) updateLibrary?.invoke(category.id)
return true return true

View File

@ -108,7 +108,7 @@ class LibraryPresenter(
lastLibraryItems = null lastLibraryItems = null
getLibrary() getLibrary()
if (preferences.showLibrarySearchSuggestions().isNotSet()) { if (preferences.showLibrarySearchSuggestions().isNotSet()) {
DelayedLibrarySuggestionsJob.setupTask(true) DelayedLibrarySuggestionsJob.setupTask(context, true)
} else if (preferences.showLibrarySearchSuggestions().get() && } else if (preferences.showLibrarySearchSuggestions().get() &&
Date().time >= preferences.lastLibrarySuggestion().get() + TimeUnit.HOURS.toMillis(2) Date().time >= preferences.lastLibrarySuggestion().get() + TimeUnit.HOURS.toMillis(2)
) { ) {

View File

@ -30,7 +30,7 @@ class SettingsBrowseController : SettingsController() {
onChange { onChange {
it as Boolean it as Boolean
ExtensionUpdateJob.setupTask(it) ExtensionUpdateJob.setupTask(context, it)
true true
} }
} }

View File

@ -43,7 +43,7 @@ class SettingsLibraryController : SettingsController() {
LibraryPresenter.setSearchSuggestion(preferences, db, Injekt.get()) LibraryPresenter.setSearchSuggestion(preferences, db, Injekt.get())
} }
} else { } else {
DelayedLibrarySuggestionsJob.setupTask(false) DelayedLibrarySuggestionsJob.setupTask(context, false)
preferences.librarySearchSuggestion().set("") preferences.librarySearchSuggestion().set("")
} }
true true
@ -115,11 +115,11 @@ class SettingsLibraryController : SettingsController() {
onChange { newValue -> onChange { newValue ->
// Always cancel the previous task, it seems that sometimes they are not updated. // Always cancel the previous task, it seems that sometimes they are not updated.
LibraryUpdateJob.setupTask(0) LibraryUpdateJob.setupTask(context, 0)
val interval = newValue as Int val interval = newValue as Int
if (interval > 0) { if (interval > 0) {
LibraryUpdateJob.setupTask(interval) LibraryUpdateJob.setupTask(context, interval)
} }
true true
} }
@ -136,7 +136,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
} }
} }