mirror of https://github.com/tasks/tasks
Convert GtasksListService to Kotlin
parent
0f27915f82
commit
a04f617fff
@ -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…
Reference in New Issue