Skip to content

Commit

Permalink
Apply sorting in UI/Compose layer
Browse files Browse the repository at this point in the history
  • Loading branch information
opatry committed Oct 15, 2024
1 parent 1c1043e commit a1b884e
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
<string name="task_due_date_label_yesterday">Hier</string>
<string name="task_due_date_label_today">Aujourd'hui</string>
<string name="task_due_date_label_tomorrow">Demain</string>
<string name="task_due_date_label_past">Passée</string>
<string name="task_due_date_label_no_date">Sans date</string>
<string name="task_due_date_update_cta">Mettre à jour</string>

<string name="task_editor_sheet_edit_title">Modifier la tâche</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
<string name="task_due_date_label_yesterday">Yesterday</string>
<string name="task_due_date_label_today">Today</string>
<string name="task_due_date_label_tomorrow">Tomorrow</string>
<string name="task_due_date_label_past">Past</string>
<string name="task_due_date_label_no_date">No date</string>
<string name="task_due_date_update_cta">Update</string>

<string name="task_editor_sheet_edit_title">Edit task</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import net.opatry.tasks.app.ui.model.TaskListUIModel
import net.opatry.tasks.app.ui.screen.TaskListSorting
import net.opatry.tasks.app.ui.tooling.TaskfolioPreview
import net.opatry.tasks.app.ui.tooling.TaskfolioThemedPreview
import net.opatry.tasks.resources.Res
Expand All @@ -71,7 +72,12 @@ enum class TaskListMenuAction {
}

@Composable
fun TaskListMenu(taskList: TaskListUIModel, expanded: Boolean, onAction: (TaskListMenuAction) -> Unit) {
fun TaskListMenu(
taskList: TaskListUIModel,
expanded: Boolean,
sorting: TaskListSorting = TaskListSorting.Manual,
onAction: (TaskListMenuAction) -> Unit
) {
val allowDelete by remember(taskList.canDelete) { mutableStateOf(taskList.canDelete) }

DropdownMenu(
Expand All @@ -90,19 +96,19 @@ fun TaskListMenu(taskList: TaskListUIModel, expanded: Boolean, onAction: (TaskLi
text = {
RowWithIcon(
stringResource(Res.string.task_list_menu_sort_manual),
LucideIcons.Check.takeIf { false/*taskList.sorting == TaskListSorting.Manual*/ })
LucideIcons.Check.takeIf { sorting == TaskListSorting.Manual })
},
enabled = false, // TODO enable when sorting is implemented
enabled = sorting != TaskListSorting.Manual,
onClick = { onAction(TaskListMenuAction.SortManual) }
)

DropdownMenuItem(
text = {
RowWithIcon(
stringResource(Res.string.task_list_menu_sort_due_date),
LucideIcons.Check.takeIf { false/*taskList.sorting == TaskListSorting.Date*/ })
LucideIcons.Check.takeIf { sorting == TaskListSorting.DueDate })
},
enabled = false, // TODO enable when sorting is implemented
enabled = sorting != TaskListSorting.DueDate,
onClick = { onAction(TaskListMenuAction.SortDate) }
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

package net.opatry.tasks.app.ui.model

enum class TaskListSorting {
Manual,
Date,
}

data class TaskListUIModel(
val id: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,33 @@ import kotlinx.datetime.daysUntil
import kotlinx.datetime.toLocalDateTime

sealed class DateRange {
open val date: LocalDate? = null
open val numberOfDays: Int? = null
data object None : DateRange()
data class Overdue(val date: LocalDate, val numberOfDays: Int) : DateRange()
data object Yesterday : DateRange()
data object Today : DateRange()
data object Tomorrow : DateRange()
data class Later(val date: LocalDate, val numberOfDays: Int) : DateRange()
data class Overdue(override val date: LocalDate, override val numberOfDays: Int) : DateRange()
data class Today(override val date: LocalDate) : DateRange() {
override val numberOfDays: Int = 0
}
data class Later(override val date: LocalDate, override val numberOfDays: Int) : DateRange()
}

operator fun DateRange.compareTo(other: DateRange): Int {
// local variable for non null smart cast convenience
val lhsNumberOfDays = this.numberOfDays
val rhsNumberOfDays = other.numberOfDays
// No date should come last
return when {
lhsNumberOfDays == null -> 1
rhsNumberOfDays == null -> -1
else -> lhsNumberOfDays.compareTo(rhsNumberOfDays)
}
}

data class TaskUIModel(
val id: Long,
val title: String,
val dueDate: LocalDate? = null,
val completionDate: LocalDate? = null,
val notes: String = "",
val isCompleted: Boolean = false,
val position: String = "", // FIXME for debug?
Expand All @@ -56,12 +71,9 @@ data class TaskUIModel(
val daysUntilDueDate = todayLocalDate.daysUntil(dueLocalDate)

return when {
daysUntilDueDate < -1 -> DateRange.Overdue(dueLocalDate, -daysUntilDueDate)
daysUntilDueDate == -1 -> DateRange.Yesterday
daysUntilDueDate == 0 -> DateRange.Today
daysUntilDueDate == 1 -> DateRange.Tomorrow
// daysUntilDueDate > 1
else -> DateRange.Later(dueLocalDate, daysUntilDueDate)
daysUntilDueDate < 0 -> DateRange.Overdue(dueLocalDate, daysUntilDueDate)
daysUntilDueDate > 0 -> DateRange.Later(dueLocalDate, daysUntilDueDate)
else -> DateRange.Today(dueLocalDate)
}
}

Expand Down
Loading

0 comments on commit a1b884e

Please sign in to comment.