-
-
Notifications
You must be signed in to change notification settings - Fork 588
feat(Android, Tabs): Update approach for loading external sources for tab icons #3216
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3523828
Loading external images with Fresco
t0maboro fb67d61
Move ImageLoader to a separate file
t0maboro cff84ed
Add comments
t0maboro 8a0b889
Linter
t0maboro 78b900f
Update comment
t0maboro d704e67
Move file, change namespace
t0maboro bf27cd1
Object -> functions
t0maboro 5cf70e7
Update android/src/main/java/com/swmansion/rnscreens/gamma/tabs/image…
t0maboro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
android/src/main/java/com/swmansion/rnscreens/gamma/tabs/TabsImageLoader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.swmansion.rnscreens.utils | ||
t0maboro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
import android.content.Context | ||
import android.graphics.drawable.Drawable | ||
import android.util.Log | ||
import androidx.core.graphics.drawable.toDrawable | ||
import androidx.core.net.toUri | ||
import com.facebook.common.executors.CallerThreadExecutor | ||
import com.facebook.common.references.CloseableReference | ||
import com.facebook.datasource.BaseDataSubscriber | ||
import com.facebook.datasource.DataSource | ||
import com.facebook.drawee.backends.pipeline.Fresco | ||
import com.facebook.imagepipeline.image.CloseableImage | ||
import com.facebook.imagepipeline.image.CloseableStaticBitmap | ||
import com.facebook.imagepipeline.request.ImageRequestBuilder | ||
|
||
object TabsImageLoader { | ||
t0maboro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
fun load( | ||
context: Context, | ||
uri: String, | ||
onLoaded: (Drawable) -> Unit, | ||
) { | ||
val source = resolveSource(context, uri) ?: return | ||
val finalUri = | ||
when (source) { | ||
is RNSImageSource.DrawableRes -> { | ||
"res://${context.packageName}/${source.resId}".toUri() | ||
} | ||
is RNSImageSource.UriString -> { | ||
source.uri.toUri() | ||
} | ||
} | ||
|
||
val imageRequest = | ||
ImageRequestBuilder | ||
.newBuilderWithSource(finalUri) | ||
.build() | ||
|
||
val dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, context) | ||
dataSource.subscribe( | ||
object : BaseDataSubscriber<CloseableReference<CloseableImage>>() { | ||
override fun onNewResultImpl(dataSource: DataSource<CloseableReference<CloseableImage>?>) { | ||
if (!dataSource.isFinished) return | ||
val imageReference = dataSource.result ?: return | ||
val closeableImage = imageReference.get() | ||
|
||
if (closeableImage is CloseableStaticBitmap) { | ||
val bitmap = closeableImage.underlyingBitmap | ||
val drawable = bitmap.toDrawable(context.resources) | ||
onLoaded(drawable) | ||
} | ||
|
||
imageReference.close() | ||
} | ||
|
||
override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>?>) { | ||
Log.e("[RNScreens]", "Error loading image: $uri", dataSource.failureCause) | ||
} | ||
}, | ||
CallerThreadExecutor.getInstance(), | ||
) | ||
} | ||
|
||
private fun resolveSource( | ||
context: Context, | ||
uri: String, | ||
): RNSImageSource? { | ||
// In release builds, assets are coming with bundle and we need to work with resource id. | ||
// In debug, metro is responsible for handling assets via http. | ||
// At the moment, we're supporting images (drawable) and SVG icons (raw). | ||
// For any other type, we may consider adding a support in the future if needed. | ||
if (uri.startsWith("_")) { | ||
val drawableResId = context.resources.getIdentifier(uri, "drawable", context.packageName) | ||
if (drawableResId != 0) { | ||
return RNSImageSource.DrawableRes(drawableResId) | ||
} | ||
val rawResId = context.resources.getIdentifier(uri, "raw", context.packageName) | ||
if (rawResId != 0) { | ||
return RNSImageSource.DrawableRes(rawResId) | ||
} | ||
Log.e("[RNScreens]", "Resource not found in drawable or raw: $uri") | ||
return null | ||
} | ||
|
||
// If asset isn't included in android source directories and we're loading it from given path. | ||
return RNSImageSource.UriString(uri) | ||
} | ||
|
||
private sealed class RNSImageSource { | ||
data class DrawableRes( | ||
val resId: Int, | ||
) : RNSImageSource() | ||
|
||
data class UriString( | ||
val uri: String, | ||
) : RNSImageSource() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.