项目作者: icerockdev

项目描述 :
Pagination logic in common code for mobile (android & ios) Kotlin Multiplatform development
高级语言: Kotlin
项目地址: git://github.com/icerockdev/moko-paging.git
创建时间: 2020-02-17T06:05:10Z
项目社区:https://github.com/icerockdev/moko-paging

开源协议:Apache License 2.0

下载


moko-paging
GitHub license Download kotlin-version

Mobile Kotlin paging

This is a Kotlin MultiPlatform library that contains pagination logic for kotlin multiplatform

Table of Contents

Features

  • Pagination implements pagination logic for the data from abstract PagedListDataSource.
  • Managing a data loading process using Pagination asynchronous functions: loadFirstPage, loadNextPage,
    refresh or their duplicates with suspend modifier.
  • Observing states of Pagination using LiveData from moko-mvvm.

Requirements

  • Gradle version 6.8+
  • Android API 16+
  • iOS version 11.0+

Installation

root build.gradle

  1. allprojects {
  2. repositories {
  3. mavenCentral()
  4. }
  5. }

project build.gradle

  1. dependencies {
  2. commonMainApi("dev.icerock.moko:paging:0.7.1")
  3. }

Usage

You can use Pagination in commonMain sourceset.

Pagination creation:

  1. val pagination: Pagination<Int> = Pagination(
  2. parentScope = coroutineScope,
  3. dataSource = LambdaPagedListDataSource { currentList ->
  4. extrenalRepository.loadPage(currentList)
  5. },
  6. comparator = Comparator { a: Int, b: Int ->
  7. a - b
  8. },
  9. nextPageListener = { result: Result<List<Int>> ->
  10. if (result.isSuccess) {
  11. println("Next page successful loaded")
  12. } else {
  13. println("Next page loading failed")
  14. }
  15. },
  16. refreshListener = { result: Result<List<Int>> ->
  17. if (result.isSuccess) {
  18. println("Refresh successful")
  19. } else {
  20. println("Refresh failed")
  21. }
  22. },
  23. initValue = listOf(1, 2, 3)
  24. )

Managing data loading:

  1. // Loading first page
  2. pagination.loadFirstPage()
  3. // Loading next page
  4. pagination.loadNextPage()
  5. // Refreshing pagnation
  6. pagination.refresh()
  7. // Setting new list
  8. pagination.setData(itemsList)

Observing Pagination states:

  1. // Observing the state of the pagination
  2. pagination.state.addObserver { state: ResourceState<List<ItemClass>, Throwable> ->
  3. // ...
  4. }
  5. // Observing the next page loading process
  6. pagination.nextPageLoading.addObserver { isLoading: Boolean ->
  7. // ...
  8. }
  9. // Observing the refresh process
  10. pagination.refreshLoading.addObserver { isRefreshing: Boolean ->
  11. // ...
  12. }

Samples

Please see more examples in the sample directory.

Set Up Locally

Contributing

All development (both new features and bug fixes) is performed in the develop branch. This way master always contains the sources of the most recently released version. Please send PRs with bug fixes to the develop branch. Documentation fixes in the markdown files are an exception to this rule. They are updated directly in master.

The develop branch is pushed to master on release.

For more details on contributing please see the contributing guide.

License

  1. Copyright 2020 IceRock MAG Inc.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.