mirror of https://github.com/tasks/tasks
Convert SyncAdapters to Kotlin
parent
c4e3f72be1
commit
dd0650af51
@ -1,51 +0,0 @@
|
||||
package org.tasks.sync;
|
||||
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import javax.inject.Inject;
|
||||
import org.tasks.data.CaldavDaoBlocking;
|
||||
import org.tasks.data.GoogleTaskListDaoBlocking;
|
||||
import org.tasks.jobs.WorkManager;
|
||||
|
||||
public class SyncAdapters {
|
||||
|
||||
private final WorkManager workManager;
|
||||
private final CaldavDaoBlocking caldavDao;
|
||||
private final GoogleTaskListDaoBlocking googleTaskListDao;
|
||||
|
||||
@Inject
|
||||
public SyncAdapters(
|
||||
WorkManager workManager, CaldavDaoBlocking caldavDao, GoogleTaskListDaoBlocking googleTaskListDao) {
|
||||
this.workManager = workManager;
|
||||
this.caldavDao = caldavDao;
|
||||
this.googleTaskListDao = googleTaskListDao;
|
||||
}
|
||||
|
||||
public void sync() {
|
||||
sync(false).subscribe();
|
||||
}
|
||||
|
||||
public Single<Boolean> sync(boolean immediate) {
|
||||
return Single.fromCallable(this::isSyncEnabled)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnSuccess(enabled -> {
|
||||
if (enabled) {
|
||||
workManager.sync(immediate);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isSyncEnabled() {
|
||||
return isGoogleTaskSyncEnabled() || isCaldavSyncEnabled();
|
||||
}
|
||||
|
||||
public boolean isGoogleTaskSyncEnabled() {
|
||||
return googleTaskListDao.getAccounts().size() > 0;
|
||||
}
|
||||
|
||||
public boolean isCaldavSyncEnabled() {
|
||||
return caldavDao.accountCount() > 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package org.tasks.sync
|
||||
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.tasks.data.CaldavDao
|
||||
import org.tasks.data.GoogleTaskListDao
|
||||
import org.tasks.jobs.WorkManager
|
||||
import javax.inject.Inject
|
||||
|
||||
class SyncAdapters @Inject constructor(
|
||||
private val workManager: WorkManager,
|
||||
private val caldavDao: CaldavDao,
|
||||
private val googleTaskListDao: GoogleTaskListDao) {
|
||||
|
||||
suspend fun sync() {
|
||||
sync(false)
|
||||
}
|
||||
|
||||
suspend fun sync(immediate: Boolean): Boolean = withContext(NonCancellable) {
|
||||
if (isSyncEnabled()) {
|
||||
workManager.sync(immediate)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun isSyncEnabled(): Boolean = isGoogleTaskSyncEnabled() || isCaldavSyncEnabled()
|
||||
|
||||
suspend fun isGoogleTaskSyncEnabled(): Boolean = googleTaskListDao.getAccounts().isNotEmpty()
|
||||
|
||||
suspend fun isCaldavSyncEnabled(): Boolean = caldavDao.accountCount() > 0
|
||||
}
|
Loading…
Reference in New Issue