chRyNaN / paginate

A Kotlin Multi-platform pagination repository abstraction library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

paginate

A Kotlin Multi-platform pagination repository abstraction library.
GitHub tag (latest by date)

Using the library

  • First, simply create a PaginateRepository implementation for your data object. The easiest way to create a PaginateRepository is to extend from BasePaginateSource.
data class User(
    val id: String,
    val name: String,
    val created: DateTimeString
)

class UserPaginateRepository @Inject constructor(private val api: UserApi) : BasePaginateSource<String, User>() {

    override suspend fun fetch(
        count: Int,
        key: String?,
        direction: PageDirection,
        currentPageCount: Int
    ): PagedResult<String, User> {
        val users = if (direction == PageDirection.AFTER) {
            api.loadUsers(after = key, count = count)
        } else {
            api.loadUsers(before = key, count = count)
        }

        val pageInfo = PageInfo(
            index = currentPageCount,
            hasPreviousPage = false,
            hasNextPage = true,
            firstKey = users.firstOrNull().id,
            lastKey = users.firstOrNull().id
        )

        return PagedResult(
            info = pageInfo,
            items = users
        )
    }
}
  • Then, use the created repository to load and paginate the data.
// Subscribe to loaded pages, starting by loading the initial page.
userPaginateRepository.loadPages()
    .onEach {
        // Do something with the emitted PagedResult objects
    }
    .catch { ... }
    .launchIn(this)

// Suspending function to load the next page. Will dispatch to the above Flow and return the PagedResult.
val nextPage = userPaginateRepository.next()

// Suspending function to load the previous page. Will dispatch to the above Flow and return the PagedResult.
val previousPage = userPaginateRepository.previous()

Building the library

The library is provided through repsy. Checkout the releases page to get the latest version.
GitHub tag (latest by date)

Repository

repositories {
    maven { url = uri("https://repo.repsy.io/mvn/chrynan/public") }
}

Dependency

implementation("com.chrynan.paginate:paginate-core:$VERSION")

Documentation

Markdown Documentation is available in the docs folder.

License

Copyright 2020 chRyNaN

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

A Kotlin Multi-platform pagination repository abstraction library.

License:Apache License 2.0


Languages

Language:Kotlin 100.0%