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

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

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