HI, all. I'm looking for a recommendation for a KM...
# multiplatform
d
HI, all. I'm looking for a recommendation for a KMP library to cache images from the web and cache them locally. Not looking for a UI widget, but for a library that, when given a URL, could load the image, cache it on the device, and retrieve it from the cache as needed. Anybody have a preferred library? I looked at and rejected COIL since it seems to want to be a UI piece, something I don't need or want.
d
Does it do persistent caching? I wasn't aware of that feature.
s
You can combine those two to create persistent storage. This is what Gemini came up with
Copy code
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.cache.*
import io.ktor.client.plugins.cache.storage.*
import java.io.File
Copy code
fun main() {
    val cacheDirectory = File("my_app_cache")
    val client = HttpClient(CIO) {
        install(HttpCache) {
            publicStorage(FileStorage(cacheDirectory)) // Use FileStorage for persistent caching
        }
    }
Copy code
// ... make HTTP requests with the client ...
}
❤️ 1
d
Excellent! Thank you for that. 😄
@streetsofboston QQ - the above code works across both iOS and Android? I'm expecting I can put the above inside of my viewmodel as part of the initialization of the app state, yeah?
s
Ah... "FileStorage" only works for JVM....
☹️ 1
You could use dependency inversion and injection, or expect/actual construct to user FileStorage for Android and use URLSessionConfiguration on iOS.
d
kk - will give that a try.
s
Maybe this will help: Use KtorNSURLSessionDelegate in iosMain module. Not sure if this works directly, but maybe this helps?
Copy code
import io.ktor.client.engine.darwin.Darwin
import io.ktor.client.engine.darwin.DarwinClientEngineConfig
import io.ktor.client.engine.darwin.KtorNSURLSessionDelegate
import io.ktor.client.HttpClient
import platform.Foundation.NSURLCache
import platform.Foundation.NSURLRequestReloadIgnoringLocalCacheData
import platform.Foundation.NSURLSession
import platform.Foundation.NSURLSessionConfiguration

val client = HttpClient(Darwin) {
    engine {
        val ktorDelegate = KtorNSURLSessionDelegate()
        val sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration().apply {
            urlCache = NSURLCache.sharedURLCache()
            requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData
        }
        val session = NSURLSession.sessionWithConfiguration(
            configuration = sessionConfiguration,
            delegate = ktorDelegate,
            delegateQueue = null
        )

        // Pass the preconfigured session and delegate to the Ktor engine.
        usePreconfiguredSession(session, ktorDelegate)
    }
}
d
@streetsofboston That definitely looks more promising.
a
https://github.com/vipulasri/kachetor I am using this to persist both android and iOS. You can take a look on there how it works. it is basically the same as using
expect
and
actual
👍 1