New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compose Multiplatform support #842
Comments
Check here for an update. TLDR: This is now planned! |
On package components
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.DefaultAlpha
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import com.tradernet.data.source.getHttpClient
import io.ktor.client.request.*
import org.jetbrains.skija.Image
import androidx.compose.foundation.Image as ComposeImage
suspend fun loadPicture(url: String): Result<ImageBitmap> {
val client = getHttpClient()
return try {
val image = client.get<ByteArray>(url)
Result.success(Image.makeFromEncoded(image).asImageBitmap())
} catch (e: Exception) {
Result.failure(e)
}
}
@Composable
actual fun ExternalImage(url: String, modifier: Modifier, OnFail: @Composable () -> Unit) {
var isLoading by remember { mutableStateOf(false) }
var hasFail by remember { mutableStateOf(false) }
var imageBitmap: ImageBitmap? by remember { mutableStateOf(null) }
LaunchedEffect(url) {
isLoading = true
loadPicture(url)
.onSuccess {
imageBitmap = it
}
.onFailure {
hasFail = true
}
isLoading = false
}
when {
isLoading -> {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
CircularProgressIndicator()
}
}
hasFail -> {
OnFail()
}
else -> {
imageBitmap?.let { bitmap ->
ComposeImage(
bitmap = bitmap,
contentDescription = "",
contentScale = ContentScale.FillWidth,
alpha = DefaultAlpha,
colorFilter = null,
)
} ?: OnFail()
}
}
} it's a variant I took from https://stackoverflow.com/questions/66002696/how-to-load-image-from-remote-url-in-kotlin-compose-desktop. |
I too would love to see a coil-desktop implementation, even if it was just using JVM calls under the hood, similar to this post |
Thanks @jershell . Just a note for those who will try to reuse your snippet: it was easier for me to use skiko v0.5.3 instead of skija, and then just swap the line in the try catch with:
Works but yikes for the caching indeed, so +1 for Coil-desktop. Not sure if there is multiplatform alternatives or even work-in-progress on this topic, please share if you know something. |
In case it's relevant to the feasibility - square/okhttp#6963 Libraries like Square Wire (gRPC) are also blocked on a multiplatform OkHttp Call.Factory. I hope in 2022, we get a good suite of KMP libraries for IO, Networking, RPC, Images, so hope this becomes feasible at some point. |
For multiplatform projects there is the Kamel, based on watch I saw of the source code, almost all the source is based on |
Hi, since coil 2.0 do you have any plans to implement this feature? |
Porting the library from Coroutines, ktor and okio are all multiplatform libraries. |
Adding ktor means adding more code in Android apps with no purpose. Either Coil stick to Android and it should not try to move to ktor (and keep a lightweight performant library), or it's decided that Coil wants to go full KMP. I think a lot of Android projects will not move to KMP right now, so Coil is still a very good option as is. Since Kamel is going multi-platform from start, maybe better to consider adding missing things to Kamel instead of trying to port a lot of Android specific code to KMP? |
This issue is for compose desktop which works on the jvm, not KMP, which I do think would be an easier ask. |
I am using this library for now which works great: Kamel-Media/Kamel#14 (comment) |
Yes desktop (jvm) version would be great, all of infrastructure as far as coroutines, okhttp etc should be there, so "only" Compose has a Note that if you want to write a proper multiplatform (mac, win, linux) desktop app, youre option basically is just Electron (VS Code, Spotify, Slack, etc), which is ..not great performance. I could see a world where compose desktop could be very popular. |
You can use libs like https://github.com/alialbaali/Kamel or https://github.com/ltttttttttttt/load-the-image and use @jershell's idea to wrap this libs with expect/actual. |
there is already an image loader that use coil as foundation to load images |
Hi folks, big update! There are now plans to add Kotlin Multiplatform support to Coil and this will be the main feature of 3.x. I can't offer a timeline, but this is top of mind. The plan is to leave Coil's public API largely the same (except for Android-specific classes) for an initial alpha then iterate on other public API changes we want to make for 3.x. Some of the main initial changes we'll need to make will be to abstract away usages of |
To be clear, by Kotlin Multiplatform support do you mean support for Compose Multiplatform or some UI framework independent solution (which will be much more complicated I think)? |
@colinrtwhite have you considered applying for a grant for this - http://kotlinfoundation.org/grants |
@mxalbert1996 The goal is to support Kotlin Multiplatform and Compose Multiplatform with as many targets as possible. The Android target will have the most functionality by supporting lifecycles, views, and other Android concepts. Other targets will be focused on supporting fetching + decoding images with integration with coroutines and less platform-specific support. I think we can accomplish this by replacing The biggest challenge I see at the moment is refactoring |
Good news! Any idea if skiko will be more or less performant than BitmapFactory on Android? For the Context I tend to use a ContentProvider that hold a static reference to the application Context, so that it can be used in androidMain code without being declared in commonMain. Do you see any issue with this approach? Is there a limitation with theme / image loading requiring the Activity/fragment context instead? |
@colinrtwhite Don't you think about making a development branch for working on this feature so we can contribute and make this process faster |
@glureau My guess is it would be slightly slower given @MohamedRejeb Will do! I'm going to create a |
To use |
@AmeerAmjed |
|
@colinrtwhite I probably misunderstood something from above but getting following when depending on
|
ah, I did have wrong dependency....following works
|
So, it's resolving but image isn't being displayed for some reason. I'll try to dig a bit deeper later but if you do want to try it's happening in Compose for Web client in following branch https://github.com/joreilly/PeopleInSpace/tree/dependency_updates This is code I have
|
@joreilly maybe we must pass string as Uri |
@joreilly I had the same issue when migrating to coil3. Network feature has been extracted from Surely you need |
Thanks @romainbsl , it's working now! |
Been trying for a while today to make a better replacement for this error dialog. Good grief... it's like JetBrains have never heard of SwingX. |
That's actually documented
I doubt that's very important to them, as this screen is not seen very often anyways and it's probably not worth adding another library, which is deprecated, just to render that error dialog differently, which you can change using |
Yeah I'm not saying to haul in the whole library for an error dialog, but to imitate it with one written in Compose which looks exactly like it. Editing just to add, I'm seeing it on average around every 2~5 minutes while doing dev , so I'm not sure what "often" means for you. |
Good afternoon everyone, I am translating a multiplatform project from compose image loader to kamel, and from it I decided to translate it to coil, I took the implementation on ktor and ran into a problem: |
Sounds like proguard maybe? |
Yes, I thought about that too, but to be honest, I don’t even know what could cause this, because coil documentation only says about coroutines and okhttp, but they are already specified in my proguard rules file |
It would be useful to actually see the error, Coil components use java services, so maybe proguard erases them |
I read the final error and there was (throwable=java.util.ServiceConfigurationError: coil3.util.FetcherServiceLoaderTarget: Provider coil3.network.ktor.internal.KtorNetworkFetcherServiceLoaderTarget not found) |
Hi, trying to use it on iOS (alpha6) and all is good but one thing, svg. While they do load, they are all tinted black.
I'll create a separate bug if this is all it should take, thank you! |
@lepicekmichal |
Does anyone know how to use |
@master-lzh You should use |
Thanks a lot, I tried it yesterday and it's working now |
Hi! I tested 3.0.0-alpha06 on android and iOS, but I can't understand how to set the headers for setting auth cookies. |
Hi everyone i received this error on my wasm web impl im using 3.0.0-alpha06 version on my project, any tip to solve this? |
Some related info here fwiw qdsfdhvh/compose-imageloader#446 |
Hi @joreilly i checked that but isn't working |
Did you set up CORS on your server to allow the request to go thru on the browser side? You may have to use a proxy service to access your images, like this one: https://wsrv.nl/ |
3.0.0-beta-2-eap-932 is out and support wasm js. |
This version is not on Maven Central, but is from different repository (KtorEAP) |
Is your feature request related to a problem? Please describe.
As an Android Developer, I would love to use Coil with Compose Desktop in the same way I use Coil for Compose on Android.
Describe the solution you'd like
Ideally, it would be the best to have the same approach we have on Android, so that the knowledge can be reused.
Additional context
I think it would be OK to link the image download scope to either a
LaunchedEffect
in aComposable
, or just link it to the Application lifecycle.The text was updated successfully, but these errors were encountered: