Giter VIP home page Giter VIP logo

Comments (10)

angelix avatar angelix commented on July 4, 2024

Very useful feature and a bit of blocker for me, especially with the lack of reflection in Native.

from compose-multiplatform.

terrakok avatar terrakok commented on July 4, 2024

#4909

from compose-multiplatform.

enoler avatar enoler commented on July 4, 2024

#4909

Wow thank you! Any chance that it will be available the same for strings?

from compose-multiplatform.

terrakok avatar terrakok commented on July 4, 2024

No chance. If you want to read strings by path then just read a text file by a path.

String items don't have a file path

from compose-multiplatform.

angelix avatar angelix commented on July 4, 2024

No chance. If you want to read strings by path then just read a text file by a path.

What about getting a string by key, eg. getString(key = "en")?

from compose-multiplatform.

terrakok avatar terrakok commented on July 4, 2024

No. How will it work with qualifiers and arguments?
There is no a such thing as a full qualified key for the string in a text representation. (as path for other resource types) We need to combine path + key in the original xml. It is not what we want. Use text files instead.

from compose-multiplatform.

terrakok avatar terrakok commented on July 4, 2024

If you notice the API Res.drawable.byPath("drawable/my_icon.xml") doesn't work with qualifiers and environment. It associates a concrete file with a new resource instance.
If you have two icons: drawable-night/my_icon.xml and drawable-light/my_icon.xml, you are supposed to select a right icon by your own.

I understand that you would like to have something like a runtime search in your string resources based on a string key and a current environment but it is a huge performance problem. That's why we converted XML files to the internal format and use generated classes instead. To iterate by files in the runtime is not possible.

So, for your case you have to save strings to the regular txt file and read it as string on your side.

from compose-multiplatform.

enoler avatar enoler commented on July 4, 2024

If you notice the API Res.drawable.byPath("drawable/my_icon.xml") doesn't work with qualifiers and environment. It associates a concrete file with a new resource instance. If you have two icons: drawable-night/my_icon.xml and drawable-light/my_icon.xml, you are supposed to select a right icon by your own.

I understand that you would like to have something like a runtime search in your string resources based on a string key and a current environment but it is a huge performance problem. That's why we converted XML files to the internal format and use generated classes instead. To iterate by files in the runtime is not possible.

So, for your case you have to save strings to the regular txt file and read it as string on your side.

My current implementation consisted in creating a mapper that maps a string (the key) with the Resource. I had to do it like that because I was not able to use reflection. The main problem is that I need to mantain this file manually and it is very susceptible to bugs.

Sorry because of my ignorance because I don't know how the resources are internally working, but it wouldn't be possible to generate this mapping internally so we could access them through it?

from compose-multiplatform.

terrakok avatar terrakok commented on July 4, 2024

It is possible but the case is rare enough to maintain it. It requires more resources to support it than a real profit.
And the preferable way is static accessors for strings.

from compose-multiplatform.

angelix avatar angelix commented on July 4, 2024

I fully agree that the recommended and performant way to access resources, especially string resources, is through static accessors.

Android supports accessing resources by key, it discourages this practice for performance reasons. However, this capability exists for special cases where it might be necessary.

@Discouraged(message = "Use of this function is discouraged because resource reflection makes "
                         + "it harder to perform build optimizations and compile-time "
                         + "verification of code. It is much more efficient to retrieve "
                         + "resources by identifier (e.g. `R.foo.bar`) than by name (e.g. "
                         + "`getIdentifier(\"bar\", \"foo\", null)`).")
public int getIdentifier(String name, String defType, String defPackage)

My proposal is to introduce an opt-in feature that allows accessing resources by key. Should be discouraged, but available for the cases where is needed.

This feature could look something like the following:

fun Res.string.byPath(key: String): StringResource? {
  switch(key){
     "key1" -> Res.string.resource_with_key_1
     "key2" -> Res.string.resource_with_key_2
     else -> null
  }
}

from compose-multiplatform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.