https://kotlinlang.org logo
#compose
Title
# compose
n

Nacho Ruiz Martin

11/08/2023, 4:05 PM
👋 Any multiplatform async image loading library that supports local files? (
android.net.Uri
and file://
NSUrl
) I know Kamel but it doesn't support local files.
j

Joel Denke

11/08/2023, 4:06 PM
https://github.com/Kamel-Media/Kamel Not entirely sure if supports local files but think so.
n

Nacho Ruiz Martin

11/09/2023, 8:48 AM
It doesn’t support local files, no 😞 . At least not out of the box.
j

Joel Denke

11/09/2023, 8:53 AM
It has resource fetcher for desktop and Android/jvm. Would be possible iOS plugin hybride I guess.
But yeah struggle with images, fonts and such in general. Its possible using custom resource sharing with jetbrains resources plugin.
n

Nacho Ruiz Martin

11/09/2023, 8:57 AM
This is easier, I think. It’s just that the images can be selected locally. Imagine a social network, you are in the edit profile screen, the avatar image is either a remote URI (the API sent you) or a local URI (you selected a file and didn’t submit yet).
j

Joel Denke

11/09/2023, 8:59 AM
Yeah I would do a custom compasable compatible with all variants, Kamel, Coil, Url, File etc and delegate per platform. I do this always to also support Lottie :) Then able to support all frameworks with one image composable you control ;)
If it happen to mean for iOS using NsUrl manual lookup can hide implementation detail with expect/actual
n

Nacho Ruiz Martin

11/09/2023, 9:00 AM
🤔 You mean to check if NSUrl is a local file and delegate to Kamel if it is remote and print it with a native solution otherwise?
j

Joel Denke

11/09/2023, 10:43 AM
Yeah :)
n

Nacho Ruiz Martin

11/09/2023, 10:47 AM
That could work 👍 I’ll give it a try!
Thank you
j

Joel Denke

11/09/2023, 11:29 AM
Also my suggestion work for things like Lottie, video, animated gifs, vectors etc. If can use local or remote but also for remote selecting rendering option per platform. Would also decouple it to like CustomImageSource and different implementation for iOS and Android as of example. Not sure which targets you have. Then having almost everything in commonMain will be possible :)
n

Nacho Ruiz Martin

11/10/2023, 8:18 AM
That’s a good advice, thank you! 👍
l

Luca

11/12/2023, 4:53 AM
@Nacho Ruiz Martin kamel does support local files
https://github.com/Kamel-Media/Kamel#loading-an-image-resource
Copy code
// File (JVM, Native)
asyncPainterResource(data = File("/path/to/image.png"))
android.net.Uri
is different. See https://github.com/Kamel-Media/Kamel/issues/62 for
NSUrl
wrap it in
URL
. https://github.com/Kamel-Media/Kamel/blob/c83ef4ae6ad05bd58a00a51352bda7068057cd02[…]kamel-core/src/appleMain/kotlin/io/kamel/core/utils/Platform.kt If it’s not working feel free to create an issue
n

Nacho Ruiz Martin

11/12/2023, 8:56 AM
Hey, Luca. Sorry I wasn’t super clear! It indeed does support local files with the File API. What I meant is that it doesn’t support local URIs. Meaning
file://
NSURLs on iOS or
content://
android.net.Uri
on Android. I saw 62 and I will study the possibility to contribute to the library by adding support for them. I don’t have spare time now but as soon as I find some I’ll make a proposal 👍 .
l

Luca

11/12/2023, 5:46 PM
@Nacho Ruiz Martin so yeah, it looks like
NSURL
will only resolve remote urls based on how I implemented it. Can you get a path from the
NSURL
? if so you can just use
File("/path/to/image.png")
as I was saying.
what really should be done is adding apple only
Mapper<NSURL, Url>
and
Mapper<NSURL, File>
implementations
n

Nacho Ruiz Martin

11/12/2023, 5:49 PM
That sounds like a really elegant solution. Would you want me to test and contribute that to the library?
l

Luca

11/12/2023, 5:49 PM
n

Nacho Ruiz Martin

11/12/2023, 5:50 PM
Maybe having something to check if it's applicable just as you have for fetchers?
l

Luca

11/12/2023, 5:52 PM
that or just have a mapper return null if it can’t do it and try the next one if available
And yeah if you want go ahead, otherwise I’ll just make an issue for it and get to it eventually… but no guarrantee’s on time
n

Nacho Ruiz Martin

11/12/2023, 6:05 PM
Please, create the issue and I’ll try to schedule it 🙂.
l

Luca

11/12/2023, 6:08 PM
made the issues. Shouldn’t be too hard to implement if you get to it before me
n

Nacho Ruiz Martin

11/12/2023, 6:16 PM
Yes, sounds easy. I’ll try to go for it asap. Do you prefer the
Copy code
//Mapper.kt
...
public fun map(input: I): O?
nullable approach or the
Copy code
public val I.isSupported: Boolean
new property approach (that would require to update all mappers)?
l

Luca

11/12/2023, 6:28 PM
hmm maybe isSupported would be better. just to be consistent
you could just add a default implementation set to true in the mapper interface though and just override it for mappers that could have multiple output types
n

Nacho Ruiz Martin

11/12/2023, 6:36 PM
Ok 👍, let’s go for that.
👍 1
3 Views