https://kotlinlang.org logo
Title
d

Dragos Rachieru

05/02/2022, 9:13 AM
Hello, I am trying to create a multiplatform image loading library, I use
ktor
for remote loading and
okio
for local loading(files). I want to have an interface for fetcher that returns a flow of
Loading(progress)
,
Success(something)
,
Error(throwable)
I am not sure what
something
should be to support both fetchers.
For remote loading I was thinking about using
ByteReadChannel
like in this library: https://github.com/alialbaali/Kamel/blob/main/kamel-core/src/commonMain/kotlin/io/kamel/core/fetcher/HttpFetcher.kt
I want to use `okio`'s
Path
to load from files and support multiplatform, but I don't know how to create a
ByteReadChannel
, I tried creating an extension for it based on
File.readChannel
, but some of the methods are internal and I gave up in the end.
a

Aleksei Tirman [JB]

05/08/2022, 3:48 PM
You can create a channel from a
java.io.File
:
File("/path").readChannel()
or use a writer to write data to a channel:
val readChannel = GlobalScope.writer {
    // channel.writeFully()
    // channel.writeStringUtf8()
}.channel
d

Dragos Rachieru

05/09/2022, 11:33 AM
It works for jvm, but not for other platforms, I tried making a new method based on that and change it to support
ByteReadChannel
but no luck
a

Aleksei Tirman [JB]

05/09/2022, 11:33 AM
With a writer you can write data from any source.
d

Dragos Rachieru

05/09/2022, 11:52 AM
I will try, thanks