Skip to content

Commit

Permalink
For mozilla-mobile#3709: Add save to PDF UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 committed Oct 5, 2022
1 parent 5391b4c commit 5cce4b5
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 37 deletions.
16 changes: 16 additions & 0 deletions app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,22 @@ events:
notification_emails:
- android-probes@mozilla.com
expires: 113
save_to_pdf_tapped:
type: event
description: |
A user tapped the save to pdf option in the share sheet.
bugs:
- https://github.com/mozilla-mobile/fenix/issues/3709
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/27257
data_sensitivity:
- interaction
notification_emails:
- android-probes@mozilla.com
expires: 122
metadata:
tags:
- Sharing

onboarding:
syn_cfr_shown:
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/mozilla/fenix/FeatureFlags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ object FeatureFlags {
* Enables the wallpaper v2 enhancements.
*/
const val wallpaperV2Enabled = true

/**
* Enables the save to PDF feature.
*/
val saveToPDF = Config.channel.isNightlyOrDebug
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class DefaultBrowserToolbarMenuController(
}
is ToolbarMenu.Item.Share -> {
val directions = NavGraphDirections.actionGlobalShareFragment(
sessionId = currentSession?.id,
data = arrayOf(
ShareData(
url = getProperUrl(currentSession),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ class DefaultSessionControlController(

private fun showShareFragment(shareSubject: String, data: List<ShareData>) {
val directions = HomeFragmentDirections.actionGlobalShareFragment(
sessionId = store.state.selectedTabId,
shareSubject = shareSubject,
data = data.toTypedArray(),
)
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/org/mozilla/fenix/share/SaveToPDFInteractor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* 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

/**
* Callbacks for possible user interactions on the [SaveToPDFItem]
*/
interface SaveToPDFInteractor {
/**
* Generates a PDF from the given [tabId].
* @param tabId The ID of the tab to save as PDF.
*/
fun onSaveToPDF(tabId: String?)
}
70 changes: 70 additions & 0 deletions app/src/main/java/org/mozilla/fenix/share/SaveToPDFItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* 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.res.Configuration
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.mozilla.fenix.R
import org.mozilla.fenix.theme.FirefoxTheme

/**
* A save to PDF item.
*
* @param onClick event handler when the save to PDF item is clicked.
*/
@Composable
fun SaveToPDFItem(
onClick: () -> Unit,
) {
Row(
modifier = Modifier
.height(56.dp)
.fillMaxWidth()
.clickable(onClick = onClick),
verticalAlignment = Alignment.CenterVertically,
) {
Spacer(Modifier.width(16.dp))

Icon(
painter = painterResource(R.drawable.ic_download),
contentDescription = stringResource(
R.string.content_description_close_button,
),
tint = FirefoxTheme.colors.iconPrimary,
)

Spacer(Modifier.width(32.dp))

Text(
color = FirefoxTheme.colors.textPrimary,
text = stringResource(R.string.share_save_to_pdf),
style = FirefoxTheme.typography.subtitle1,
)
}
}

@Composable
@Preview
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO)
private fun SaveToPDFItemPreview() {
FirefoxTheme {
SaveToPDFItem {}
}
}
16 changes: 15 additions & 1 deletion app/src/main/java/org/mozilla/fenix/share/ShareController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import mozilla.components.concept.engine.prompt.ShareData
import mozilla.components.concept.sync.Device
import mozilla.components.concept.sync.TabData
import mozilla.components.feature.accounts.push.SendTabUseCases
import mozilla.components.feature.session.SessionUseCases
import mozilla.components.feature.share.RecentAppsStorage
import mozilla.components.service.glean.private.NoExtras
import mozilla.components.support.ktx.kotlin.isExtensionUrl
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.SyncAccount
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
Expand All @@ -47,6 +49,11 @@ interface ShareController {
fun handleReauth()
fun handleShareClosed()
fun handleShareToApp(app: AppShareOption)

/**
* Handles when a save to PDF action was requested.
*/
fun handleSaveToPDF(tabId: String?)
fun handleAddNewDevice()
fun handleShareToDevice(device: Device)
fun handleShareToAllDevices(devices: List<Device>)
Expand All @@ -68,12 +75,13 @@ interface ShareController {
* @param navController - [NavController] used for navigation.
* @param dismiss - callback signalling sharing can be closed.
*/
@Suppress("TooManyFunctions")
@Suppress("TooManyFunctions", "LongParameterList")
class DefaultShareController(
private val context: Context,
private val shareSubject: String?,
private val shareData: List<ShareData>,
private val sendTabUseCases: SendTabUseCases,
private val saveToPdfUseCase: SessionUseCases.SaveToPdfUseCase,
private val snackbar: FenixSnackbar,
private val navController: NavController,
private val recentAppsStorage: RecentAppsStorage,
Expand Down Expand Up @@ -130,6 +138,12 @@ class DefaultShareController(
dismiss(result)
}

override fun handleSaveToPDF(tabId: String?) {
Events.saveToPdfTapped.record(NoExtras())
handleShareClosed()
saveToPdfUseCase.invoke(tabId)
}

override fun handleAddNewDevice() {
val directions = ShareFragmentDirections.actionShareFragmentToAddNewDeviceFragment()
navController.navigate(directions)
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/org/mozilla/fenix/share/ShareFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.core.view.isVisible
import androidx.fragment.app.clearFragmentResult
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels
Expand All @@ -22,11 +24,13 @@ import mozilla.components.browser.state.selector.findTabOrCustomTab
import mozilla.components.concept.engine.prompt.PromptRequest
import mozilla.components.feature.accounts.push.SendTabUseCases
import mozilla.components.feature.share.RecentAppsStorage
import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.databinding.FragmentShareBinding
import org.mozilla.fenix.ext.getRootView
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.theme.FirefoxTheme

class ShareFragment : AppCompatDialogFragment() {

Expand Down Expand Up @@ -80,6 +84,7 @@ class ShareFragment : AppCompatDialogFragment() {
),
navController = findNavController(),
sendTabUseCases = SendTabUseCases(accountManager),
saveToPdfUseCase = requireComponents.useCases.sessionUseCases.saveToPdf,
recentAppsStorage = RecentAppsStorage(requireContext()),
viewLifecycleScope = viewLifecycleOwner.lifecycleScope,
) { result ->
Expand Down Expand Up @@ -111,6 +116,20 @@ class ShareFragment : AppCompatDialogFragment() {
}
shareToAppsView = ShareToAppsView(binding.appsShareLayout, shareInteractor)

if (FeatureFlags.saveToPDF) {
binding.dividerLineAppsShareAndPdfSection.isVisible = true
binding.savePdf.apply {
isVisible = true
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
FirefoxTheme {
SaveToPDFItem {
shareInteractor.onSaveToPDF(tabId = args.sessionId)
}
}
}
}
}
return binding.root
}

Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/org/mozilla/fenix/share/ShareInteractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.mozilla.fenix.share.listadapters.AppShareOption
*/
class ShareInteractor(
private val controller: ShareController,
) : ShareCloseInteractor, ShareToAccountDevicesInteractor, ShareToAppsInteractor {
) : ShareCloseInteractor, ShareToAccountDevicesInteractor, ShareToAppsInteractor, SaveToPDFInteractor {
override fun onReauth() {
controller.handleReauth()
}
Expand Down Expand Up @@ -40,4 +40,8 @@ class ShareInteractor(
override fun onShareToApp(appToShareTo: AppShareOption) {
controller.handleShareToApp(appToShareTo)
}

override fun onSaveToPDF(tabId: String?) {
controller.handleSaveToPDF(tabId)
}
}
25 changes: 21 additions & 4 deletions app/src/main/res/layout/fragment_share.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
app:layout_constraintBottom_toBottomOf="parent">

<FrameLayout
android:id="@+id/appsShareLayout"
android:id="@+id/devicesShareLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" />
app:layout_constraintBottom_toTopOf="@id/divider_line" />

<FrameLayout
android:id="@+id/devicesShareLayout"
android:id="@+id/appsShareLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/divider_line" />
app:layout_constraintTop_toBottomOf="@id/divider_line" />

<View
android:id="@+id/divider_line"
Expand All @@ -54,6 +54,23 @@
android:background="?borderPrimary"
app:layout_constraintBottom_toTopOf="@id/appsShareLayout" />

<View
android:id="@+id/divider_line_apps_share_and_pdf_section"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="?borderPrimary"
app:layout_constraintTop_toBottomOf="@id/appsShareLayout" />

<androidx.compose.ui.platform.ComposeView
android:visibility="gone"
android:id="@+id/save_pdf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider_line_apps_share_and_pdf_section" />

<androidx.constraintlayout.widget.Group
android:id="@+id/devicesShareGroup"
android:layout_width="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@
app:destination="@id/addNewDeviceFragment" />
<argument
android:name="sessionId"
android:defaultValue="null"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />
<argument
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,8 @@
<!-- Content description (not visible, for screen readers etc.):
"Share" button. Opens the share menu when pressed. -->
<string name="share_button_content_description">Share</string>
<!-- Text for the Save to PDF feature in the share menu -->
<string name="share_save_to_pdf">Save as PDF</string>
<!-- Sub-header in the dialog to share a link to another sync device -->
<string name="share_device_subheader">Send to device</string>
<!-- Sub-header in the dialog to share a link to an app from the full list -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ class DefaultBrowserToolbarMenuControllerTest {
navController.navigate(
directionsEq(
NavGraphDirections.actionGlobalShareFragment(
sessionId = browserStore.state.selectedTabId,
data = arrayOf(ShareData(url = "https://mozilla.org", title = "Mozilla")),
showPage = true,
),
Expand Down Expand Up @@ -656,6 +657,7 @@ class DefaultBrowserToolbarMenuControllerTest {
navController.navigate(
directionsEq(
NavGraphDirections.actionGlobalShareFragment(
sessionId = browserStore.state.selectedTabId,
data = arrayOf(ShareData(url = "https://mozilla.org", title = "Mozilla")),
showPage = true,
),
Expand Down
Loading

0 comments on commit 5cce4b5

Please sign in to comment.