Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Have StringPreference also persist a default if no value currently exist #12207

Closed
Mugurell opened this issue May 20, 2022 · 1 comment
Closed
Assignees

Comments

@Mugurell
Copy link
Contributor

Mugurell commented May 20, 2022

Saw the need on Fenix to use a locally persisted string but having to first provide a value and ensuring this is fist persisted to then always be returned on subsequent requests.
This could be done manually in-place but a more general approach supported by default in StringPreference can also be extracted.

Confirmed with @Amejia481 an updated api that would add a new parameter:

persistDefaultIfNotExists: Boolean = false

which would mean no breaking changes
but an easy way to add the new behavior for the full api now being:

 var persistedStringValue by stringPreference(
      key = "permissions_enabled",
      default = "blocked",
      persistDefaultIfNotExists = true,
 )

┆Issue is synchronized with this Jira Task

@Mugurell Mugurell self-assigned this May 20, 2022
Mugurell added a commit to Mugurell/android-components that referenced this issue May 20, 2022
…value of a string preference

Moved the tests for string preferences to a new file where we can use
Robolectric for just this preferences type and test the actual behavior without
relying on mocking which would prevent full testing the change because of the
two readings using a `null` default.
mergify bot pushed a commit that referenced this issue May 23, 2022
…ing preference

Moved the tests for string preferences to a new file where we can use
Robolectric for just this preferences type and test the actual behavior without
relying on mocking which would prevent full testing the change because of the
two readings using a `null` default.
@Mugurell
Copy link
Contributor Author

Change landed and is used on Fenix. Closing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant