From 363eaf9bf917c3e6be6ab1c43f9be38d858a25da Mon Sep 17 00:00:00 2001 From: Amine Date: Sat, 3 Nov 2018 18:14:22 +0100 Subject: [PATCH] Preferences for the background tasks. --- .../bou/readerforselfoss/HomeActivity.kt | 37 ++++++++++++------- app/src/main/res/values/strings.xml | 5 +++ app/src/main/res/xml/pref_offline.xml | 24 ++++++++++++ 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index e79063f..c3fbb70 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -27,6 +27,7 @@ import androidx.room.Room import androidx.room.RoomDatabase import androidx.work.Constraints import androidx.work.ExistingPeriodicWorkPolicy +import androidx.work.NetworkType import androidx.work.OneTimeWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager @@ -118,6 +119,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { private var itemsCaching: Boolean = false private var hiddenTags: List = emptyList() + private var periodicRefresh = false + private var refreshMinutes: Long = 360L + private var refreshWhenChargingOnly = false + private lateinit var tabNewBadge: TextBadgeItem private lateinit var tabArchiveBadge: TextBadgeItem private lateinit var tabStarredBadge: TextBadgeItem @@ -393,6 +398,13 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { } else { emptyList() } + periodicRefresh = sharedPref.getBoolean("periodic_refresh", false) + refreshWhenChargingOnly = sharedPref.getBoolean("refresh_when_charging", false) + refreshMinutes = sharedPref.getString("periodic_refresh_minutes", "360").toLong() + + if (refreshMinutes <= 15) { + refreshMinutes = 15 + } } private fun handleThemeBinding() { @@ -1399,23 +1411,22 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { } private fun handleRecurringTask() { - // TODO: add network type - val myConstraints = Constraints.Builder() - .setRequiresBatteryNotLow(true) - .setRequiresStorageNotLow(true) - .build() - - // TODO: make the time variable from the settings. - val backgroundWork = - PeriodicWorkRequestBuilder(4, TimeUnit.HOURS) - .setConstraints(myConstraints) - .addTag("selfoss-loading") + if (periodicRefresh) { + val myConstraints = Constraints.Builder() + .setRequiresBatteryNotLow(true) + .setRequiresCharging(refreshWhenChargingOnly) + .setRequiresStorageNotLow(true) .build() - - WorkManager.getInstance().enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.REPLACE, backgroundWork) + val backgroundWork = + PeriodicWorkRequestBuilder(refreshMinutes, TimeUnit.MINUTES) + .setConstraints(myConstraints) + .addTag("selfoss-loading") + .build() + WorkManager.getInstance().enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.KEEP, backgroundWork) + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a69accd..a935b3f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -153,4 +153,9 @@ Articles will be saved to the device memory and will be used for offline use. Save items for offline use Not connected ! + Sync articles + Articles will not be synced in the background + Articles will periodically be synced + = 15 minutes)]]> + Only refresh when phone is charging diff --git a/app/src/main/res/xml/pref_offline.xml b/app/src/main/res/xml/pref_offline.xml index 35e62a0..442f745 100644 --- a/app/src/main/res/xml/pref_offline.xml +++ b/app/src/main/res/xml/pref_offline.xml @@ -5,4 +5,28 @@ android:summaryOff="@string/pref_switch_items_caching_off" android:summaryOn="@string/pref_switch_items_caching_on" android:title="@string/pref_switch_items_caching" /> + + + + + + +