Skip to content
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

Adds support for generic Uri strings. #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ class RealImageLoaderIntegrationTest {
testGet(data)
}

@Test
fun fileUrl() {
val data = "file://${copyNormalImageAssetToCacheDir().absolutePath}"
testLoad(data)
testGet(data)
}

@Test
fun filePath() {
val data = copyNormalImageAssetToCacheDir().absolutePath
testLoad(data)
testGet(data)
}

@Test
fun drawable() {
val data = context.getDrawableCompat(R.drawable.normal)
Expand Down
6 changes: 4 additions & 2 deletions coil-base/src/main/java/coil/RealImageLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import coil.fetch.ResourceFetcher
import coil.fetch.SourceResult
import coil.fetch.UriFetcher
import coil.map.FileMapper
import coil.map.UriStringMapper
import coil.map.HttpUriMapper
import coil.map.StringMapper
import coil.map.HttpStringMapper
import coil.memory.BitmapReferenceCounter
import coil.memory.DelegateService
import coil.memory.MemoryCache
Expand Down Expand Up @@ -94,9 +95,10 @@ internal class RealImageLoader(
private val networkObserver = NetworkObserver(context)

private val registry = ComponentRegistry(registry) {
add(StringMapper())
add(HttpStringMapper())
add(HttpUriMapper())
add(FileMapper())
add(UriStringMapper())

add(HttpUrlFetcher(okHttpClient))
add(UriFetcher(context))
Expand Down
10 changes: 10 additions & 0 deletions coil-base/src/main/java/coil/map/HttpStringMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package coil.map

import okhttp3.HttpUrl

internal class HttpStringMapper : Mapper<String, HttpUrl> {

override fun handles(data: String): Boolean = HttpUrl.parse(data) != null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have benchmarks to verify this, but I believe this adds a non-zero amount of overhead to every String URL request since we end up calling HttpUrl.get twice. Mappers are also called on the main thread so they need to be fast + non-blocking.


override fun map(data: String): HttpUrl = HttpUrl.get(data)
}
8 changes: 0 additions & 8 deletions coil-base/src/main/java/coil/map/StringMapper.kt

This file was deleted.

15 changes: 15 additions & 0 deletions coil-base/src/main/java/coil/map/UriStringMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package coil.map

import android.content.ContentResolver
import android.net.Uri

internal class UriStringMapper : Mapper<String, Uri> {

override fun map(data: String): Uri = Uri.parse(data).let {
return if (it.scheme != null) {
it
} else {
it.buildUpon().scheme(ContentResolver.SCHEME_FILE).build()
}
}
}