feat: 优化排序方式 & 增加按照截止日期排序
This commit is contained in:
parent
87cccfefc1
commit
d3cb35a631
3 changed files with 46 additions and 8 deletions
|
|
@ -38,7 +38,7 @@ android {
|
||||||
applicationId = "cn.super12138.todo"
|
applicationId = "cn.super12138.todo"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = 990
|
versionCode = 991
|
||||||
versionName = "2.3.3"
|
versionName = "2.3.3"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,9 @@ enum class SortingMethod(
|
||||||
AlphabeticalAscending(id = 5, nameRes = R.string.sorting_alphabetical_ascending),
|
AlphabeticalAscending(id = 5, nameRes = R.string.sorting_alphabetical_ascending),
|
||||||
|
|
||||||
// 按字母降序
|
// 按字母降序
|
||||||
AlphabeticalDescending(id = 6, nameRes = R.string.sorting_alphabetical_descending);
|
AlphabeticalDescending(id = 6, nameRes = R.string.sorting_alphabetical_descending),
|
||||||
|
|
||||||
|
DueDate(id = 7, nameRes = R.string.sorting_alphabetical_descending);
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun fromId(id: Int) = entries.find { it.id == id } ?: Sequential
|
fun fromId(id: Int) = entries.find { it.id == id } ?: Sequential
|
||||||
|
|
|
||||||
|
|
@ -43,12 +43,48 @@ class MainViewModel : ViewModel() {
|
||||||
DataStoreManager.sortingMethodFlow.flatMapLatest { sortingMethod ->
|
DataStoreManager.sortingMethodFlow.flatMapLatest { sortingMethod ->
|
||||||
toDos.map { list ->
|
toDos.map { list ->
|
||||||
when (SortingMethod.fromId(sortingMethod)) {
|
when (SortingMethod.fromId(sortingMethod)) {
|
||||||
SortingMethod.Sequential -> list.sortedBy { it.id }
|
SortingMethod.Sequential -> list.sortedWith(
|
||||||
SortingMethod.Category -> list.sortedBy { it.category }
|
comparator = compareBy<TodoEntity> { it.isCompleted } // 必须先要按照是否完成排序
|
||||||
SortingMethod.Priority -> list.sortedByDescending { it.priority } // 优先级高的在前
|
.thenBy { it.id }
|
||||||
SortingMethod.Completion -> list.sortedBy { it.isCompleted } // 未完成的在前
|
)
|
||||||
SortingMethod.AlphabeticalAscending -> list.sortedBy { it.content }
|
|
||||||
SortingMethod.AlphabeticalDescending -> list.sortedByDescending { it.content }
|
SortingMethod.Category -> list.sortedWith(
|
||||||
|
comparator = compareBy<TodoEntity> { it.isCompleted }
|
||||||
|
.thenByDescending { it.priority }
|
||||||
|
.thenBy { it.category }
|
||||||
|
)
|
||||||
|
|
||||||
|
SortingMethod.Priority -> list.sortedWith(
|
||||||
|
comparator = compareBy<TodoEntity> { it.isCompleted }
|
||||||
|
.thenByDescending { it.priority }
|
||||||
|
.thenBy { it.category }
|
||||||
|
) // 优先级高的在前
|
||||||
|
|
||||||
|
SortingMethod.Completion -> list.sortedWith(
|
||||||
|
comparator = compareBy<TodoEntity> { it.isCompleted }
|
||||||
|
.thenBy { it.category }
|
||||||
|
.thenBy { it.content }
|
||||||
|
.thenByDescending { it.priority }
|
||||||
|
) // 未完成的在前
|
||||||
|
SortingMethod.AlphabeticalAscending -> list.sortedWith(
|
||||||
|
comparator = compareBy<TodoEntity> { it.isCompleted }
|
||||||
|
.thenBy { it.category }
|
||||||
|
.thenBy { it.content }
|
||||||
|
.thenByDescending { it.priority }
|
||||||
|
)
|
||||||
|
|
||||||
|
SortingMethod.AlphabeticalDescending -> list.sortedWith(
|
||||||
|
comparator = compareBy<TodoEntity> { it.isCompleted }
|
||||||
|
.thenByDescending { it.category }
|
||||||
|
.thenByDescending { it.content }
|
||||||
|
.thenByDescending { it.priority }
|
||||||
|
)
|
||||||
|
|
||||||
|
SortingMethod.DueDate -> list.sortedWith(
|
||||||
|
comparator = compareBy<TodoEntity> { it.isCompleted }
|
||||||
|
.thenBy { it.dueDate }
|
||||||
|
.thenByDescending { it.priority }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue