Skip to content

Commit b4dd153

Browse files
committed
TaskRepository.kt: Add option to specify a set of fields to order by
* Add generic `OrderSpec` open class
1 parent 7c36e06 commit b4dd153

File tree

3 files changed

+45
-4
lines changed
  • data/common/src/main/kotlin/com/edricchan/studybuddy/domain/common/sorting
  • features/tasks
    • data/src/main/kotlin/com/edricchan/studybuddy/features/tasks/data/repo
    • domain/src/main/kotlin/com/edricchan/studybuddy/features/tasks/domain/repo

3 files changed

+45
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.edricchan.studybuddy.domain.common.sorting
2+
3+
/**
4+
* Specification for a specific field to order a list of data by.
5+
* @property field Desired field to order the data by.
6+
* @property direction Desired sorting direction.
7+
*/
8+
open class OrderSpec<F>(
9+
open val field: F,
10+
open val direction: SortDirection = SortDirection.Descending
11+
)
12+
13+
/** Desired sorting direction for a given field. */
14+
enum class SortDirection {
15+
/** Sort the resulting data in descending order, i.e. largest to smallest. */
16+
Descending,
17+
18+
/** Sort the resulting data in ascending order, i.e. smallest to largest. */
19+
Ascending
20+
}

features/tasks/data/src/main/kotlin/com/edricchan/studybuddy/features/tasks/data/repo/TaskRepository.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import androidx.paging.map
99
import com.edricchan.studybuddy.data.common.QueryMapper
1010
import com.edricchan.studybuddy.data.paging.firestore.firestorePagingSource
1111
import com.edricchan.studybuddy.data.source.firestore.IDefaultFirestoreDataSource
12+
import com.edricchan.studybuddy.domain.common.sorting.SortDirection
1213
import com.edricchan.studybuddy.features.tasks.data.mapper.toDomain
1314
import com.edricchan.studybuddy.features.tasks.data.mapper.toDto
1415
import com.edricchan.studybuddy.features.tasks.data.model.TodoItem
1516
import com.edricchan.studybuddy.features.tasks.data.model.TodoProject
1617
import com.edricchan.studybuddy.features.tasks.domain.model.TaskItem
1718
import com.edricchan.studybuddy.features.tasks.domain.repo.ITaskRepository
1819
import com.google.firebase.firestore.DocumentReference
20+
import com.google.firebase.firestore.Query
1921
import kotlinx.coroutines.ExperimentalCoroutinesApi
2022
import kotlinx.coroutines.flow.Flow
2123
import kotlinx.coroutines.flow.combine
@@ -115,7 +117,15 @@ class TaskRepository @Inject constructor(
115117
TodoItem.Field.IsDone.fieldName,
116118
true
117119
) else it
118-
}.orderBy(config.orderByField.toDto().fieldName)
120+
}.let {
121+
config.orderByFields.fold(it) { query, spec ->
122+
val direction = when (spec.direction) {
123+
SortDirection.Descending -> Query.Direction.DESCENDING
124+
SortDirection.Ascending -> Query.Direction.ASCENDING
125+
}
126+
query.orderBy(spec.field.toDto().fieldName, direction)
127+
}
128+
}
119129

120130
emitAll(
121131
Pager(

features/tasks/domain/src/main/kotlin/com/edricchan/studybuddy/features/tasks/domain/repo/ITaskRepository.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.edricchan.studybuddy.features.tasks.domain.repo
22

33
import androidx.paging.PagingData
44
import androidx.paging.cachedIn
5+
import com.edricchan.studybuddy.domain.common.sorting.OrderSpec
6+
import com.edricchan.studybuddy.domain.common.sorting.SortDirection
57
import com.edricchan.studybuddy.features.tasks.domain.model.TaskItem
68
import kotlinx.coroutines.CoroutineScope
79
import kotlinx.coroutines.flow.Flow
@@ -18,15 +20,24 @@ interface ITaskRepository {
1820
* @property includeArchived Whether archived tasks should be included in the list.
1921
* @property excludeCompleted Whether completed tasks should be excluded from the list.
2022
* @property pageSize Number of tasks to be shown per page.
21-
* @property orderByField The [TaskItem.Field] to order the list of tasks by.
23+
* @property orderByFields The set of [TaskOrderSpec] configurations to order the resulting data by,
24+
* applied sequentially in-order.
2225
*/
2326
data class PaginationConfig(
2427
val cachedCoroutineScope: CoroutineScope,
2528
val includeArchived: Boolean = false,
2629
val excludeCompleted: Boolean = false,
2730
val pageSize: Int = 30,
28-
val orderByField: TaskItem.Field = TaskItem.Field.CreatedAt
29-
)
31+
val orderByFields: Set<TaskOrderSpec> = setOf(TaskOrderSpec())
32+
) {
33+
data class TaskOrderSpec(
34+
override val field: TaskItem.Field = TaskItem.Field.CreatedAt,
35+
override val direction: SortDirection = SortDirection.Descending
36+
) : OrderSpec<TaskItem.Field>(
37+
field = field,
38+
direction = direction
39+
)
40+
}
3041

3142
/**
3243
* Gets a paginated list of tasks.

0 commit comments

Comments
 (0)