Convert SyncAdapters to Kotlin

pull/1055/head
Alex Baker 4 years ago
parent c4e3f72be1
commit dd0650af51

@ -50,6 +50,7 @@ import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.tasks.LocalBroadcastManager
@ -92,7 +93,6 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
TaskViewHolder.ViewHolderCallbacks {
private val refreshReceiver = RefreshReceiver()
private val repeatConfirmationReceiver = RepeatConfirmationReceiver()
private var disposables: CompositeDisposable? = null
@Inject lateinit var syncAdapters: SyncAdapters
@Inject lateinit var taskDeleter: TaskDeleter
@ -146,20 +146,14 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
private lateinit var callbacks: TaskListFragmentCallbackHandler
override fun onRefresh() {
disposables!!.add(
syncAdapters
.sync(true)
.doOnSuccess { initiated: Boolean ->
if (!initiated) {
lifecycleScope.launch {
if (syncAdapters.sync(true)) {
delay(1000)
setSyncOngoing()
} else {
refresh()
}
}
.delay(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.subscribe { initiated: Boolean ->
if (initiated) {
setSyncOngoing()
}
})
}
private fun setSyncOngoing() {
@ -445,7 +439,6 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
override fun onResume() {
super.onResume()
disposables = CompositeDisposable()
localBroadcastManager.registerRefreshReceiver(refreshReceiver)
localBroadcastManager.registerRepeatReceiver(repeatConfirmationReceiver)
refresh()
@ -465,7 +458,6 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
override fun onPause() {
super.onPause()
disposables?.dispose()
localBroadcastManager.unregisterReceiver(repeatConfirmationReceiver)
localBroadcastManager.unregisterReceiver(refreshReceiver)
}
@ -499,7 +491,9 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
for (task in tasks) {
onTaskCreated(task.uuid)
}
lifecycleScope.launch {
syncAdapters.sync()
}
loadTaskListContent()
}

@ -71,8 +71,8 @@ class AfterSaveWork @WorkerInject constructor(
timerPlugin.stopTimer(task)
}
}
if (data.getBoolean(EXTRA_PUSH_GTASKS, false) && syncAdapters.isGoogleTaskSyncEnabled
|| data.getBoolean(EXTRA_PUSH_CALDAV, false) && syncAdapters.isCaldavSyncEnabled) {
if (data.getBoolean(EXTRA_PUSH_GTASKS, false) && syncAdapters.isGoogleTaskSyncEnabled()
|| data.getBoolean(EXTRA_PUSH_CALDAV, false) && syncAdapters.isCaldavSyncEnabled()) {
workManager.sync(false)
}
refreshScheduler.scheduleRefresh(task)

@ -31,7 +31,7 @@ class SyncWork @WorkerInject constructor(
private val syncAdapters: SyncAdapters) : BaseWorker(context, workerParams, firebase) {
override suspend fun run(): Result {
if (!syncAdapters.isSyncEnabled) {
if (!syncAdapters.isSyncEnabled()) {
return Result.success()
}
synchronized(LOCK) {

@ -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…
Cancel
Save