Skip to content

Dynamically updatable strings.xml wrapper for Android

Notifications You must be signed in to change notification settings

ravindrabarthwal/FString

Repository files navigation

FString - Dynamically Updatable* strings.xml for Android

FString provides an elegant solution for dynamically updating the strings.xml values. It uses a combination of Android SharedPreferences, strings.xml and reflections to dynamically update the values in SharedPreferences. By using FString, strings.xml existing value can be updated using JSON string without releasing a new version.

FString stores the updated values in the SharedPreferences (as we can not update strings.xml directly). FString first checks the value in the SharedPreferences otherwise it returns the default value from the strings.xml

Installation

  • In your project's module build.gradle add this dependency
    implementation 'com.ravindrabarthwal.fstring:fstring:1.0.1'
  • In your Android Application class add the following code
    import com.example.myapp.R.string as RString // Import your string 
    
    class MyApp: Application() {
    
        override fun onCreate() {
            super.onCreate()
            FString.init(RString::class.java) // Initializes the FString
        }
        
    }

Usage

To access the strings

  // This is how you get strings without FString
  context.getString(R.string.my_app_name) // This is how you normally do
  
  // To get the string using FString use
  FString.getString(context, R.string.my_app_name)
  
  // If you prefer extension function use
  context.getFString(R.string.my_app_name) // This is how FString works ;)

  // String with formatting args
  // Eg. For StringRes <string name="my_app_version">MyApp %s</string>
  context.getString(R.string.my_app_version, "v1.0") // This is how you normally use getString with args

  // To get the same using FString use
  FString.getString(context, R.string.my_app_version, listOf("v1.0"))

  // If you prefer extension function use
  context.getFString(R.string.my_app_version, listOf("v1.0"))

To update the FString values

  /*
   * Assume this JSON is coming from server. The JSON string 
   * must be parseable to a JSON object with key and value pairs
   */
  var jsonFromServer = """
      {
        "my_app_name": "MyApp v2.0",
        "some_other_string": "this is so cool",
      }
  """.trimIndent()
  
  // This will update the SharedPreference using keys from
  // above JSON and corresponding value. The SharedPreference will
  // not save any key that is not is strings.xml
  FString.update(context, jsonFromServer) 
  

To clear values

  /*
   * If you want to clear some keys' value to default, either update 
   * the FString with previous values or use the below api
   */
   
   // To clear all values
   FString.clearAll(context)
   
   // To clear specific values send JSON String that must be 
   // parseable to a JSONArray containing the keys to clear
   
   val jsonToClear = """
       [
          "my_app_name"
       ]
   """.trimIndent()
   
   FString.clear(context, jsonToClear)
  

Limitations

All TODOs Fixed

Contributors

  • Ravindra Barthwal (Me) alt text alt text
  • Palash Bansal alt text

Contribution Guideline

Feel free to fork it and create a pull request.

You can support this project by clicking on star on top right. 🙏

Made with ❤ at Mouve - Animated Stories Creator for Instagram

Releases

No releases published

Packages

No packages published

Languages