vinceglb / FileKit

Pick and save Files, Medias and Folder for Kotlin Multiplatform / KMP and Compose Multiplatform / CMP
https://vinceglb.github.io/FileKit/
MIT License
249 stars 8 forks source link

Android: AutoInitialize libray? #33

Open alorma opened 1 month ago

alorma commented 1 month ago

Hi! Thanks for the libray it's great to see more and more features coming to KMP and Compose Multiplaform!

I saw that the library requests initialization on Android, and thought about using ContentProvider for it.

Do you ever read about that? Libraries like firebase do it by themselves.

Here's a blog post that talks about that: https://medium.com/turkcell/using-android-content-providers-for-automatically-initialization-a5d3c05efd4e

Thanks anyway!

vinceglb commented 1 month ago

Hi @alorma ! Thank you for your kind words!

I tried to use Android App Startup to initialize the library under the hood, but I wasn't successful.

To access files on Android, we need to get a result from an activity. This can be done using:

I couldn't find a way to avoid using Activity or ComponentActivity in the process. When using App Startup, the context available isn't linked to any activity, and I couldn't figure out how to obtain the activity from the Android App Startup Initializer.

As a result, I created the FileKit.init(activity: ComponentActivity) method 🥲

If you have any idea on how to solve this problem and remove this method, I'd love to hear them!

alorma commented 1 month ago

Oh, my bad then. I thought that was supposed to be done on the Application not the Activity.

I could think on how to do that for compose. But no for regular Android view system

alorma commented 1 month ago

I mean.

On compose we have rememberLauncherForActivityResult() right?

That's a @Composable method that has access to LocalContext, and it can be cast to ComponentActivity (safely with an as?... So you will have the right activity then?

Maybe creating your own rememberLauncherForActivityResult()?

vinceglb commented 1 month ago

Sorry for the late response.

Yeah you're right, on compose we have access to all the information that we need from the @Composable. FileKit compose methods (like rememberFilePickerLauncher) are working without any initialization, even on Android.

I need to improve the documentation on that point:

The ideal is to find a way to remove this initialization setup and get the current ComponentActivity without any developer action. Android App Startup seems to be the solution, but I'm probably missing something to make it work.