Convert GtasksListService to Kotlin

pull/1066/head
Alex Baker 4 years ago
parent 0f27915f82
commit a04f617fff

@ -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))

@ -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<TaskList> remoteLists) {
List<GoogleTaskList> lists = googleTaskListDao.getLists(account.getAccount());
Set<Long> 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();
}
}

@ -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<TaskList>) {
val lists = googleTaskListDao.getLists(account.account!!)
val previousLists: MutableSet<Long> = 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()
}
}

@ -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<GoogleTaskList> = 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)
}
}
Loading…
Cancel
Save