kuuuurt / multiplatform-paging

Kotlin Multiplatform library for Pagination
Apache License 2.0
250 stars 22 forks source link

Add JVM support #21

Closed msfjarvis closed 2 years ago

msfjarvis commented 2 years ago

The AndroidX Paging APIs currently used in the Android source set are not Android specific and can be used on desktop directly. To facilitate this with minimal code duplication, I have created a new jvmAndAndroidMain directory to share code between JVM and Android targets and moved all current Android code to it.

I have tested this in a Compose for Desktop project, commits for which can be found here if needed.

This PR also includes a fix to allow syncing and publishing the project when sonatype.properties does not exist.

Fixes #10

msfjarvis commented 2 years ago

@kuuuurt would you be able to take a look at this anytime soon?

kuuuurt commented 2 years ago

Thansk for this @msfjarvis. I'm quite confused though. Isn't paging an Android dependency?

msfjarvis commented 2 years ago

Thansk for this @msfjarvis. I'm quite confused though. Isn't paging an Android dependency?

It's not! paging-common is actually built as a ~pura Java~ JVM library. See the difference in applied plugins for paging-common versus paging-runtime.

msfjarvis commented 2 years ago

@kuuuurt is there anything you'd like for me to change in this PR for it to be ready?

kuuuurt commented 2 years ago

Sorry @msfjarvis. I haven't found much time to work on personal projects. Thanks for letting me know that these are JVM libraries. Since these are not Android specific, can we just do a jvm() instead of targeting both then merging in a jvmAndAndroidMain?

msfjarvis commented 2 years ago

@kuuuurt I've dropped the Android target, and renamed the source set to jvmMain. Additionally I've also tested that I'm able to add it as a dependency in my Android sources.

kuuuurt commented 2 years ago

Thanks @msfjarvis. This would be included in the next release 0.4.5

kuuuurt commented 2 years ago

@msfjarvis 0.4.5 should be released

msfjarvis commented 2 years ago

@msfjarvis 0.4.5 should be released

I saw, thanks a lot!

glureau commented 2 years ago

Thanks for your work! Could we also include your extensions like this one? I feels like it's a really good starting point for newcomer to find those functions + a minimal documentation on Desktop(Jvm). I can open another ticket and maybe even try to contribute if you're ok.

@Composable
fun <T : Any> Flow<PagingData<T>>.collectAsLazyPagingItems(): LazyPagingItems<T> {
  val lazyPagingItems = remember(this) { LazyPagingItems(this) }

  LaunchedEffect(lazyPagingItems) { lazyPagingItems.collectPagingData() }
  LaunchedEffect(lazyPagingItems) { lazyPagingItems.collectLoadState() }

  return lazyPagingItems
}
msfjarvis commented 2 years ago

Thanks for your work! Could we also include your extensions like this one? I feels like it's a really good starting point for newcomer to find those functions + a minimal documentation on Desktop(Jvm). I can open another ticket and maybe even try to contribute if you're ok.

@Composable
fun <T : Any> Flow<PagingData<T>>.collectAsLazyPagingItems(): LazyPagingItems<T> {
  val lazyPagingItems = remember(this) { LazyPagingItems(this) }

  LaunchedEffect(lazyPagingItems) { lazyPagingItems.collectPagingData() }
  LaunchedEffect(lazyPagingItems) { lazyPagingItems.collectLoadState() }

  return lazyPagingItems
}

Unfortunately LazyPagingItems belongs to the androidx.paging:paging-compose library, which is published as an Android library rather than a JVM one. This prevents extensions like that to be compiled for desktop. For my own needs I ended up copying the class and removing its use of Android classes by hand, and I suspect that will have to be the case for everyone until proper Paging support is available for compose-desktop.

glureau commented 2 years ago

Thanks for the tip, will do the same!