-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements a ObjectMapper class for Compose
In #80, we implemented the Configuration class. We introduced a separate mapper class for Composable functions so that you can customize the object conversion process.
- Loading branch information
1 parent
a41b0b3
commit 55db2a7
Showing
30 changed files
with
1,159 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
soil-query-compose/src/commonMain/kotlin/soil/query/compose/InfiniteQueryObjectMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose | ||
|
||
import soil.query.InfiniteQueryRef | ||
import soil.query.QueryChunks | ||
import soil.query.QueryState | ||
import soil.query.QueryStatus | ||
import soil.query.core.getOrThrow | ||
import soil.query.core.isNone | ||
import soil.query.core.map | ||
|
||
/** | ||
* A mapper that converts [QueryState] to [InfiniteQueryObject]. | ||
*/ | ||
interface InfiniteQueryObjectMapper { | ||
|
||
/** | ||
* Converts the given [QueryState] to [InfiniteQueryObject]. | ||
* | ||
* @param query The query reference. | ||
* @param select A function that selects the object from the chunks. | ||
* @return The converted object. | ||
*/ | ||
fun <T, S, U> QueryState<QueryChunks<T, S>>.toObject( | ||
query: InfiniteQueryRef<T, S>, | ||
select: (chunks: QueryChunks<T, S>) -> U | ||
): InfiniteQueryObject<U, S> | ||
|
||
companion object | ||
} | ||
|
||
/** | ||
* The default [InfiniteQueryObjectMapper]. | ||
*/ | ||
val InfiniteQueryObjectMapper.Companion.Default: InfiniteQueryObjectMapper | ||
get() = DefaultInfiniteQueryObjectMapper | ||
|
||
private object DefaultInfiniteQueryObjectMapper : InfiniteQueryObjectMapper { | ||
override fun <T, S, U> QueryState<QueryChunks<T, S>>.toObject( | ||
query: InfiniteQueryRef<T, S>, | ||
select: (chunks: QueryChunks<T, S>) -> U | ||
): InfiniteQueryObject<U, S> = when (status) { | ||
QueryStatus.Pending -> InfiniteQueryLoadingObject( | ||
reply = reply.map(select), | ||
replyUpdatedAt = replyUpdatedAt, | ||
error = error, | ||
errorUpdatedAt = errorUpdatedAt, | ||
staleAt = staleAt, | ||
fetchStatus = fetchStatus, | ||
isInvalidated = isInvalidated, | ||
refresh = query::invalidate, | ||
loadMore = query::loadMore, | ||
loadMoreParam = null | ||
) | ||
|
||
QueryStatus.Success -> InfiniteQuerySuccessObject( | ||
reply = reply.map(select), | ||
replyUpdatedAt = replyUpdatedAt, | ||
error = error, | ||
errorUpdatedAt = errorUpdatedAt, | ||
staleAt = staleAt, | ||
fetchStatus = fetchStatus, | ||
isInvalidated = isInvalidated, | ||
refresh = query::invalidate, | ||
loadMore = query::loadMore, | ||
loadMoreParam = query.key.loadMoreParam(reply.getOrThrow()) | ||
) | ||
|
||
QueryStatus.Failure -> if (reply.isNone) { | ||
InfiniteQueryLoadingErrorObject( | ||
reply = reply.map(select), | ||
replyUpdatedAt = replyUpdatedAt, | ||
error = checkNotNull(error), | ||
errorUpdatedAt = errorUpdatedAt, | ||
staleAt = staleAt, | ||
fetchStatus = fetchStatus, | ||
isInvalidated = isInvalidated, | ||
refresh = query::invalidate, | ||
loadMore = query::loadMore, | ||
loadMoreParam = null | ||
) | ||
} else { | ||
InfiniteQueryRefreshErrorObject( | ||
reply = reply.map(select), | ||
replyUpdatedAt = replyUpdatedAt, | ||
error = checkNotNull(error), | ||
errorUpdatedAt = errorUpdatedAt, | ||
staleAt = staleAt, | ||
fetchStatus = fetchStatus, | ||
isInvalidated = isInvalidated, | ||
refresh = query::invalidate, | ||
loadMore = query::loadMore, | ||
loadMoreParam = query.key.loadMoreParam(reply.getOrThrow()) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.