From a04f617fff112337cdc2972b9f0e7983c9ef3584 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 4 Aug 2020 16:00:01 -0500 Subject: [PATCH] Convert GtasksListService to Kotlin --- .../astrid/gtasks/GtasksListServiceTest.kt | 19 ++-- .../astrid/gtasks/GtasksListService.java | 88 ------------------- .../astrid/gtasks/GtasksListService.kt | 69 +++++++++++++++ .../tasks/data/GoogleTaskListDaoBlocking.kt | 31 ------- 4 files changed, 79 insertions(+), 128 deletions(-) delete mode 100644 app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.java create mode 100644 app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.kt delete mode 100644 app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt diff --git a/app/src/androidTest/java/com/todoroo/astrid/gtasks/GtasksListServiceTest.kt b/app/src/androidTest/java/com/todoroo/astrid/gtasks/GtasksListServiceTest.kt index d7018b7a3..18ed57651 100644 --- a/app/src/androidTest/java/com/todoroo/astrid/gtasks/GtasksListServiceTest.kt +++ b/app/src/androidTest/java/com/todoroo/astrid/gtasks/GtasksListServiceTest.kt @@ -5,13 +5,14 @@ import com.natpryce.makeiteasy.MakeItEasy.with import com.todoroo.astrid.service.TaskDeleter import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.UninstallModules +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Before import org.junit.Test import org.tasks.LocalBroadcastManager import org.tasks.data.GoogleTaskAccount -import org.tasks.data.GoogleTaskListDaoBlocking +import org.tasks.data.GoogleTaskListDao import org.tasks.injection.InjectingTestCase import org.tasks.injection.ProductionModule import org.tasks.makers.GtaskListMaker.ID @@ -27,7 +28,7 @@ import javax.inject.Inject class GtasksListServiceTest : InjectingTestCase() { @Inject lateinit var taskDeleter: TaskDeleter @Inject lateinit var localBroadcastManager: LocalBroadcastManager - @Inject lateinit var googleTaskListDao: GoogleTaskListDaoBlocking + @Inject lateinit var googleTaskListDao: GoogleTaskListDao private lateinit var gtasksListService: GtasksListService @@ -38,7 +39,7 @@ class GtasksListServiceTest : InjectingTestCase() { } @Test - fun testCreateNewList() { + fun testCreateNewList() = runBlocking { setLists( newRemoteList( with(RemoteGtaskListMaker.REMOTE_ID, "1"), with(RemoteGtaskListMaker.NAME, "Default"))) @@ -48,19 +49,19 @@ class GtasksListServiceTest : InjectingTestCase() { } @Test - fun testGetListByRemoteId() { + fun testGetListByRemoteId() = runBlocking { val list = newGtaskList(with(REMOTE_ID, "1")) list.id = googleTaskListDao.insertOrReplace(list) assertEquals(list, googleTaskListDao.getByRemoteId("1")) } @Test - fun testGetListReturnsNullWhenNotFound() { + fun testGetListReturnsNullWhenNotFound() = runBlocking { assertNull(googleTaskListDao.getByRemoteId("1")) } @Test - fun testDeleteMissingList() { + fun testDeleteMissingList() = runBlocking { googleTaskListDao.insertOrReplace(newGtaskList(with(ID, 1L), with(REMOTE_ID, "1"))) val taskList = newRemoteList(with(RemoteGtaskListMaker.REMOTE_ID, "2")) setLists(taskList) @@ -70,7 +71,7 @@ class GtasksListServiceTest : InjectingTestCase() { } @Test - fun testUpdateListName() { + fun testUpdateListName() = runBlocking { googleTaskListDao.insertOrReplace( newGtaskList(with(ID, 1L), with(REMOTE_ID, "1"), with(NAME, "oldName"))) setLists( @@ -80,12 +81,12 @@ class GtasksListServiceTest : InjectingTestCase() { } @Test - fun testNewListLastSyncIsZero() { + fun testNewListLastSyncIsZero() = runBlocking { setLists(TaskList().setId("1")) assertEquals(0L, googleTaskListDao.getByRemoteId("1")!!.lastSync) } - private fun setLists(vararg list: TaskList) { + private suspend fun setLists(vararg list: TaskList) { val account = GoogleTaskAccount("account") googleTaskListDao.insert(account) gtasksListService.updateLists(account, listOf(*list)) diff --git a/app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.java b/app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.java deleted file mode 100644 index 27c03d84e..000000000 --- a/app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2012 Todoroo Inc - * - * See the file "LICENSE" for the full license governing this code. - */ - -package com.todoroo.astrid.gtasks; - -import com.google.api.services.tasks.model.TaskList; -import com.todoroo.astrid.service.TaskDeleter; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.inject.Inject; -import org.tasks.LocalBroadcastManager; -import org.tasks.data.GoogleTaskAccount; -import org.tasks.data.GoogleTaskList; -import org.tasks.data.GoogleTaskListDaoBlocking; -import timber.log.Timber; - -public class GtasksListService { - - private final GoogleTaskListDaoBlocking googleTaskListDao; - private final TaskDeleter taskDeleter; - private final LocalBroadcastManager localBroadcastManager; - - @Inject - public GtasksListService( - GoogleTaskListDaoBlocking googleTaskListDao, - TaskDeleter taskDeleter, - LocalBroadcastManager localBroadcastManager) { - this.googleTaskListDao = googleTaskListDao; - this.taskDeleter = taskDeleter; - this.localBroadcastManager = localBroadcastManager; - } - - /** - * Reads in remote list information and updates local list objects. - * - * @param remoteLists remote information about your lists - */ - public synchronized void updateLists(GoogleTaskAccount account, List remoteLists) { - List lists = googleTaskListDao.getLists(account.getAccount()); - - Set previousLists = new HashSet<>(); - for (GoogleTaskList list : lists) { - previousLists.add(list.getId()); - } - - for (int i = 0; i < remoteLists.size(); i++) { - com.google.api.services.tasks.model.TaskList remote = remoteLists.get(i); - - String id = remote.getId(); - GoogleTaskList local = null; - for (GoogleTaskList list : lists) { - if (list.getRemoteId().equals(id)) { - local = list; - break; - } - } - - String title = remote.getTitle(); - if (local == null) { - GoogleTaskList byRemoteId = googleTaskListDao.findExistingList(id); - if (byRemoteId != null) { - byRemoteId.setAccount(account.getAccount()); - local = byRemoteId; - } else { - Timber.d("Adding new gtask list %s", title); - local = new GoogleTaskList(); - local.setAccount(account.getAccount()); - local.setRemoteId(id); - } - } - - local.setTitle(title); - googleTaskListDao.insertOrReplace(local); - previousLists.remove(local.getId()); - } - - // check for lists that aren't on remote server - for (Long listId : previousLists) { - taskDeleter.delete(googleTaskListDao.getById(listId)); - } - - localBroadcastManager.broadcastRefreshList(); - } -} diff --git a/app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.kt b/app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.kt new file mode 100644 index 000000000..453c20c21 --- /dev/null +++ b/app/src/main/java/com/todoroo/astrid/gtasks/GtasksListService.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2012 Todoroo Inc + * + * See the file "LICENSE" for the full license governing this code. + */ +package com.todoroo.astrid.gtasks + +import com.google.api.services.tasks.model.TaskList +import com.todoroo.astrid.service.TaskDeleter +import org.tasks.LocalBroadcastManager +import org.tasks.data.GoogleTaskAccount +import org.tasks.data.GoogleTaskList +import org.tasks.data.GoogleTaskListDao +import timber.log.Timber +import java.util.* +import javax.inject.Inject + +class GtasksListService @Inject constructor( + private val googleTaskListDao: GoogleTaskListDao, + private val taskDeleter: TaskDeleter, + private val localBroadcastManager: LocalBroadcastManager) { + + /** + * Reads in remote list information and updates local list objects. + * + * @param remoteLists remote information about your lists + */ + @Synchronized + suspend fun updateLists(account: GoogleTaskAccount, remoteLists: List) { + val lists = googleTaskListDao.getLists(account.account!!) + val previousLists: MutableSet = HashSet() + for (list in lists) { + previousLists.add(list.id) + } + for (i in remoteLists.indices) { + val remote = remoteLists[i] + val id = remote.id + var local: GoogleTaskList? = null + for (list in lists) { + if (list.remoteId == id) { + local = list + break + } + } + val title = remote.title + if (local == null) { + val byRemoteId = googleTaskListDao.findExistingList(id) + if (byRemoteId != null) { + byRemoteId.account = account.account + local = byRemoteId + } else { + Timber.d("Adding new gtask list %s", title) + local = GoogleTaskList() + local.account = account.account + local.remoteId = id + } + } + local.title = title + googleTaskListDao.insertOrReplace(local) + previousLists.remove(local.id) + } + + // check for lists that aren't on remote server + for (listId in previousLists) { + taskDeleter.delete(googleTaskListDao.getById(listId)!!) + } + localBroadcastManager.broadcastRefreshList() + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt b/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt deleted file mode 100644 index c2fc826ce..000000000 --- a/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt +++ /dev/null @@ -1,31 +0,0 @@ -package org.tasks.data - -import kotlinx.coroutines.runBlocking -import javax.inject.Inject - -@Deprecated("use coroutines") -class GoogleTaskListDaoBlocking @Inject constructor(private val dao: GoogleTaskListDao) { - fun getById(id: Long): GoogleTaskList? = runBlocking { - dao.getById(id) - } - - fun getLists(account: String): List = runBlocking { - dao.getLists(account) - } - - fun getByRemoteId(remoteId: String): GoogleTaskList? = runBlocking { - dao.getByRemoteId(remoteId) - } - - fun findExistingList(remoteId: String): GoogleTaskList? = runBlocking { - dao.findExistingList(remoteId) - } - - fun insertOrReplace(googleTaskList: GoogleTaskList): Long = runBlocking { - dao.insertOrReplace(googleTaskList) - } - - fun insert(googleTaskAccount: GoogleTaskAccount) = runBlocking { - dao.insert(googleTaskAccount) - } -} \ No newline at end of file