• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kuuuurt/multiplatform-paging: Kotlin Multiplatform library for Pagination

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

kuuuurt/multiplatform-paging

开源软件地址(OpenSource Url):

https://github.com/kuuuurt/multiplatform-paging

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

Multiplatform Paging

Download

A Kotlin Multiplatform library for pagination.

Setup

This library is used on Kotlin Multiplatform that targets Android and iOS.

Check the table below for the compatibility across versions

Library Kotlin Paging
0.4.7 1.6.10 3.1.0
0.4.6 1.6.0 3.1.0
0.4.5 1.5.31 3.0.1
0.4.4 1.5.30 3.0.1
0.4.3 1.5.30 3.0.1
0.4.2 1.5.10 3.0.0
0.4.1 1.5.10 3.0.0
0.4.0 1.5.10 3.0.0
0.3.11 1.4.32 3.0.0-beta03
0.3.10 1.4.32 3.0.0-beta03
0.3.9 1.4.31 3.0.0-beta01
0.3.8 1.4.30 3.0.0-beta01
0.3.4+ 1.4.30 3.0.0-alpha13
0.3.3 1.4.30 3.0.0-alpha11
0.3.2 1.4.21 3.0.0-alpha11
0.3.1 1.4.10 3.0.0-alpha07
0.3.0 1.4.0 3.0.0-alpha06
0.2.0 1.3.70 3.0.0-alpha01
0.1.+ 1.3.70 2.1.1
0.1.0 1.3.61 2.1.1

Add the mavenCentral repository on your Project-level gradle

allprojects {
    repositories {
        ...
        mavenCentral()
    }
}

On the module, add the library in your dependencies.

kotlin {
    ...
    sourceSets["commonMain"].dependencies {
        implementation("io.github.kuuuurt:multiplatform-paging:0.4.7")
    }
}

On Android, make sure to add androidx.paging:paging-runtime as a dependency Note: On older versions, androidx.paging:paging-runtime:3.0.0 was added as a transitive depenency.

On iOS, you have to export it on your targets

kotlin {
    ...
    cocoapods {
        ...
        framework {
            ...
            export("io.github.kuuuurt:multiplatform-paging:0.4.7")
        }
    }

    val commonMain by sourceSets.getting {
        dependencies {
            api("io.github.kuuuurt:multiplatform-paging:0.4.7")
            ...
        }
    }
}

Usage

Common

Multiplatform paging exposes paginators which you can use in your multiplatform code to have common pagination on Android and iOS.

class MyMultiplatformController {
    private val pager = Pager<Int, String>(
        clientScope = coroutineScope,
        config = PagingConfig(
            pageSize = 10,
            enablePlaceholders = false // Ignored on iOS
        ),
        initialKey = 1, // Key to use when initialized
        getItems = { currentKey, size ->
            val items = ... // How you will get the items (API Call or Local DB)
            PagingResult(
                items = items,
                currentKey = currentKey,
                prevKey = { _, _ -> null }, // Key for previous page, null means don't load previous pages
                nextKey = { items, currentKey -> currentKey + 1 } // Key for next page. Use `items` or `currentKey` to get it depending on the pagination strategy
            )
        }
    )

    val pagingData: CommonFlow<PagingData<String>>
        get() = pager.pagingData
            .cachedIn(clientScope) // cachedIn from AndroidX Paging. on iOS, this is a no-op
            .asCommonFlow() // So that iOS can consume the Flow 
}

CommonFlow is a helper we can use to consume Flow on iOS. See FlowHelpers

Android

On Android, multiplatform paging uses Android Architecture Component's Paging library and exposes pagedList as a Flow<androidx.paging.PagedList<T>> which can be observed and submitted onto the PagedListAdapter.

class MyFragment : Fragment() {
    val myMultiplatformController = MyMultiplatformController()
    val myPagingDataAdapter = MyPagingDataAdapter()
    
    override fun onViewCreated(...) {
        super.onViewCreated(...)
      
        myMultiplatformController.pagingData
            .onEach { myPagingDataAdapter.submitData(it) }
            .launchIn(viewLifecyleOwner.lifecyclerScope)     
    }
}

iOS

On iOS, it exposes pagedList as a Flow<List<T>> which can be observed and rendered to a UITableView

class MyViewController UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    
    let myMultiplatformController = MyMultiplatformController()
    
    private var data: [T] = []
    private var count: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // setup...
        
        myMultiplatformController.pagingData.watch { [unowned self] nullableArray in
            guard let list = nullableArray?.compactMap({ $0 as? T }) else {
                return
            }
      
            self.data = list
            self.count = list.count
            self.tableView.reloadData()
        }
    }
}

Disclaimer: I'm not an iOS developer and this is what I was able to make of. If someone has a better example, contributions are welcome!

Jetpack Compose and SwiftUI

For samples using Jetpack Compose and SwiftUI, you can refer to MortyComposeKMM by joreilly.

Maintainers

Contributing

Feel free to dive in! Open an issue or submit PRs.

License

Apache-2.0 © Kurt Renzo Acosta




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap