From c9e1ec97eb3f6fc979ec93a22f9c99da980795ee Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 23 Jul 2020 14:08:19 -0500 Subject: [PATCH] Prevent creation of multiple local accounts --- app/src/main/java/org/tasks/data/CaldavDao.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/tasks/data/CaldavDao.kt b/app/src/main/java/org/tasks/data/CaldavDao.kt index 9489b6805..c29eab22f 100644 --- a/app/src/main/java/org/tasks/data/CaldavDao.kt +++ b/app/src/main/java/org/tasks/data/CaldavDao.kt @@ -8,6 +8,8 @@ import com.todoroo.astrid.api.FilterListItem.NO_ORDER import com.todoroo.astrid.core.SortHelper.APPLE_EPOCH import com.todoroo.astrid.data.Task import com.todoroo.astrid.helper.UUIDHelper +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import org.tasks.R import org.tasks.date.DateTimeUtils.toAppleEpoch import org.tasks.db.SuspendDbUtils.chunkedMap @@ -251,13 +253,15 @@ abstract class CaldavDao { @Query("UPDATE caldav_lists SET cdl_order = :order WHERE cdl_id = :id") abstract suspend fun setOrder(id: Long, order: Int) - suspend fun setupLocalAccount(context: Context): CaldavAccount { + suspend fun setupLocalAccount(context: Context): CaldavAccount = mutex.withLock { val account = getLocalAccount() getLocalList(context, account) return account } - suspend fun getLocalList(context: Context) = getLocalList(context, getLocalAccount()) + suspend fun getLocalList(context: Context) = mutex.withLock { + getLocalList(context, getLocalAccount()) + } private suspend fun getLocalAccount() = getAccountByUuid(LOCAL) ?: CaldavAccount().apply { accountType = CaldavAccount.TYPE_LOCAL @@ -274,5 +278,6 @@ abstract class CaldavDao { companion object { const val LOCAL = "local" + private val mutex = Mutex() } } \ No newline at end of file