Convert TaskContainer to Kotlin

pull/2282/head
Alex Baker 2 years ago
parent 19b2551f54
commit a8d62b6632

@ -742,7 +742,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
R.id.menu_select_all -> {
lifecycleScope.launch {
taskAdapter.setSelected(taskDao.fetchTasks(preferences, filter)
.map(TaskContainer::getId))
.map(TaskContainer::id))
updateModeTitle()
recyclerAdapter?.notifyDataSetChanged()
}
@ -779,7 +779,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
private fun send(tasks: List<TaskContainer>) {
val output = tasks.joinToString("\n") { t -> Task
"${(if (t.isCompleted) "☑" else "☐").padStart(1 + t.getIndent() * 3, ' ')} ${t.title}"
"${(if (t.isCompleted) "☑" else "☐").padStart(1 + t.indent * 3, ' ')} ${t.title}"
}
val intent = ShareCompat
.IntentBuilder(requireContext())
@ -803,9 +803,9 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
val fragmentManager = parentFragmentManager
if (fragmentManager.findFragmentByTag(FRAG_TAG_DATE_TIME_PICKER) == null) {
newDateTimePicker(
preferences.getBoolean(R.string.p_auto_dismiss_datetime_list_screen, false),
task.task)
.show(fragmentManager, FRAG_TAG_DATE_TIME_PICKER)
preferences.getBoolean(R.string.p_auto_dismiss_datetime_list_screen, false),
task.task)
.show(fragmentManager, FRAG_TAG_DATE_TIME_PICKER)
}
}
@ -862,7 +862,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
return
}
lifecycleScope.launch {
taskCompleter.setComplete(task.getTask(), newState)
taskCompleter.setComplete(task.task, newState)
taskAdapter.onCompletedTask(task, newState)
loadTaskListContent()
}
@ -880,7 +880,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
if (isActionModeActive) {
recyclerAdapter?.toggle(taskViewHolder)
} else {
onTaskListItemClicked(taskViewHolder.task.getTask())
onTaskListItemClicked(taskViewHolder.task.task)
}
}

@ -29,9 +29,9 @@ class CaldavManualSortTaskAdapter internal constructor(
val newPosition = when {
previous == null -> next!!.caldavSortOrder - 1
indent > previous.getIndent() && next?.indent == indent -> next.caldavSortOrder - 1
indent > previous.getIndent() -> null
indent == previous.getIndent() -> previous.caldavSortOrder + 1
indent > previous.indent && next?.indent == indent -> next.caldavSortOrder - 1
indent > previous.indent -> null
indent == previous.indent -> previous.caldavSortOrder + 1
else -> getTask((to - 1 downTo 0).find { getTask(it).indent == indent }!!).caldavSortOrder + 1
}
caldavDao.move(task, newParent, newPosition)
@ -48,7 +48,7 @@ class CaldavManualSortTaskAdapter internal constructor(
}
private suspend fun changeParent(task: TaskContainer, newParent: Long) {
val caldavTask = task.getCaldavTask()
val caldavTask = task.caldavTask ?: return
if (newParent == 0L) {
caldavTask.remoteParent = ""
task.parent = 0
@ -58,6 +58,6 @@ class CaldavManualSortTaskAdapter internal constructor(
task.parent = newParent
}
caldavDao.update(caldavTask.id, caldavTask.remoteParent)
taskDao.save(task.getTask(), null)
taskDao.save(task.task, null)
}
}

@ -17,9 +17,9 @@ class GoogleTaskManualSortAdapter internal constructor(
override suspend fun moved(from: Int, to: Int, indent: Int) {
val task = getTask(from)
val googleTask = task.caldavTask
val googleTask = task.caldavTask ?: return
val list = googleTask.calendar ?: return
val previous = if (to > 0) getTask(to - 1) else null
val list = googleTask.calendar!!
if (previous == null) {
googleTaskDao.move(
task = task.task,
@ -34,21 +34,21 @@ class GoogleTaskManualSortAdapter internal constructor(
task = task.task,
list = list,
newParent = 0,
newPosition = previous.getPrimarySort() + if (to == count) 0 else 1,
newPosition = previous.primarySort + if (to == count) 0 else 1,
)
previous.hasParent() && previous.parent == task.parent ->
googleTaskDao.move(
task = task.task,
list = list,
newParent = previous.parent,
newPosition = previous.getSecondarySort() + if (to == count) 0 else 1,
newPosition = previous.secondarySort + if (to == count) 0 else 1,
)
previous.hasParent() ->
googleTaskDao.move(
task = task.task,
list = list,
newParent = previous.parent,
newPosition = previous.getSecondarySort() + 1,
newPosition = previous.secondarySort + 1,
)
else ->
googleTaskDao.move(
@ -65,21 +65,21 @@ class GoogleTaskManualSortAdapter internal constructor(
task = task.task,
list = list,
newParent = 0,
newPosition = previous.getPrimarySort() + if (task.hasParent()) 1 else 0,
newPosition = previous.primarySort + if (task.hasParent()) 1 else 0,
)
previous.hasParent() && previous.parent == task.parent ->
googleTaskDao.move(
task = task.task,
list = list,
newParent = previous.parent,
newPosition = previous.getSecondarySort(),
newPosition = previous.secondarySort,
)
previous.hasParent() ->
googleTaskDao.move(
task = task.task,
list = list,
newParent = previous.parent,
newPosition = previous.getSecondarySort() + 1,
newPosition = previous.secondarySort + 1,
)
else ->
googleTaskDao.move(

@ -5,7 +5,10 @@
*/
package com.todoroo.astrid.adapter
import com.todoroo.astrid.core.SortHelper.*
import com.todoroo.astrid.core.SortHelper.SORT_DUE
import com.todoroo.astrid.core.SortHelper.SORT_IMPORTANCE
import com.todoroo.astrid.core.SortHelper.SORT_LIST
import com.todoroo.astrid.core.SortHelper.SORT_START
import com.todoroo.astrid.dao.TaskDao
import com.todoroo.astrid.data.Task
import com.todoroo.astrid.data.Task.Companion.HIDE_UNTIL_SPECIFIC_DAY
@ -62,7 +65,7 @@ open class TaskAdapter(
fun clearCollapsed() = collapsed.retainAll(listOf(HEADER_COMPLETED))
open fun getIndent(task: TaskContainer): Int = task.getIndent()
open fun getIndent(task: TaskContainer): Int = task.indent
open fun canMove(source: TaskContainer, from: Int, target: TaskContainer, to: Int): Boolean {
if (target.isGoogleTask) {
@ -152,7 +155,7 @@ open class TaskAdapter(
changeSortGroup(task, if (from < to) to - 1 else to)
}
newParent.isGoogleTask -> changeGoogleTaskParent(task, newParent)
newParent.isCaldavTask -> changeCaldavParent(task, newParent)
newParent.isCaldavTask() -> changeCaldavParent(task, newParent)
}
}
@ -190,7 +193,7 @@ open class TaskAdapter(
}
for (i in to - 1 downTo 0) {
val previous = getTask(i)
if (indent > previous.getIndent()) {
if (indent > previous.indent) {
return previous
}
}
@ -202,7 +205,7 @@ open class TaskAdapter(
SORT_IMPORTANCE -> {
val newPriority = dataSource.nearestHeader(if (pos == 0) 1 else pos).toInt()
if (newPriority != task.priority) {
val t = task.getTask()
val t = task.task
t.priority = newPriority
taskDao.save(t)
}
@ -240,7 +243,7 @@ open class TaskAdapter(
private suspend fun moveToTopLevel(task: TaskContainer) {
when {
task.isGoogleTask -> changeGoogleTaskParent(task, null)
task.isCaldavTask -> changeCaldavParent(task, null)
task.isCaldavTask() -> changeCaldavParent(task, null)
}
}
@ -256,7 +259,7 @@ open class TaskAdapter(
} else {
task.parent = newParent.id
task.caldavTask = CaldavTask(task.id, list, remoteId = null)
googleTaskDao.insertAndShift(task.task, task.caldavTask, newTasksOnTop)
googleTaskDao.insertAndShift(task.task, task.caldavTask!!, newTasksOnTop)
}
taskDao.touch(task.id)
if (BuildConfig.DEBUG) {
@ -266,7 +269,7 @@ open class TaskAdapter(
private suspend fun changeCaldavParent(task: TaskContainer, newParent: TaskContainer?) {
val list = newParent?.caldav ?: task.caldav!!
val caldavTask = task.getCaldavTask() ?: CaldavTask(
val caldavTask = task.caldavTask ?: CaldavTask(
task.id,
list,
)

@ -4,7 +4,12 @@ import com.todoroo.astrid.api.Filter
import com.todoroo.astrid.data.Task
import org.tasks.LocalBroadcastManager
import org.tasks.caldav.VtodoCache
import org.tasks.data.*
import org.tasks.data.CaldavAccount
import org.tasks.data.CaldavCalendar
import org.tasks.data.DeletionDao
import org.tasks.data.GoogleTaskDao
import org.tasks.data.TaskContainer
import org.tasks.data.TaskDao
import org.tasks.db.QueryUtils
import org.tasks.db.SuspendDbUtils.chunkedMap
import org.tasks.jobs.WorkManager
@ -46,7 +51,7 @@ class TaskDeleter @Inject constructor(
val completed = taskDao.fetchTasks(preferences, deleteFilter)
.filter(TaskContainer::isCompleted)
.filterNot(TaskContainer::isReadOnly)
.map(TaskContainer::getId)
.map(TaskContainer::id)
.toMutableList()
completed.removeAll(deletionDao.hasRecurringAncestors(completed))
markDeleted(completed)

@ -198,7 +198,7 @@ fun ExistingSubtaskRow(
modifier = Modifier.align(Alignment.Top),
)
Text(
text = task.title,
text = task.title!!,
modifier = Modifier
.weight(1f)
.alpha(if (task.isCompleted || task.isHidden) ContentAlpha.disabled else ContentAlpha.high)
@ -252,20 +252,20 @@ fun SubtasksPreview() {
hasParent = false,
desaturate = true,
existingSubtasks = listOf(
TaskContainer().apply {
TaskContainer(
task = Task().apply {
title = "Existing subtask 1"
priority = Task.Priority.HIGH
}
},
indent = 0
},
TaskContainer().apply {
),
TaskContainer(
task = Task().apply {
title = "Existing subtask 2 with a really long title"
priority = Task.Priority.LOW
}
},
indent = 1
}
)
),
newSubtasks = listOf(
Task().apply {

@ -1,224 +0,0 @@
package org.tasks.data;
import androidx.annotation.Nullable;
import androidx.room.Embedded;
import com.todoroo.astrid.data.Task;
import java.util.Objects;
public class TaskContainer {
@Embedded public Task task;
@Embedded public CaldavTask caldavTask;
@Embedded public Location location;
public boolean isGoogleTask;
public boolean parentComplete;
public String tags;
public int children;
public Long sortGroup;
public long primarySort;
public long secondarySort;
public int indent;
private int targetIndent;
public String getTagsString() {
return tags;
}
public @Nullable String getCaldav() {
if (caldavTask != null) {
return caldavTask.getCalendar();
}
return null;
}
public boolean isCaldavTask() {
return caldavTask != null;
}
public String getNotes() {
return task.getNotes();
}
public boolean hasNotes() {
return task.hasNotes();
}
public String getTitle() {
return task.getTitle();
}
public boolean isHidden() {
return task.isHidden();
}
public long getStartDate() {
return task.getHideUntil();
}
public boolean isCompleted() {
return task.isCompleted();
}
public boolean hasDueDate() {
return task.hasDueDate();
}
public boolean hasDueTime() {
return task.hasDueTime();
}
public boolean isOverdue() {
return task.isOverdue();
}
public long getDueDate() {
return task.getDueDate();
}
public Task getTask() {
return task;
}
public long getId() {
return task.getId();
}
public long getPrimarySort() {
return primarySort;
}
public long getSecondarySort() {
return secondarySort;
}
public int getIndent() {
return indent;
}
public long getCreationDate() {
return task.getCreationDate();
}
public void setIndent(int indent) {
this.indent = indent;
targetIndent = indent;
}
public boolean isReadOnly() {
return task.getReadOnly();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TaskContainer)) {
return false;
}
TaskContainer that = (TaskContainer) o;
return children == that.children
&& primarySort == that.primarySort
&& secondarySort == that.secondarySort
&& indent == that.indent
&& targetIndent == that.targetIndent
&& Objects.equals(task, that.task)
&& Objects.equals(caldavTask, that.caldavTask)
&& Objects.equals(location, that.location)
&& Objects.equals(tags, that.tags)
&& Objects.equals(sortGroup, that.sortGroup);
}
@Override
public int hashCode() {
return Objects.hash(
task,
caldavTask,
location,
tags,
children,
sortGroup,
primarySort,
secondarySort,
indent,
targetIndent);
}
@Override
public String toString() {
return "TaskContainer{"
+ "task="
+ task
+ ", caldavTask="
+ caldavTask
+ ", location="
+ location
+ ", tags='"
+ tags
+ '\''
+ ", children="
+ children
+ ", sortGroup="
+ sortGroup
+ ", primarySort="
+ primarySort
+ ", secondarySort="
+ secondarySort
+ ", indent="
+ indent
+ ", targetIndent="
+ targetIndent
+ '}';
}
public String getUuid() {
return task.getUuid();
}
public long getParent() {
return task.getParent();
}
public void setParent(long parent) {
task.setParent(parent);
}
public boolean hasParent() {
return getParent() > 0;
}
public boolean hasChildren() {
return children > 0;
}
public CaldavTask getCaldavTask() {
return caldavTask;
}
public int getTargetIndent() {
return targetIndent;
}
public void setTargetIndent(int indent) {
targetIndent = indent;
}
public boolean hasLocation() {
return location != null;
}
public Location getLocation() {
return location;
}
public boolean isCollapsed() {
return task.isCollapsed();
}
public long getCaldavSortOrder() {
return indent == 0 ? primarySort : secondarySort;
}
public int getPriority() {
return task.getPriority();
}
}

@ -0,0 +1,80 @@
package org.tasks.data
import androidx.room.Embedded
import com.todoroo.astrid.data.Task
data class TaskContainer(
@Embedded val task: Task,
@Embedded var caldavTask: CaldavTask? = null,
@Embedded val location: Location? = null,
val isGoogleTask: Boolean = false,
val parentComplete: Boolean = false,
val tagsString: String? = null,
val children: Int = 0,
val sortGroup: Long? = null,
val primarySort: Long = 0,
val secondarySort: Long = 0,
var indent: Int = 0,
var targetIndent: Int = 0,
){
val caldav: String?
get() = caldavTask?.calendar
fun isCaldavTask(): Boolean = caldavTask != null
val notes: String?
get() = task.notes
fun hasNotes(): Boolean {
return task.hasNotes()
}
val title: String?
get() = task.title
val isHidden: Boolean
get() = task.isHidden
val isCompleted: Boolean
get() = task.isCompleted
fun hasDueDate(): Boolean {
return task.hasDueDate()
}
fun hasDueTime(): Boolean {
return task.hasDueTime()
}
val isOverdue: Boolean
get() = task.isOverdue
val dueDate: Long
get() = task.dueDate
val id: Long
get() = task.id
val creationDate: Long
get() = task.creationDate
val uuid: String
get() = task.uuid
var parent: Long
get() = task.parent
set(parent) {
task.parent = parent
}
fun hasParent(): Boolean = parent > 0
fun hasChildren(): Boolean = children > 0
fun hasLocation(): Boolean = location != null
val isCollapsed: Boolean
get() = task.isCollapsed
val caldavSortOrder: Long
get() = if (indent == 0) primarySort else secondarySort
val priority: Int
get() = task.priority
val isReadOnly: Boolean
get() = task.readOnly
}

@ -180,7 +180,7 @@ FROM recursive_tasks
internal suspend fun setCollapsed(preferences: Preferences, filter: Filter, collapsed: Boolean) {
fetchTasks(preferences, filter)
.filter(TaskContainer::hasChildren)
.map(TaskContainer::getId)
.map(TaskContainer::id)
.eachChunk { setCollapsed(it, collapsed) }
}
@ -231,7 +231,7 @@ FROM recursive_tasks
val start = if (BuildConfig.DEBUG) now() else 0
val tasks = fetchTasks(query)
Timber.v("%sms: %s", now() - start, query.sql)
return tasks.map(TaskContainer::getTask)
return tasks.map(TaskContainer::task)
}
@Query("""

@ -40,6 +40,6 @@ internal class DiffCallback(
}
val oldItem = old.getItem(oldPosition)!!
val newItem = new.getItem(newPosition)!!
return !refreshDates && oldItem == newItem && oldItem.getIndent() == adapter.getIndent(newItem)
return !refreshDates && oldItem == newItem && oldItem.indent == adapter.getIndent(newItem)
}
}

@ -196,7 +196,7 @@ class DragAndDropRecyclerAdapter(
max((currentIndent - minIndent) * -shiftSize, dX)
}
val targetIndent = currentIndent + java.lang.Float.valueOf(dxAdjusted / shiftSize).toInt()
if (targetIndent != task.getIndent()) {
if (targetIndent != task.indent) {
if (from == -1) {
taskList.finishActionMode()
vh.selected = false
@ -206,7 +206,7 @@ class DragAndDropRecyclerAdapter(
task.targetIndent = minIndent
} else task.targetIndent = min(targetIndent, maxIndent)
}
dX = (task.targetIndent - task.getIndent()) * shiftSize
dX = (task.targetIndent - task.indent) * shiftSize
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
@ -227,12 +227,14 @@ class DragAndDropRecyclerAdapter(
if (from < to) {
to++
}
vh.task.setIndent(targetIndent)
vh.task.indent = targetIndent
vh.task.targetIndent = targetIndent
vh.indent = targetIndent
moved(from, to, targetIndent)
} else if (task.getIndent() != targetIndent) {
} else if (task.indent != targetIndent) {
val position = vh.adapterPosition
vh.task.setIndent(targetIndent)
vh.task.indent = targetIndent
vh.task.targetIndent = targetIndent
vh.indent = targetIndent
moved(position, position, targetIndent)
}

@ -86,7 +86,7 @@ class SectionedDataSource constructor(
sections.add(AdapterSection(i, header, 0, isCollapsed))
} else {
val previousTask = tasks[i - 1]
val previous = previousTask.sortGroup
val previous = previousTask.sortGroup ?: 0L
when {
completedAtBottom && task.parentComplete -> {
if (!previousTask.parentComplete) {

@ -30,7 +30,8 @@ abstract class TaskListRecyclerAdapter internal constructor(
.bindView(task, filter, if (groupsEnabled) preferences.sortMode else -1)
holder.moving = false
val indent = adapter.getIndent(task)
task.setIndent(indent)
task.indent = indent
task.targetIndent = indent
holder.indent = indent
holder.selected = adapter.isSelected(task)
}

@ -194,7 +194,7 @@ class TaskViewHolder internal constructor(
nameView.paintFlags = nameView.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
completeBox.isChecked = task.isCompleted
completeBox.setImageDrawable(checkBoxProvider.getCheckBox(task.getTask()))
completeBox.setImageDrawable(checkBoxProvider.getCheckBox(task.task))
completeBox.invalidate()
}
@ -206,7 +206,7 @@ class TaskViewHolder internal constructor(
dueDate.setTextColor(textColorSecondary)
}
val dateValue: String? = if (sortByDueDate
&& task.sortGroup >= now().startOfDay()
&& (task.sortGroup ?: 0) >= now().startOfDay()
&& showGroupHeaders
) {
task.takeIf { it.hasDueTime() }?.let {
@ -238,7 +238,7 @@ class TaskViewHolder internal constructor(
collapsed = task.isCollapsed,
isHidden = task.isHidden,
sortGroup = task.sortGroup,
startDate = task.startDate,
startDate = task.task.hideUntil,
place = task.location?.place,
list = task.caldav,
tagsString = task.tagsString,

@ -17,7 +17,7 @@ import org.tasks.themes.CustomIcons
import org.tasks.time.DateTimeUtils.startOfDay
import org.tasks.ui.ChipListCache
import java.time.format.FormatStyle
import java.util.*
import java.util.Locale
import javax.inject.Inject
class ChipProvider @Inject constructor(
@ -49,15 +49,15 @@ class ChipProvider @Inject constructor(
fun getStartDateChip(task: TaskContainer, showFullDate: Boolean, sortByStartDate: Boolean): RemoteViews? {
return if (task.isHidden) {
val chip = newChip()
val time = if (sortByStartDate && task.sortGroup?.startOfDay() == task.startDate.startOfDay()) {
task.startDate
val time = if (sortByStartDate && task.sortGroup?.startOfDay() == task.task.hideUntil.startOfDay()) {
task.task.hideUntil
.takeIf { Task.hasDueTime(it) }
?.let { DateUtilities.getTimeString(context, it.toDateTime()) }
?: return null
} else {
DateUtilities.getRelativeDateTime(
context,
task.startDate,
task.task.hideUntil,
locale,
FormatStyle.MEDIUM,
showFullDate,
@ -88,7 +88,7 @@ class ChipProvider @Inject constructor(
}
fun getTagChips(filter: Filter?, task: TaskContainer): List<RemoteViews> {
val tags = task.tags?.split(",")?.toHashSet() ?: return emptyList()
val tags = task.tagsString?.split(",")?.toHashSet() ?: return emptyList()
if (filter is TagFilter) {
tags.remove(filter.uuid)
}

@ -187,7 +187,7 @@ internal class ScrollableViewsFactory(
private fun buildUpdate(position: Int): RemoteViews? {
try {
val taskContainer = getTask(position) ?: return null
val task = taskContainer.getTask()
val task = taskContainer.task
var textColorTitle = textColorPrimary
val row = newRemoteView()
if (task.isHidden) {
@ -279,12 +279,12 @@ internal class ScrollableViewsFactory(
.getListChip(filter, taskContainer)
?.let { row.addView(R.id.chips, it) }
}
if (showTags && taskContainer.tags?.isNotBlank() == true) {
if (showTags && taskContainer.tagsString?.isNotBlank() == true) {
chipProvider
.getTagChips(filter, taskContainer)
.forEach { row.addView(R.id.chips, it) }
}
val startPad = taskContainer.getIndent() * indentPadding
val startPad = taskContainer.indent * indentPadding
row.setViewPadding(R.id.widget_row, startPad, 0, 0, 0)
return row
} catch (e: Exception) {
@ -315,7 +315,7 @@ internal class ScrollableViewsFactory(
}
row.setViewVisibility(dueDateRes, View.VISIBLE)
val text = if (sortMode == SortHelper.SORT_DUE
&& task.sortGroup >= now().startOfDay()
&& (task.sortGroup ?: 0L) >= now().startOfDay()
&& !disableGroups
) {
task.takeIf { it.hasDueTime() }?.let {

@ -19,16 +19,17 @@ object TaskContainerMaker {
val CREATED: Property<TaskContainer, DateTime> = newProperty()
private val instantiator = Instantiator { lookup: PropertyLookup<TaskContainer> ->
val container = TaskContainer()
val parent = lookup.valueOf(PARENT, null as TaskContainer?)
val taskId = lookup.valueOf(ID, NO_ID)
val created = lookup.valueOf(CREATED, newDateTime())
container.task = newTask(
val parent = lookup.valueOf(PARENT, null as TaskContainer?)
TaskContainer(
task = newTask(
with(TaskMaker.ID, taskId),
with(TaskMaker.CREATION_TIME, created),
with(TaskMaker.PARENT, parent?.id ?: 0L))
container.indent = parent?.indent?.plus(1) ?: 0
container
with(TaskMaker.PARENT, parent?.id ?: 0L)
),
indent = parent?.indent?.plus(1) ?: 0,
)
}
fun newTaskContainer(vararg properties: PropertyValue<in TaskContainer?, *>): TaskContainer {

Loading…
Cancel
Save