Skip to content

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

Merged
terrakok merged 10 commits intomasterfrom
k.tskh/resourceBytes
Apr 23, 2024
Merged

[resources] Add functions to retrieve bytes from drawable or font resources.#4651
terrakok merged 10 commits intomasterfrom
k.tskh/resourceBytes

Conversation

@terrakok
Copy link
Copy Markdown
Member

@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

@terrakok terrakok requested review from MatkovIvan and igordmn April 18, 2024 15:27
@terrakok terrakok force-pushed the k.tskh/resourceBytes branch from 47bd458 to 4c1bb1e Compare April 19, 2024 15:46
…e loading functions and function to get current compose environment
@terrakok terrakok force-pushed the k.tskh/resourceBytes branch from 4c1bb1e to cd26460 Compare April 19, 2024 16:01
*/
@ExperimentalResourceApi
suspend fun getFontResourceBytes(
environment: ResourceEnvironment = getResourceEnvironment(),
Copy link
Copy Markdown
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
Copy Markdown
Member 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
Copy Markdown
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
Copy Markdown
Contributor

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