Skip to content

Commit

Permalink
Merge pull request #57 from opatry/api-listAll-as-extension
Browse files Browse the repository at this point in the history
Extract Task*Api.listAll as an extension to ease Api as interface extraction
  • Loading branch information
opatry authored Oct 13, 2024
2 parents 0a47fd0 + 87b4f43 commit ea18c3a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,6 @@ class TaskListsApi(
}
}

/**
* Iterate on [list]'s paginated results and returns single list of all task lists.
*
* @see [list]
*/
suspend fun listAll(): List<TaskList> {
var nextPageToken: String? = null
return buildList {
do {
val response = list(maxResults = 100, nextPageToken)
addAll(response.items)
nextPageToken = response.nextPageToken
} while (nextPageToken != null)
}
}

/**
* [Updates the authenticated user's specified task list](https://developers.google.com/tasks/reference/rest/v1/tasklists/patch). This method supports patch semantics.
*
Expand Down Expand Up @@ -176,4 +160,20 @@ class TaskListsApi(
throw ClientRequestException(response, response.bodyAsText())
}
}
}
}

/**
* Iterate on [TaskListsApi.list]'s paginated results and returns single list of all task lists.
*
* @see [TaskListsApi.list]
*/
suspend fun TaskListsApi.listAll(): List<TaskList> {
var nextPageToken: String? = null
return buildList {
do {
val response = list(maxResults = 100, nextPageToken)
addAll(response.items)
nextPageToken = response.nextPageToken
} while (nextPageToken != null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,46 +191,6 @@ class TasksApi(
}
}

/**
* Iterate on [list]'s paginated results and returns single list of all tasks.
*
* @see [list]
*/
suspend fun listAll(
taskListId: String,
completedMin: Instant? = null,
completedMax: Instant? = null,
dueMin: Instant? = null,
dueMax: Instant? = null,
showCompleted: Boolean = true,
showDeleted: Boolean = false,
showHidden: Boolean = false,
updatedMin: Instant? = null,
showAssigned: Boolean = false
): List<Task> {
var nextPageToken: String? = null
return buildList {
do {
val response = list(
taskListId,
completedMin,
completedMax,
dueMin,
dueMax,
maxResults = 100,
nextPageToken,
showCompleted,
showDeleted,
showHidden,
updatedMin,
showAssigned
)
addAll(response.items)
nextPageToken = response.nextPageToken
} while (nextPageToken != null)
}
}

/**
* [Moves the specified task](https://developers.google.com/tasks/reference/rest/v1/tasks/move) to another position in the destination task list.
*
Expand Down Expand Up @@ -306,4 +266,44 @@ class TasksApi(
throw ClientRequestException(response, response.bodyAsText())
}
}
}
}

/**
* Iterate on [TasksApi.list]'s paginated results and returns single list of all tasks.
*
* @see [TasksApi.list]
*/
suspend fun TasksApi.listAll(
taskListId: String,
completedMin: Instant? = null,
completedMax: Instant? = null,
dueMin: Instant? = null,
dueMax: Instant? = null,
showCompleted: Boolean = true,
showDeleted: Boolean = false,
showHidden: Boolean = false,
updatedMin: Instant? = null,
showAssigned: Boolean = false
): List<Task> {
var nextPageToken: String? = null
return buildList {
do {
val response = list(
taskListId,
completedMin,
completedMax,
dueMin,
dueMax,
maxResults = 100,
nextPageToken,
showCompleted,
showDeleted,
showHidden,
updatedMin,
showAssigned
)
addAll(response.items)
nextPageToken = response.nextPageToken
} while (nextPageToken != null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import net.opatry.google.tasks.TaskListsApi
import net.opatry.google.tasks.TasksApi
import net.opatry.google.tasks.listAll
import net.opatry.google.tasks.model.Task
import net.opatry.google.tasks.model.TaskList
import net.opatry.tasks.data.entity.TaskEntity
Expand Down

0 comments on commit ea18c3a

Please sign in to comment.