mirror of https://github.com/tasks/tasks
Convert TaskerTaskCreator to Kotlin
parent
3db92db33b
commit
c62796acec
@ -1,79 +0,0 @@
|
||||
package org.tasks.locale.receiver;
|
||||
|
||||
import static org.tasks.Strings.isNullOrEmpty;
|
||||
import static org.tasks.time.DateTimeUtils.currentTimeMillis;
|
||||
|
||||
import com.todoroo.astrid.dao.TaskDaoBlocking;
|
||||
import com.todoroo.astrid.data.Task;
|
||||
import com.todoroo.astrid.data.Task.Priority;
|
||||
import com.todoroo.astrid.service.TaskCreator;
|
||||
import javax.inject.Inject;
|
||||
import org.tasks.locale.bundle.TaskCreationBundle;
|
||||
import org.tasks.time.DateTime;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class TaskerTaskCreator {
|
||||
|
||||
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
|
||||
private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_LOCAL_TIME;
|
||||
|
||||
private final TaskCreator taskCreator;
|
||||
private final TaskDaoBlocking taskDao;
|
||||
|
||||
@Inject
|
||||
TaskerTaskCreator(TaskCreator taskCreator, TaskDaoBlocking taskDao) {
|
||||
this.taskCreator = taskCreator;
|
||||
this.taskDao = taskDao;
|
||||
}
|
||||
|
||||
public void handle(TaskCreationBundle bundle) {
|
||||
Task task = taskCreator.basicQuickAddTask(bundle.getTitle());
|
||||
|
||||
String dueDateString = bundle.getDueDate();
|
||||
if (!isNullOrEmpty(dueDateString)) {
|
||||
try {
|
||||
LocalDate dueDate = LocalDate.parse(dueDateString, dateFormatter);
|
||||
DateTime dt =
|
||||
new DateTime(dueDate.getYear(), dueDate.getMonthValue(), dueDate.getDayOfMonth());
|
||||
task.setDueDate(Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, dt.getMillis()));
|
||||
} catch (Exception e) {
|
||||
Timber.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
String dueTimeString = bundle.getDueTime();
|
||||
if (!isNullOrEmpty(dueTimeString)) {
|
||||
try {
|
||||
LocalTime dueTime = LocalTime.parse(dueTimeString, timeFormatter);
|
||||
task.setDueDate(
|
||||
Task.createDueDate(
|
||||
Task.URGENCY_SPECIFIC_DAY_TIME,
|
||||
new DateTime(task.hasDueDate() ? task.getDueDate() : currentTimeMillis())
|
||||
.withHourOfDay(dueTime.getHour())
|
||||
.withMinuteOfHour(dueTime.getMinute())
|
||||
.getMillis()));
|
||||
} catch (Exception e) {
|
||||
Timber.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
String priorityString = bundle.getPriority();
|
||||
if (!isNullOrEmpty(priorityString)) {
|
||||
try {
|
||||
int priority = Integer.parseInt(priorityString);
|
||||
task.setPriority(Math.max(Priority.HIGH, Math.min(Priority.NONE, priority)));
|
||||
} catch (NumberFormatException e) {
|
||||
Timber.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
task.setNotes(bundle.getDescription());
|
||||
|
||||
taskDao.save(task);
|
||||
|
||||
taskCreator.createTags(task);
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package org.tasks.locale.receiver
|
||||
|
||||
import com.todoroo.astrid.dao.TaskDao
|
||||
import com.todoroo.astrid.data.Task
|
||||
import com.todoroo.astrid.data.Task.Companion.createDueDate
|
||||
import com.todoroo.astrid.service.TaskCreator
|
||||
import org.tasks.Strings.isNullOrEmpty
|
||||
import org.tasks.locale.bundle.TaskCreationBundle
|
||||
import org.tasks.time.DateTime
|
||||
import org.tasks.time.DateTimeUtils
|
||||
import timber.log.Timber
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import javax.inject.Inject
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
class TaskerTaskCreator @Inject internal constructor(
|
||||
private val taskCreator: TaskCreator,
|
||||
private val taskDao: TaskDao) {
|
||||
|
||||
suspend fun handle(bundle: TaskCreationBundle) {
|
||||
val task = taskCreator.basicQuickAddTask(bundle.title)
|
||||
val dueDateString = bundle.dueDate
|
||||
if (!isNullOrEmpty(dueDateString)) {
|
||||
try {
|
||||
val dueDate = LocalDate.parse(dueDateString, dateFormatter)
|
||||
val dt = DateTime(dueDate.year, dueDate.monthValue, dueDate.dayOfMonth)
|
||||
task.dueDate = createDueDate(Task.URGENCY_SPECIFIC_DAY, dt.millis)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
val dueTimeString = bundle.dueTime
|
||||
if (!isNullOrEmpty(dueTimeString)) {
|
||||
try {
|
||||
val dueTime = LocalTime.parse(dueTimeString, timeFormatter)
|
||||
task.dueDate = createDueDate(
|
||||
Task.URGENCY_SPECIFIC_DAY_TIME,
|
||||
DateTime(if (task.hasDueDate()) task.dueDate else DateTimeUtils.currentTimeMillis())
|
||||
.withHourOfDay(dueTime.hour)
|
||||
.withMinuteOfHour(dueTime.minute)
|
||||
.millis)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
val priorityString = bundle.priority
|
||||
if (!isNullOrEmpty(priorityString)) {
|
||||
try {
|
||||
val priority = priorityString.toInt()
|
||||
task.priority = max(Task.Priority.HIGH, min(Task.Priority.NONE, priority))
|
||||
} catch (e: NumberFormatException) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
task.notes = bundle.description
|
||||
taskDao.save(task)
|
||||
taskCreator.createTags(task)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE
|
||||
private val timeFormatter = DateTimeFormatter.ISO_LOCAL_TIME
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue