Convert TaskCreator to Kotlin

pull/1055/head
Alex Baker 4 years ago
parent 6670403b7a
commit c825bfe94a

@ -1,212 +0,0 @@
package com.todoroo.astrid.service;
import static com.todoroo.andlib.utility.DateUtilities.now;
import static com.todoroo.astrid.helper.UUIDHelper.newUUID;
import static org.tasks.Strings.isNullOrEmpty;
import android.net.Uri;
import androidx.annotation.Nullable;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.api.CaldavFilter;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.GtasksFilter;
import com.todoroo.astrid.api.PermaSql;
import com.todoroo.astrid.dao.TaskDaoBlocking;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gcal.GCalHelper;
import com.todoroo.astrid.utility.TitleParser;
import java.util.ArrayList;
import java.util.Map;
import javax.inject.Inject;
import org.tasks.R;
import org.tasks.data.CaldavDaoBlocking;
import org.tasks.data.CaldavTask;
import org.tasks.data.Geofence;
import org.tasks.data.GoogleTask;
import org.tasks.data.GoogleTaskDaoBlocking;
import org.tasks.data.LocationDaoBlocking;
import org.tasks.data.Place;
import org.tasks.data.Tag;
import org.tasks.data.TagDaoBlocking;
import org.tasks.data.TagData;
import org.tasks.data.TagDataDaoBlocking;
import org.tasks.preferences.DefaultFilterProvider;
import org.tasks.preferences.Preferences;
import timber.log.Timber;
public class TaskCreator {
private final GCalHelper gcalHelper;
private final Preferences preferences;
private final TagDaoBlocking tagDao;
private final GoogleTaskDaoBlocking googleTaskDao;
private final DefaultFilterProvider defaultFilterProvider;
private final CaldavDaoBlocking caldavDao;
private final LocationDaoBlocking locationDao;
private final TagDataDaoBlocking tagDataDao;
private final TaskDaoBlocking taskDao;
@Inject
public TaskCreator(
GCalHelper gcalHelper,
Preferences preferences,
TagDataDaoBlocking tagDataDao,
TaskDaoBlocking taskDao,
TagDaoBlocking tagDao,
GoogleTaskDaoBlocking googleTaskDao,
DefaultFilterProvider defaultFilterProvider,
CaldavDaoBlocking caldavDao,
LocationDaoBlocking locationDao) {
this.gcalHelper = gcalHelper;
this.preferences = preferences;
this.tagDataDao = tagDataDao;
this.taskDao = taskDao;
this.tagDao = tagDao;
this.googleTaskDao = googleTaskDao;
this.defaultFilterProvider = defaultFilterProvider;
this.caldavDao = caldavDao;
this.locationDao = locationDao;
}
private static void setDefaultReminders(Preferences preferences, Task task) {
task.setReminderPeriod(
DateUtilities.ONE_HOUR
* preferences.getIntegerFromString(R.string.p_rmd_default_random_hours, 0));
task.setReminderFlags(preferences.getDefaultReminders() | preferences.getDefaultRingMode());
}
public Task basicQuickAddTask(String title) {
title = title.trim();
Task task = createWithValues(title);
taskDao.createNew(task);
boolean gcalCreateEventEnabled =
preferences.isDefaultCalendarSet() && task.hasDueDate(); // $NON-NLS-1$
if (!isNullOrEmpty(task.getTitle())
&& gcalCreateEventEnabled
&& isNullOrEmpty(task.getCalendarURI())) {
Uri calendarUri = gcalHelper.createTaskEvent(task, preferences.getDefaultCalendar());
task.setCalendarURI(calendarUri.toString());
}
createTags(task);
boolean addToTop = preferences.addTasksToTop();
if (task.hasTransitory(GoogleTask.KEY)) {
googleTaskDao.insertAndShift(
new GoogleTask(task.getId(), task.getTransitory(GoogleTask.KEY)), addToTop);
} else if (task.hasTransitory(CaldavTask.KEY)) {
caldavDao.insert(
task, new CaldavTask(task.getId(), task.getTransitory(CaldavTask.KEY)), addToTop);
} else {
Filter remoteList = defaultFilterProvider.getDefaultList();
if (remoteList instanceof GtasksFilter) {
googleTaskDao.insertAndShift(
new GoogleTask(task.getId(), ((GtasksFilter) remoteList).getRemoteId()), addToTop);
} else if (remoteList instanceof CaldavFilter) {
caldavDao.insert(
task, new CaldavTask(task.getId(), ((CaldavFilter) remoteList).getUuid()), addToTop);
}
}
if (task.hasTransitory(Place.KEY)) {
Place place = locationDao.getPlace(task.getTransitory(Place.KEY));
if (place != null) {
locationDao.insert(new Geofence(place.getUid(), preferences));
}
}
taskDao.save(task, null);
return task;
}
public Task createWithValues(String title) {
return create(null, title);
}
public Task createWithValues(Filter filter, String title) {
return create(filter == null ? null : filter.valuesForNewTasks, title);
}
/**
* Create task from the given content values, saving it. This version doesn't need to start with a
* base task model.
*/
private Task create(@Nullable Map<String, Object> values, String title) {
Task task = new Task();
task.setCreationDate(now());
task.setModificationDate(now());
if (title != null) {
task.setTitle(title.trim());
}
task.setUuid(newUUID());
task.setPriority(preferences.getDefaultPriority());
task.setDueDate(
Task.createDueDate(
preferences.getIntegerFromString(R.string.p_default_urgency_key, Task.URGENCY_NONE),
0));
int setting =
preferences.getIntegerFromString(R.string.p_default_hideUntil_key, Task.HIDE_UNTIL_NONE);
task.setHideUntil(task.createHideUntil(setting, 0));
setDefaultReminders(preferences, task);
ArrayList<String> tags = new ArrayList<>();
if (values != null && values.size() > 0) {
for (Map.Entry<String, Object> item : values.entrySet()) {
String key = item.getKey();
Object value = item.getValue();
switch (key) {
case Tag.KEY:
tags.add((String) value);
break;
case GoogleTask.KEY:
case CaldavTask.KEY:
case Place.KEY:
task.putTransitory(key, value);
break;
default:
if (value instanceof String) {
value = PermaSql.replacePlaceholdersForNewTask((String) value);
}
switch (key) {
case "dueDate":
task.setDueDate(Long.valueOf((String) value));
break;
case "importance":
task.setPriority(Integer.valueOf((String) value));
break;
default:
break;
}
break;
}
}
}
try {
TitleParser.parse(tagDataDao, task, tags);
} catch (Throwable e) {
Timber.e(e);
}
task.setTags(tags);
return task;
}
public void createTags(Task task) {
for (String tag : task.getTags()) {
TagData tagData = tagDataDao.getTagByName(tag);
if (tagData == null) {
tagData = new TagData();
tagData.setName(tag);
tagDataDao.createNew(tagData);
}
tagDao.insert(new Tag(task, tagData));
}
}
}

@ -0,0 +1,150 @@
package com.todoroo.astrid.service
import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.api.CaldavFilter
import com.todoroo.astrid.api.Filter
import com.todoroo.astrid.api.GtasksFilter
import com.todoroo.astrid.api.PermaSql
import com.todoroo.astrid.dao.TaskDaoBlocking
import com.todoroo.astrid.data.Task
import com.todoroo.astrid.data.Task.Companion.createDueDate
import com.todoroo.astrid.gcal.GCalHelper
import com.todoroo.astrid.helper.UUIDHelper
import com.todoroo.astrid.utility.TitleParser.parse
import org.tasks.R
import org.tasks.Strings.isNullOrEmpty
import org.tasks.data.*
import org.tasks.preferences.DefaultFilterProvider
import org.tasks.preferences.Preferences
import timber.log.Timber
import java.util.*
import javax.inject.Inject
class TaskCreator @Inject constructor(
private val gcalHelper: GCalHelper,
private val preferences: Preferences,
private val tagDataDao: TagDataDaoBlocking,
private val taskDao: TaskDaoBlocking,
private val tagDao: TagDaoBlocking,
private val googleTaskDao: GoogleTaskDaoBlocking,
private val defaultFilterProvider: DefaultFilterProvider,
private val caldavDao: CaldavDaoBlocking,
private val locationDao: LocationDaoBlocking) {
fun basicQuickAddTask(title: String): Task {
var title = title
title = title.trim { it <= ' ' }
val task = createWithValues(title)
taskDao.createNew(task)
val gcalCreateEventEnabled = preferences.isDefaultCalendarSet && task.hasDueDate() // $NON-NLS-1$
if (!isNullOrEmpty(task.title)
&& gcalCreateEventEnabled
&& isNullOrEmpty(task.calendarURI)) {
val calendarUri = gcalHelper.createTaskEvent(task, preferences.defaultCalendar)
task.calendarURI = calendarUri.toString()
}
createTags(task)
val addToTop = preferences.addTasksToTop()
if (task.hasTransitory(GoogleTask.KEY)) {
googleTaskDao.insertAndShift(
GoogleTask(task.id, task.getTransitory<String>(GoogleTask.KEY)!!), addToTop)
} else if (task.hasTransitory(CaldavTask.KEY)) {
caldavDao.insert(
task, CaldavTask(task.id, task.getTransitory<String>(CaldavTask.KEY)), addToTop)
} else {
val remoteList = defaultFilterProvider.defaultList
if (remoteList is GtasksFilter) {
googleTaskDao.insertAndShift(
GoogleTask(task.id, remoteList.remoteId), addToTop)
} else if (remoteList is CaldavFilter) {
caldavDao.insert(
task, CaldavTask(task.id, remoteList.uuid), addToTop)
}
}
if (task.hasTransitory(Place.KEY)) {
val place = locationDao.getPlace(task.getTransitory<String>(Place.KEY)!!)
if (place != null) {
locationDao.insert(Geofence(place.uid, preferences))
}
}
taskDao.save(task, null)
return task
}
fun createWithValues(title: String?): Task {
return create(null, title)
}
fun createWithValues(filter: Filter?, title: String?): Task {
return create(filter?.valuesForNewTasks, title)
}
/**
* Create task from the given content values, saving it. This version doesn't need to start with a
* base task model.
*/
private fun create(values: Map<String, Any>?, title: String?): Task {
val task = Task()
task.creationDate = DateUtilities.now()
task.modificationDate = DateUtilities.now()
if (title != null) {
task.title = title.trim { it <= ' ' }
}
task.uuid = UUIDHelper.newUUID()
task.priority = preferences.defaultPriority
task.dueDate = createDueDate(
preferences.getIntegerFromString(R.string.p_default_urgency_key, Task.URGENCY_NONE),
0)
val setting = preferences.getIntegerFromString(R.string.p_default_hideUntil_key, Task.HIDE_UNTIL_NONE)
task.hideUntil = task.createHideUntil(setting, 0)
setDefaultReminders(preferences, task)
val tags = ArrayList<String>()
if (values != null && values.isNotEmpty()) {
for (item in values.entries) {
val key = item.key
var value: Any? = item.value
when (key) {
Tag.KEY -> tags.add(value as String)
GoogleTask.KEY, CaldavTask.KEY, Place.KEY -> task.putTransitory(key, value!!)
else -> {
if (value is String) {
value = PermaSql.replacePlaceholdersForNewTask(value as String?)
}
when (key) {
"dueDate" -> task.dueDate = java.lang.Long.valueOf((value as String?)!!)
"importance" -> task.priority = Integer.valueOf((value as String?)!!)
else -> {
}
}
}
}
}
}
try {
parse(tagDataDao, task, tags)
} catch (e: Throwable) {
Timber.e(e)
}
task.setTags(tags)
return task
}
fun createTags(task: Task) {
for (tag in task.tags) {
var tagData = tagDataDao.getTagByName(tag)
if (tagData == null) {
tagData = TagData()
tagData.name = tag
tagDataDao.createNew(tagData)
}
tagDao.insert(Tag(task, tagData))
}
}
companion object {
private fun setDefaultReminders(preferences: Preferences, task: Task) {
task.reminderPeriod = (DateUtilities.ONE_HOUR
* preferences.getIntegerFromString(R.string.p_rmd_default_random_hours, 0))
task.reminderFlags = preferences.defaultReminders or preferences.defaultRingMode
}
}
}
Loading…
Cancel
Save