Prevent duplicate accounts when not pro

pull/1086/head
Alex Baker 4 years ago
parent f6844a7c07
commit 9f760c6bfb

@ -28,10 +28,7 @@ class OpenTaskDao @Inject constructor(
private val tasks = Tasks.getContentUri(authority)
private val properties = Properties.getContentUri(authority)
suspend fun newAccounts(): List<String> =
getListsByAccount()
.newAccounts(caldavDao)
.map { it.key }
suspend fun newAccounts(): List<String> = getListsByAccount().newAccounts(caldavDao)
suspend fun getListsByAccount(): Map<String, List<CaldavCalendar>> =
getLists().groupBy { it.account!! }
@ -248,6 +245,8 @@ class OpenTaskDao @Inject constructor(
suspend fun Map<String, List<CaldavCalendar>>.newAccounts(caldavDao: CaldavDao) =
filterNot { (_, lists) -> caldavDao.anyExist(lists.map { it.url!! }) }
.map { it.key }
.distinct()
fun Cursor.getString(columnName: String): String? =
getString(getColumnIndex(columnName))

@ -58,13 +58,15 @@ class OpenTasksSynchronizer @Inject constructor(
suspend fun sync() {
val lists = openTaskDao.getListsByAccount()
lists.newAccounts(caldavDao).forEach { (account, _) ->
caldavDao.insert(CaldavAccount().apply {
name = account.split(":")[1]
uuid = account
accountType = CaldavAccount.TYPE_OPENTASKS
})
}
lists.newAccounts(caldavDao)
.filter { caldavDao.getAccountByUuid(it) == null }
.forEach {
caldavDao.insert(CaldavAccount().apply {
name = it.split(":")[1]
uuid = it
accountType = CaldavAccount.TYPE_OPENTASKS
})
}
caldavDao.getAccounts(CaldavAccount.TYPE_OPENTASKS).forEach { account ->
if (!lists.containsKey(account.uuid)) {
setError(account, context.getString(R.string.account_not_found))

Loading…
Cancel
Save