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

[resources] Add functions to retrieve bytes from drawable or font resources. #4651

Merged
merged 10 commits into from
Apr 23, 2024

Conversation

terrakok
Copy link
Collaborator

@terrakok terrakok commented Apr 18, 2024

Implemented two new experimental functions:

/**
 * Retrieves the byte array of the drawable resource.
 *
 * @param environment The resource environment, which can be obtained from [rememberResourceEnvironment] or [getSystemResourceEnvironment].
 * @param resource The drawable resource.
 * @return The byte array representing the drawable resource.
 */
@ExperimentalResourceApi
suspend fun getDrawableResourceBytes(
    environment: ResourceEnvironment,
    resource: DrawableResource
): ByteArray {...}

/**
 * Retrieves the byte array of the font resource.
 *
 * @param environment The resource environment, which can be obtained from [rememberResourceEnvironment] or [getSystemResourceEnvironment].
 * @param resource The font resource.
 * @return The byte array representing the font resource.
 */
@ExperimentalResourceApi
suspend fun getFontResourceBytes(
    environment: ResourceEnvironment,
    resource: FontResource
): ByteArray {...}

fixes #4360

…e loading functions and function to get current compose environment
@ExperimentalResourceApi
suspend fun getFontResourceBytes(
environment: ResourceEnvironment = getResourceEnvironment(),
Copy link
Collaborator

@igordmn igordmn Apr 19, 2024

Choose a reason for hiding this comment

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

Having environment on the first place forces users to use resource name explicitly. Was this the intention?

I don't have a strong opinion here. On one hand, it is better for users to make a conscience choice to use the system environment, on the other hand it is verbose.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it was done to avoid parameters after a vararg in

suspend fun getString(
    environment: ResourceEnvironment,
    resource: StringResource,
    vararg formatArgs: Any
): String

Copy link
Collaborator

@igordmn igordmn Apr 23, 2024

Choose a reason for hiding this comment

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

In this case, I would suggest to do the same for the other functions - split them into 2:

@ExperimentalResourceApi
suspend fun getFontResourceBytes(
    environment: ResourceEnvironment,
    resource: FontResource
): ByteArray

@ExperimentalResourceApi
suspend fun getFontResourceBytes(
    environment: ResourceEnvironment,
): ByteArray = getFontResourceBytes(getSystemResourceEnvironment(), resource)

Otherwise we can't write getDrawableResourceBytes(Res.drawable.title), only getDrawableResourceBytes(resource = Res.drawable.title)

@terrakok terrakok requested a review from igordmn April 23, 2024 12:58
@terrakok terrakok requested a review from igordmn April 23, 2024 14:18
val theme: ThemeQualifier,
val density: DensityQualifier
@ExperimentalResourceApi
data class ResourceEnvironment internal constructor(
Copy link
Member

Choose a reason for hiding this comment

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

@terrakok terrakok merged commit afe548b into master Apr 23, 2024
@terrakok terrakok deleted the k.tskh/resourceBytes branch April 23, 2024 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose an API for converting DrawableResource -> ByteArray
3 participants