diff --git a/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java b/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java index d1730b30..f0f11fee 100644 --- a/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java @@ -2,16 +2,23 @@ package com.readrops.app.fragments.settings; import android.content.Intent; import android.os.Bundle; +import android.util.Pair; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatDelegate; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; import com.readrops.app.R; -import com.readrops.readropsdb.Database; +import com.readrops.app.utils.SyncWorker; import com.readrops.app.utils.feedscolors.FeedsColorsIntentService; +import com.readrops.readropsdb.Database; import java.util.ArrayList; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import static com.readrops.app.utils.ReadropsKeys.FEEDS; @@ -24,6 +31,8 @@ public class SettingsFragment extends PreferenceFragmentCompat { Preference feedsColorsPreference = findPreference("reload_feeds_colors"); Preference themePreference = findPreference("dark_theme"); + Preference synchroPreference = findPreference("auto_synchro"); + AtomicBoolean serviceStarted = new AtomicBoolean(false); feedsColorsPreference.setOnPreferenceClickListener(preference -> { @@ -53,6 +62,68 @@ public class SettingsFragment extends PreferenceFragmentCompat { return true; }); + + synchroPreference.setOnPreferenceChangeListener(((preference, newValue) -> { + WorkManager workManager = WorkManager.getInstance(getContext()); + Pair interval = getWorkerInterval((String) newValue); + + if (interval != null) { + PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(SyncWorker.class, interval.first, interval.second) + .addTag(SyncWorker.Companion.getTAG()) + .build(); + + workManager.enqueueUniquePeriodicWork(SyncWorker.Companion.getTAG(), ExistingPeriodicWorkPolicy.REPLACE, request); + } else { + workManager.cancelAllWorkByTag(SyncWorker.Companion.getTAG()); + } + + return true; + })); + } + + @Nullable + private Pair getWorkerInterval(String newValue) { + int interval; + TimeUnit timeUnit; + + switch (newValue) { + case "0.15": + interval = 15; + timeUnit = TimeUnit.MINUTES; + break; + case "0.30": + interval = 30; + timeUnit = TimeUnit.MINUTES; + break; + case "1": + interval = 1; + timeUnit = TimeUnit.HOURS; + break; + case "2": + interval = 2; + timeUnit = TimeUnit.HOURS; + break; + case "3": + interval = 3; + timeUnit = TimeUnit.HOURS; + break; + case "6": + interval = 6; + timeUnit = TimeUnit.HOURS; + break; + case "12": + interval = 12; + timeUnit = TimeUnit.HOURS; + break; + case "24": + interval = 1; + timeUnit = TimeUnit.DAYS; + break; + default: + return null; + } + + return new Pair<>(interval, timeUnit); } } diff --git a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt new file mode 100644 index 00000000..d99fb628 --- /dev/null +++ b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt @@ -0,0 +1,38 @@ +package com.readrops.app.utils + +import android.content.Context +import androidx.work.Worker +import androidx.work.WorkerParameters +import com.readrops.app.repositories.ARepository +import com.readrops.readropsdb.Database +import io.reactivex.disposables.Disposable + +class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) { + + private lateinit var disposable: Disposable + + override fun doWork(): Result { + val database = Database.getInstance(applicationContext) + val accounts = database.accountDao().selectAll() + var result = Result.success() + + accounts.forEach { + val repository = ARepository.repositoryFactory(it, applicationContext) + + disposable = repository.sync(null) + .doOnError { result = Result.failure() } + .subscribe() + } + + return result + } + + override fun onStopped() { + super.onStopped() + disposable.dispose() + } + + companion object { + val TAG = SyncWorker::class.java.simpleName + } +} \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java index d656476d..0c5d4108 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/AddFeedsViewModel.java @@ -62,6 +62,6 @@ public class AddFeedsViewModel extends AndroidViewModel { } public LiveData> getAccounts() { - return database.accountDao().selectAll(); + return database.accountDao().selectAllAsync(); } } diff --git a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java index b368e759..b2f129b5 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java @@ -133,7 +133,7 @@ public class MainViewModel extends AndroidViewModel { //region Account public LiveData> getAllAccounts() { - return db.accountDao().selectAll(); + return db.accountDao().selectAllAsync(); } private Completable deselectOldCurrentAccount(int accountId) { diff --git a/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java b/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java index 7f426457..ea7d5cdd 100644 --- a/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java +++ b/readropsdb/src/main/java/com/readrops/readropsdb/dao/AccountDao.java @@ -14,7 +14,10 @@ import io.reactivex.Single; public abstract class AccountDao implements BaseDao { @Query("Select * from Account") - public abstract LiveData> selectAll(); + public abstract LiveData> selectAllAsync(); + + @Query("Select * from Account") + public abstract List selectAll(); @Query("Update Account set last_modified = :lastModified Where id = :accountId") public abstract void updateLastModified(int accountId, long lastModified);