Commit d9c29204 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Cancel the automatic refresh if it is disabled

parent 5a001f40
Pipeline #5077 passed with stage
in 1 minute and 45 seconds
...@@ -137,34 +137,35 @@ class DnsRuleFragment : Fragment() { ...@@ -137,34 +137,35 @@ class DnsRuleFragment : Fragment() {
} }
}, refreshConfigChanged = { }, refreshConfigChanged = {
getPreferences().apply { getPreferences().apply {
val constraints = Constraints.Builder() val workManager = WorkManager.getInstance(context!!)
.setRequiresStorageNotLow(true) workManager.cancelAllWorkByTag("hostSourceRefresh")
.setRequiresBatteryNotLow(true) if(automaticHostRefresh) {
.setRequiredNetworkType(if (this.automaticHostRefreshWifiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED) val constraints = Constraints.Builder()
.build() .setRequiresStorageNotLow(true)
val mappedTimeAmount = automaticHostRefreshTimeAmount.let { .setRequiresBatteryNotLow(true)
if (automaticHostRefreshTimeUnit == HostSourceRefreshDialog.TimeUnit.WEEKS) it * 7 .setRequiredNetworkType(if (this.automaticHostRefreshWifiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
else it .build()
}.toLong() val mappedTimeAmount = automaticHostRefreshTimeAmount.let {
val mappedTimeUnit = automaticHostRefreshTimeUnit.let { if (automaticHostRefreshTimeUnit == HostSourceRefreshDialog.TimeUnit.WEEKS) it * 7
when (it) { else it
HostSourceRefreshDialog.TimeUnit.WEEKS -> TimeUnit.DAYS }.toLong()
HostSourceRefreshDialog.TimeUnit.DAYS -> TimeUnit.DAYS val mappedTimeUnit = automaticHostRefreshTimeUnit.let {
HostSourceRefreshDialog.TimeUnit.HOURS -> TimeUnit.HOURS when (it) {
HostSourceRefreshDialog.TimeUnit.MINUTES -> TimeUnit.MINUTES HostSourceRefreshDialog.TimeUnit.WEEKS -> TimeUnit.DAYS
HostSourceRefreshDialog.TimeUnit.DAYS -> TimeUnit.DAYS
HostSourceRefreshDialog.TimeUnit.HOURS -> TimeUnit.HOURS
HostSourceRefreshDialog.TimeUnit.MINUTES -> TimeUnit.MINUTES
}
} }
} val workRequest = PeriodicWorkRequest.Builder(RuleImportStartWorker::class.java,
val workRequest = PeriodicWorkRequest.Builder(RuleImportStartWorker::class.java, mappedTimeAmount,
mappedTimeAmount, mappedTimeUnit)
mappedTimeUnit) .setConstraints(constraints)
.setConstraints(constraints) .setInitialDelay(mappedTimeAmount, mappedTimeUnit)
.setInitialDelay(mappedTimeAmount, mappedTimeUnit) .addTag("hostSourceRefresh")
.addTag("hostSourceRefresh")
WorkManager.getInstance(context!!).apply {
cancelAllWorkByTag("hostSourceRefresh")
enqueue(workRequest.build())
}
workManager.enqueue(workRequest.build())
}
} }
}).show() }).show()
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment