Last active
December 15, 2025 19:06
-
-
Save Mugurell/e3f20977eaa1c214b4b77bf432cc10e5 to your computer and use it in GitHub Desktop.
Simple InMemoryPersister API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Subject: [PATCH] InMemoryPersister | |
| --- | |
| Index: mobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt | |
| IDEA additional info: | |
| Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
| <+>UTF-8 | |
| =================================================================== | |
| diff --git a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt | |
| --- a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt (revision 12123e5f77b7ee854fd6ac2fee99257866e0b4f1) | |
| +++ b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/FenixApplication.kt (date 1765825487087) | |
| @@ -124,16 +124,29 @@ | |
| private const val RAM_THRESHOLD_MEGABYTES = 1024 | |
| private const val BYTES_TO_MEGABYTES_CONVERSION = 1024.0 * 1024.0 | |
| +interface InMemoryPersister { | |
| + operator fun <T> get(key: String): T? | |
| + operator fun <T> set(key: String, value: T) | |
| +} | |
| + | |
| /** | |
| * The main application class for Fenix. Records data to measure initialization performance. | |
| * Installs [CrashReporter], initializes [Glean] in fenix builds and setup [Megazord] in the main process. | |
| */ | |
| @Suppress("Registered", "TooManyFunctions", "LargeClass") | |
| -open class FenixApplication : LocaleAwareApplication(), Provider { | |
| +open class FenixApplication : LocaleAwareApplication(), Provider, InMemoryPersister { | |
| init { | |
| recordOnInit() // DO NOT MOVE ANYTHING ABOVE HERE: the timing of this measurement is critical. | |
| } | |
| + private val inMemoryData = mutableMapOf<String, Any>() | |
| + @Suppress("UNCHECKED_CAST") | |
| + override fun <T> get(key: String) = inMemoryData[key] as? T | |
| + | |
| + override fun <T> set(key: String, value: T) { | |
| + inMemoryData[key] = value as Any | |
| + } | |
| + | |
| private val logger = Logger("FenixApplication") | |
| internal val isDeviceRamAboveThreshold by lazy { | |
| Index: mobile/android/fenix/app/src/main/java/org/mozilla/fenix/tabstray/ui/TabManagementFragment.kt | |
| IDEA additional info: | |
| Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
| <+>UTF-8 | |
| =================================================================== | |
| diff --git a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/tabstray/ui/TabManagementFragment.kt b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/tabstray/ui/TabManagementFragment.kt | |
| --- a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/tabstray/ui/TabManagementFragment.kt (revision 12123e5f77b7ee854fd6ac2fee99257866e0b4f1) | |
| +++ b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/tabstray/ui/TabManagementFragment.kt (date 1765823980345) | |
| @@ -10,6 +10,7 @@ | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| +import android.widget.Toast | |
| import androidx.activity.compose.BackHandler | |
| import androidx.activity.result.ActivityResultLauncher | |
| import androidx.annotation.UiThread | |
| @@ -23,6 +24,7 @@ | |
| import androidx.compose.ui.graphics.toArgb | |
| import androidx.compose.ui.platform.LocalDensity | |
| import androidx.fragment.app.DialogFragment | |
| +import androidx.fragment.app.Fragment | |
| import androidx.fragment.app.activityViewModels | |
| import androidx.fragment.app.setFragmentResultListener | |
| import androidx.fragment.compose.content | |
| @@ -53,8 +55,10 @@ | |
| import org.mozilla.fenix.GleanMetrics.PrivateBrowsingLocked | |
| import org.mozilla.fenix.GleanMetrics.TabsTray | |
| import org.mozilla.fenix.HomeActivity | |
| +import org.mozilla.fenix.InMemoryPersister | |
| import org.mozilla.fenix.R | |
| import org.mozilla.fenix.ext.actualInactiveTabs | |
| +import org.mozilla.fenix.ext.application | |
| import org.mozilla.fenix.ext.components | |
| import org.mozilla.fenix.ext.getBottomToolbarHeight | |
| import org.mozilla.fenix.ext.getTopToolbarHeight | |
| @@ -98,8 +102,12 @@ | |
| import org.mozilla.fenix.theme.ThemeManager | |
| import org.mozilla.fenix.utils.Settings | |
| import org.mozilla.fenix.utils.getSnackbarTimeout | |
| +import kotlin.let | |
| import kotlin.math.abs | |
| +val Fragment.inMemoryPersister | |
| + get() = (requireContext().application as? InMemoryPersister) | |
| + | |
| /** | |
| * The fullscreen fragment for displaying the tabs management UI. | |
| */ | |
| @@ -523,6 +531,15 @@ | |
| override fun onResume() { | |
| super.onResume() | |
| hideToolbar() | |
| + inMemoryPersister?.let { | |
| + val persistedText: String? = it["test"] | |
| + if (persistedText != null) { | |
| + Toast.makeText(requireContext(), persistedText, Toast.LENGTH_LONG).show() | |
| + } else { | |
| + it["test"] = "Persisted message: It works" | |
| + Toast.makeText(requireContext(), "Just persisted a new message", Toast.LENGTH_LONG).show() | |
| + } | |
| + } | |
| } | |
| private fun onCancelDownloadWarningAccepted(tabId: String?, source: String?) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment