diff --git a/app/src/main/java/org/mozilla/fenix/components/Core.kt b/app/src/main/java/org/mozilla/fenix/components/Core.kt index 5dfc2c5763..b0d482f561 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Core.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Core.kt @@ -92,6 +92,7 @@ import org.mozilla.fenix.perf.StrictModeManager import org.mozilla.fenix.perf.lazyMonitored import org.mozilla.fenix.settings.SupportUtils import org.mozilla.fenix.settings.advanced.getSelectedLocale +import org.mozilla.fenix.share.SaveToPDFMiddleware import org.mozilla.fenix.telemetry.TelemetryMiddleware import org.mozilla.fenix.utils.getUndoDelay import org.mozilla.geckoview.GeckoRuntime @@ -244,6 +245,7 @@ class Core( LastMediaAccessMiddleware(), HistoryMetadataMiddleware(historyMetadataService), SessionPrioritizationMiddleware(), + SaveToPDFMiddleware(context), ) BrowserStore( diff --git a/app/src/main/java/org/mozilla/fenix/share/SaveToPDFMiddleware.kt b/app/src/main/java/org/mozilla/fenix/share/SaveToPDFMiddleware.kt new file mode 100644 index 0000000000..6ea627b8fe --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/share/SaveToPDFMiddleware.kt @@ -0,0 +1,46 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.share + +import android.content.Context +import android.widget.Toast +import android.widget.Toast.LENGTH_LONG +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import mozilla.components.browser.state.action.BrowserAction +import mozilla.components.browser.state.action.EngineAction +import mozilla.components.browser.state.state.BrowserState +import mozilla.components.lib.state.Action +import mozilla.components.lib.state.Middleware +import mozilla.components.lib.state.MiddlewareContext +import org.mozilla.fenix.R + +/** + * [BrowserAction] middleware reacting in response to Save to PDF related [Action]s. + * @property context An Application context. + */ +class SaveToPDFMiddleware( + private val context: Context, +) : Middleware { + + @OptIn(DelicateCoroutinesApi::class) + override fun invoke( + ctx: MiddlewareContext, + next: (BrowserAction) -> Unit, + action: BrowserAction, + ) { + if (action is EngineAction.SaveToPdfExceptionAction) { + // See https://github.com/mozilla-mobile/fenix/issues/27649 for more details, + // why a Toast is used here. + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText(context, R.string.unable_to_save_to_pdf_error, LENGTH_LONG).show() + } + } else { + next(action) + } + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3c24676a90..84f3908732 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1028,6 +1028,8 @@ Share Save as PDF + + Unable to generate PDF Send to device