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() {
}
}, refreshConfigChanged = {
getPreferences().apply {
val constraints = Constraints.Builder()
.setRequiresStorageNotLow(true)
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(if (this.automaticHostRefreshWifiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
.build()
val mappedTimeAmount = automaticHostRefreshTimeAmount.let {
if (automaticHostRefreshTimeUnit == HostSourceRefreshDialog.TimeUnit.WEEKS) it * 7
else it
}.toLong()
val mappedTimeUnit = automaticHostRefreshTimeUnit.let {
when (it) {
HostSourceRefreshDialog.TimeUnit.WEEKS -> TimeUnit.DAYS
HostSourceRefreshDialog.TimeUnit.DAYS -> TimeUnit.DAYS
HostSourceRefreshDialog.TimeUnit.HOURS -> TimeUnit.HOURS
HostSourceRefreshDialog.TimeUnit.MINUTES -> TimeUnit.MINUTES
val workManager = WorkManager.getInstance(context!!)
workManager.cancelAllWorkByTag("hostSourceRefresh")
if(automaticHostRefresh) {
val constraints = Constraints.Builder()
.setRequiresStorageNotLow(true)
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(if (this.automaticHostRefreshWifiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
.build()
val mappedTimeAmount = automaticHostRefreshTimeAmount.let {
if (automaticHostRefreshTimeUnit == HostSourceRefreshDialog.TimeUnit.WEEKS) it * 7
else it
}.toLong()
val mappedTimeUnit = automaticHostRefreshTimeUnit.let {
when (it) {
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,
mappedTimeAmount,
mappedTimeUnit)
.setConstraints(constraints)
.setInitialDelay(mappedTimeAmount, mappedTimeUnit)
.addTag("hostSourceRefresh")
WorkManager.getInstance(context!!).apply {
cancelAllWorkByTag("hostSourceRefresh")
enqueue(workRequest.build())
}
val workRequest = PeriodicWorkRequest.Builder(RuleImportStartWorker::class.java,
mappedTimeAmount,
mappedTimeUnit)
.setConstraints(constraints)
.setInitialDelay(mappedTimeAmount, mappedTimeUnit)
.addTag("hostSourceRefresh")
workManager.enqueue(workRequest.build())
}
}
}).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