Skip to content

Way to handle strings in android ViewModel. Persistently for VM layer and flexible for UI layer (for configuration changes)

Notifications You must be signed in to change notification settings

Andrew0000/UiText

Repository files navigation

UiText

Helps you to separate context-specific and context-independent parts of texts.

The problem:

How to store strings / texts in android View Model? The good approach is to store only ids. But what about more complex cases when we need not only simple string by id, but something formatted or even sometimes just plain string?

The solution:

Use UiText wrappers.
Example for typical ViewModel:

_field01.value = UiText.Str("Just string")

_field02.value = UiText.Res(R.string.plain_string)

_field03.value = UiText.Format(R.string.formatted_string, "first", 2, 3f)

_field04.value = UiText.Plural(R.plurals.plural_plain_string, 1)

_field05.value = UiText.PluralFormat(R.plurals.plural_format_string, 3,  1, "2", "3")

In view layer:

textView01.setUiText(it)

In this way we can forget about language changes (view recreation), because we still rely on ids basically.

Need more? Take a look at UiText.Custom

Setup:

  1. Add maven { url 'https://jitpack.io' } to the allprojects or dependencyResolutionManagement section in top-level build.gradle or settings.gradle.
    For example (settings.gradle):
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven { url "https://jitpack.io" }
    }
}
  1. Add implementation 'com.github.Andrew0000:UiText:$latest_version' to the module-level build.gradle

About

Way to handle strings in android ViewModel. Persistently for VM layer and flexible for UI layer (for configuration changes)

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages