在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):kuuuurt/multiplatform-paging开源软件地址(OpenSource Url):https://github.com/kuuuurt/multiplatform-paging开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):Multiplatform PagingA Kotlin Multiplatform library for pagination. SetupThis library is used on Kotlin Multiplatform that targets Android and iOS. Check the table below for the compatibility across versions
Add the 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 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")
...
}
}
} UsageCommonMultiplatform 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
}
AndroidOn Android, multiplatform paging uses Android Architecture Component's Paging library and exposes class MyFragment : Fragment() {
val myMultiplatformController = MyMultiplatformController()
val myPagingDataAdapter = MyPagingDataAdapter()
override fun onViewCreated(...) {
super.onViewCreated(...)
myMultiplatformController.pagingData
.onEach { myPagingDataAdapter.submitData(it) }
.launchIn(viewLifecyleOwner.lifecyclerScope)
}
} iOSOn iOS, it exposes 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 SwiftUIFor samples using Jetpack Compose and SwiftUI, you can refer to MortyComposeKMM by joreilly. Maintainers
ContributingFeel free to dive in! Open an issue or submit PRs. LicenseApache-2.0 © Kurt Renzo Acosta |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论