-
Notifications
You must be signed in to change notification settings - Fork 616
Migrate common from SharedPreferences to DataStore #6801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Javadoc Changes:--- /home/runner/diff/original/firebase-kotlindoc/android/client/firebase-common/_toc.yaml 2025-06-09 15:52:18.614894518 +0000
+++ /home/runner/diff/modified/firebase-kotlindoc/android/client/firebase-common/_toc.yaml 2025-06-09 15:48:47.817515308 +0000
@@ -27,6 +27,16 @@
- title: "FirebaseTooManyRequestsException"
path: "/docs/reference/android/com/google/firebase/FirebaseTooManyRequestsException.html"
+- title: "firebase.datastore"
+ path: "/docs/reference/android/com/google/firebase/datastore/package-summary.html"
+
+ section:
+ - title: "Classes"
+
+ section:
+ - title: "DataStoreKt"
+ path: "/docs/reference/android/com/google/firebase/datastore/DataStoreKt.html"
+
- title: "firebase.ktx"
status: deprecated
path: "/docs/reference/android/com/google/firebase/ktx/package-summary.html" --- /home/runner/diff/original/firebase-kotlindoc/android/com/google/firebase/datastore/DataStoreKt.html 1970-01-01 00:00:00.000000000 +0000
+++ /home/runner/diff/modified/firebase-kotlindoc/android/com/google/firebase/datastore/DataStoreKt.html 2025-06-09 15:48:47.824515390 +0000
@@ -0,0 +1,81 @@
+<html devsite="true">
+ <head>
+ <title>DataStoreKt</title>
+{% setvar book_path %}/_book.yaml{% endsetvar %}
+{% include "docs/reference/android/_reference-head-tags.html" %}
+ </head>
+ <body>
+ <h1>DataStoreKt</h1>
+ <p>
+ <pre>public final class <a href="/docs/reference/android/com/google/firebase/datastore/DataStoreKt.html">DataStoreKt</a></pre>
+ </p>
+ <hr>
+ <h2>Summary</h2>
+ <div class="devsite-table-wrapper">
+ <table class="responsive">
+ <colgroup>
+ <col width="40%">
+ <col>
+ </colgroup>
+ <thead>
+ <tr>
+ <th colspan="100%"><h3>Public methods</h3></th>
+ </tr>
+ </thead>
+ <tbody class="list">
+ <tr>
+ <td><code>static final @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T</code></td>
+ <td>
+ <div><code><T extends <a href="https://developer.android.com/reference/kotlin/java/lang/Object.html">Object</a>> <a href="/docs/reference/android/com/google/firebase/datastore/DataStoreKt.html">DataStoreKt</a>.<a href="/docs/reference/android/com/google/firebase/datastore/DataStoreKt.html#(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)">getOrDefault</a>(<br> @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a> receiver,<br> @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html">Preferences.Key</a><@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T> key,<br> @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T defaultValue<br>)</code></div>
+ <p>Helper method for getting the value out of a <code><a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a></code> object if it exists, else falling back to the default value.</p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ <div class="list">
+ <h2>Public methods</h2>
+ <div class="api-item"><a name="(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key, kotlin.Any)"></a><a name="-androidx.datastore.preferences.core.Preferences-.getOrDefault-androidx.datastore.preferences.core.Preferences.Key-kotlin.Any-"></a><a name="getordefault"></a>
+ <h3 class="api-name" id="(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)">DataStoreKt.getOrDefault</h3>
+ <pre class="api-signature no-pretty-print">public static final @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T <T extends <a href="https://developer.android.com/reference/kotlin/java/lang/Object.html">Object</a>> <a href="/docs/reference/android/com/google/firebase/datastore/DataStoreKt.html">DataStoreKt</a>.<a href="/docs/reference/android/com/google/firebase/datastore/DataStoreKt.html#(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)">getOrDefault</a>(<br> @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a> receiver,<br> @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html">Preferences.Key</a><@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T> key,<br> @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T defaultValue<br>)</pre>
+ <p>Helper method for getting the value out of a <code><a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a></code> object if it exists, else falling back to the default value.</p>
+ <p>This is primarily useful when working with an instance of <code><a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences.html">MutablePreferences</a></code></p>
+ <ul>
+ <li>
+ <p>like when working within an <code><a href="/docs/reference/android/com/google/firebase/datastore/DataStorage.html#editSync(kotlin.Function1)">DataStorage.editSync</a></code> callback.</p>
+ </li>
+ </ul>
+ <p>Example:</p>
+ <pre class="prettyprint">dataStore.editSync((pref) -> {<br> long heartBeatCount = DataStoreKt.getOrDefault(pref, HEART_BEAT_COUNT_TAG, 0L);<br> heartBeatCount+=1;<br> pref.set(HEART_BEAT_COUNT_TAG, heartBeatCount);<br><br> return null;<br>});</pre>
+ <div class="devsite-table-wrapper">
+ <table class="responsive">
+ <colgroup>
+ <col width="40%">
+ <col>
+ </colgroup>
+ <thead>
+ <tr>
+ <th colspan="100%">Parameters</th>
+ </tr>
+ </thead>
+ <tbody class="list">
+ <tr>
+ <td><code>@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html">Preferences.Key</a><@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T> key</code></td>
+ <td>
+ <p>The typed key of the entry to get data for.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code>@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> T defaultValue</code></td>
+ <td>
+ <p>A value to default to, if the key isn't found.</p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ </div>
+ </div>
+ </body>
+</html>
+ --- /home/runner/diff/original/firebase-kotlindoc/android/com/google/firebase/datastore/package-summary.html 1970-01-01 00:00:00.000000000 +0000
+++ /home/runner/diff/modified/firebase-kotlindoc/android/com/google/firebase/datastore/package-summary.html 2025-06-09 15:48:47.824515390 +0000
@@ -0,0 +1,26 @@
+<html devsite="true">
+ <head>
+ <title>com.google.firebase.datastore</title>
+{% setvar book_path %}/_book.yaml{% endsetvar %}
+{% include "docs/reference/android/_reference-head-tags.html" %}
+ </head>
+ <body>
+ <h1>com.google.firebase.datastore</h1>
+ <h2>Classes</h2>
+ <div class="devsite-table-wrapper">
+ <table class="responsive">
+ <colgroup>
+ <col width="40%">
+ <col>
+ </colgroup>
+ <tbody class="list">
+ <tr>
+ <td><code><a href="/docs/reference/android/com/google/firebase/datastore/DataStoreKt.html">DataStoreKt</a></code></td>
+ <td></td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ </body>
+</html>
+ --- /home/runner/diff/original/firebase-kotlindoc/kotlin/client/firebase-common/_toc.yaml 2025-06-09 15:52:18.617894552 +0000
+++ /home/runner/diff/modified/firebase-kotlindoc/kotlin/client/firebase-common/_toc.yaml 2025-06-09 15:48:47.825515402 +0000
@@ -29,6 +29,9 @@
- title: "Firebase"
path: "/docs/reference/kotlin/com/google/firebase/Firebase.html"
+- title: "firebase.datastore"
+ path: "/docs/reference/kotlin/com/google/firebase/datastore/package-summary.html"
+
- title: "firebase.ktx"
status: deprecated
path: "/docs/reference/kotlin/com/google/firebase/ktx/package-summary.html" --- /home/runner/diff/original/firebase-kotlindoc/kotlin/com/google/firebase/datastore/package-summary.html 1970-01-01 00:00:00.000000000 +0000
+++ /home/runner/diff/modified/firebase-kotlindoc/kotlin/com/google/firebase/datastore/package-summary.html 2025-06-09 15:48:47.832515484 +0000
@@ -0,0 +1,70 @@
+<html devsite="true">
+ <head>
+ <title>com.google.firebase.datastore</title>
+{% setvar book_path %}/_book.yaml{% endsetvar %}
+{% include "docs/reference/kotlin/_reference-head-tags.html" %}
+ </head>
+ <body>
+ <h1>com.google.firebase.datastore</h1>
+ <h2>Extension functions summary</h2>
+ <div class="devsite-table-wrapper">
+ <table class="responsive">
+ <colgroup>
+ <col width="40%">
+ <col>
+ </colgroup>
+ <tbody class="list">
+ <tr>
+ <td><code>T</code></td>
+ <td>
+ <div><code><T : <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?> <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a>.<a href="/docs/reference/kotlin/com/google/firebase/datastore/package-summary.html#(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)">getOrDefault</a>(key: <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html">Preferences.Key</a><T>, defaultValue: T)</code></div>
+ <p>Helper method for getting the value out of a <code><a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a></code> object if it exists, else falling back to the default value.</p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ <h2>Extension functions</h2>
+ <div class="api-item"><a name="(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key, kotlin.Any)"></a><a name="-androidx.datastore.preferences.core.Preferences-.getOrDefault-androidx.datastore.preferences.core.Preferences.Key-kotlin.Any-"></a><a name="getordefault"></a>
+ <h3 class="api-name" id="(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)">getOrDefault</h3>
+ <pre class="api-signature no-pretty-print">fun <T : <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?> <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a>.<a href="/docs/reference/kotlin/com/google/firebase/datastore/package-summary.html#(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)">getOrDefault</a>(key: <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html">Preferences.Key</a><T>, defaultValue: T): T</pre>
+ <p>Helper method for getting the value out of a <code><a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html">Preferences</a></code> object if it exists, else falling back to the default value.</p>
+ <p>This is primarily useful when working with an instance of <code><a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences.html">MutablePreferences</a></code></p>
+ <ul>
+ <li>
+ <p>like when working within an <code><a href="/docs/reference/kotlin/com/google/firebase/datastore/DataStorage.html#editSync(kotlin.Function1)">DataStorage.editSync</a></code> callback.</p>
+ </li>
+ </ul>
+ <p>Example:</p>
+ <pre class="prettyprint">dataStore.editSync((pref) -> {<br> long heartBeatCount = DataStoreKt.getOrDefault(pref, HEART_BEAT_COUNT_TAG, 0L);<br> heartBeatCount+=1;<br> pref.set(HEART_BEAT_COUNT_TAG, heartBeatCount);<br><br> return null;<br>});</pre>
+ <div class="devsite-table-wrapper">
+ <table class="responsive">
+ <colgroup>
+ <col width="40%">
+ <col>
+ </colgroup>
+ <thead>
+ <tr>
+ <th colspan="100%">Parameters</th>
+ </tr>
+ </thead>
+ <tbody class="list">
+ <tr>
+ <td><code>key: <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html">Preferences.Key</a><T></code></td>
+ <td>
+ <p>The typed key of the entry to get data for.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code>defaultValue: T</code></td>
+ <td>
+ <p>A value to default to, if the key isn't found.</p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ </div>
+ </body>
+</html>
+ |
Coverage Report 1Affected Products
Test Logs |
Test Results 50 files - 1 008 50 suites - 1 008 1m 16s ⏱️ - 34m 30s Results for commit 5619a56. ± Comparison against base commit f61a649. This pull request removes 5909 tests.
♻️ This comment has been updated with latest results. |
Size Report 1Affected Products
Test Logs |
Startup Time Report 1Note: Layout is sometimes suboptimal due to limited formatting support on GitHub. Please check this report on GCS. Notes
Startup Times
|
Firebase AI Mock Responses Check
|
* | ||
* @hide | ||
*/ | ||
class DataStorage(val context: Context, val name: String) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use a name like DataStorageForJava
, JavaDataStorage
, DataStorageJavaAdapter
, or the like to make it even more clear that this is intended for Java only? Since this is not meant to be part of the public API we can go with ugly but clearer naming
* | ||
* Automatically migrates data from any `SharedPreferences` that share the same context and name. | ||
* | ||
* There should only ever be _one_ instance of this class per context and name variant. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this something we should try to enforce through code?
import android.os.Build; | ||
import androidx.annotation.RestrictTo; | ||
import androidx.annotation.VisibleForTesting; | ||
import androidx.datastore.preferences.core.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No star imports
Per b/423592727,
This adds a
DataStore
class that wraps aroundandroidx.datastore.core.DataStore
, and provides more Java friendly methods for creation and interop. This should make migrations fromSharedPreferences
in existing Java code much easier.Note that usages of
SharedPreferences
in Kotlin should just useandroidx.datastore.core.DataStore
directly instead, as they have no need for the interop features. Furthermore, it would make it easier in the future to remove ourDataStore
wrapper once more SDKs have been migrated to Kotlin.This wrapper class comes with full documentation detailing how it should be used, and quirks to keep in mind.
Since
firebase-common
has been migrated to use this wrapper, and common has tests with theirSharedPreferences
usages, our wrapper already has implicit testing.This PR also fixes the following: