Add sync worker

This commit is contained in:
Shinokuni 2020-02-04 22:28:04 +01:00
parent 6c10ceb009
commit 6d3f517790
5 changed files with 116 additions and 4 deletions

View File

@ -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<Integer, TimeUnit> 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<Integer, TimeUnit> 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);
}
}

View File

@ -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
}
}

View File

@ -62,6 +62,6 @@ public class AddFeedsViewModel extends AndroidViewModel {
}
public LiveData<List<Account>> getAccounts() {
return database.accountDao().selectAll();
return database.accountDao().selectAllAsync();
}
}

View File

@ -133,7 +133,7 @@ public class MainViewModel extends AndroidViewModel {
//region Account
public LiveData<List<Account>> getAllAccounts() {
return db.accountDao().selectAll();
return db.accountDao().selectAllAsync();
}
private Completable deselectOldCurrentAccount(int accountId) {

View File

@ -14,7 +14,10 @@ import io.reactivex.Single;
public abstract class AccountDao implements BaseDao<Account> {
@Query("Select * from Account")
public abstract LiveData<List<Account>> selectAll();
public abstract LiveData<List<Account>> selectAllAsync();
@Query("Select * from Account")
public abstract List<Account> selectAll();
@Query("Update Account set last_modified = :lastModified Where id = :accountId")
public abstract void updateLastModified(int accountId, long lastModified);