diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt index f5e704e85..43752b622 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -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) { - refresh() - } - } - .delay(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread()) - .subscribe { initiated: Boolean -> - if (initiated) { - setSyncOngoing() - } - }) + lifecycleScope.launch { + if (syncAdapters.sync(true)) { + delay(1000) + setSyncOngoing() + } else { + refresh() + } + } } 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) } - syncAdapters.sync() + lifecycleScope.launch { + syncAdapters.sync() + } loadTaskListContent() } diff --git a/app/src/main/java/org/tasks/jobs/AfterSaveWork.kt b/app/src/main/java/org/tasks/jobs/AfterSaveWork.kt index f93d142c8..dc52c442b 100644 --- a/app/src/main/java/org/tasks/jobs/AfterSaveWork.kt +++ b/app/src/main/java/org/tasks/jobs/AfterSaveWork.kt @@ -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) diff --git a/app/src/main/java/org/tasks/jobs/SyncWork.kt b/app/src/main/java/org/tasks/jobs/SyncWork.kt index a3d49e1f8..62bcc7abf 100644 --- a/app/src/main/java/org/tasks/jobs/SyncWork.kt +++ b/app/src/main/java/org/tasks/jobs/SyncWork.kt @@ -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) { diff --git a/app/src/main/java/org/tasks/sync/SyncAdapters.java b/app/src/main/java/org/tasks/sync/SyncAdapters.java deleted file mode 100644 index c7b56f529..000000000 --- a/app/src/main/java/org/tasks/sync/SyncAdapters.java +++ /dev/null @@ -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 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; - } -} diff --git a/app/src/main/java/org/tasks/sync/SyncAdapters.kt b/app/src/main/java/org/tasks/sync/SyncAdapters.kt new file mode 100644 index 000000000..72533c434 --- /dev/null +++ b/app/src/main/java/org/tasks/sync/SyncAdapters.kt @@ -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 +} \ No newline at end of file