Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

io.github.kuuuurt:multiplatform-paging

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

io.github.kuuuurt:multiplatform-paging

A Kotlin Multiplatform library for pagination on Android and iOS

  • 0.6.2
  • Source
  • Maven
  • Socket score

Version published
Maintainers
1
Source

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

LibraryKotlinPaging
0.6.21.8.03.1.1
0.6.11.8.03.1.1
0.6.01.7.203.1.1
0.5.01.7.103.1.1
0.4.71.6.103.1.0
0.4.61.6.03.1.0
0.4.51.5.313.0.1
0.4.41.5.303.0.1
0.4.31.5.303.0.1
0.4.21.5.103.0.0
0.4.11.5.103.0.0
0.4.01.5.103.0.0
0.3.111.4.323.0.0-beta03
0.3.101.4.323.0.0-beta03
0.3.91.4.313.0.0-beta01
0.3.81.4.303.0.0-beta01
0.3.4+1.4.303.0.0-alpha13
0.3.31.4.303.0.0-alpha11
0.3.21.4.213.0.0-alpha11
0.3.11.4.103.0.0-alpha07
0.3.01.4.03.0.0-alpha06
0.2.01.3.703.0.0-alpha01
0.1.+1.3.702.1.1
0.1.01.3.612.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:{version}")
    }
}

On Android, make sure to add androidx.paging:paging-runtime as a dependency

On iOS, you have to export it on your targets

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

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

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

FAQs

Package last updated on 31 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc