Skip to content

Commit

Permalink
Bug 1900960 - Add an option to cancel a download r=android-reviewers,…
Browse files Browse the repository at this point in the history
…ohall

Differential Revision: https://phabricator.services.mozilla.com/D215189
  • Loading branch information
iorgamgabriel committed Jul 16, 2024
1 parent ba1252e commit b7cbc43
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* 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.translations.preferences.downloadlanguages

import androidx.compose.foundation.background
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.AlertDialog
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import org.mozilla.fenix.R
import org.mozilla.fenix.compose.annotation.LightDarkPreview
import org.mozilla.fenix.compose.button.TextButton
import org.mozilla.fenix.theme.FirefoxTheme
import java.util.Locale

/**
* Cancel Download Languages file Dialog.
*
* @param language Language name that should be displayed in the dialogue title.
* @param onConfirmDelete Invoked when the user clicks on the "Yes" dialog button.
* @param onCancel Invoked when the user clicks on the "No" dialog button.
*/
@Composable
fun CancelDownloadFileDialog(
language: String? = null,
onConfirmDelete: () -> Unit,
onCancel: () -> Unit,
) {
AlertDialog(
onDismissRequest = {},
modifier = Modifier.background(
color = FirefoxTheme.colors.layer2,
shape = RoundedCornerShape(8.dp),
),
title = {
language?.let {
Text(
text = stringResource(
id = R.string.cancel_download_language_file_dialog_title,
it,
),
color = FirefoxTheme.colors.textPrimary,
style = FirefoxTheme.typography.headline7,
)
}
},
confirmButton = {
TextButton(
text = stringResource(id = R.string.cancel_download_language_file_dialog_positive_button_text),
upperCaseText = false,
onClick = { onConfirmDelete() },
)
},
dismissButton = {
TextButton(
text = stringResource(id = R.string.cancel_download_language_file_negative_button_text),
upperCaseText = false,
onClick = { onCancel() },
)
},
backgroundColor = FirefoxTheme.colors.layer2,
)
}

@Composable
@LightDarkPreview
private fun CancelDownloadFileDialogPreview() {
FirefoxTheme {
CancelDownloadFileDialog(
language = Locale.CHINA.displayLanguage,
onConfirmDelete = {},
onCancel = {},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import org.mozilla.fenix.shopping.ui.ReviewQualityCheckInfoCard
import org.mozilla.fenix.shopping.ui.ReviewQualityCheckInfoType
import org.mozilla.fenix.theme.FirefoxTheme
import org.mozilla.fenix.translations.DownloadIconIndicator
import org.mozilla.fenix.translations.DownloadInProgressIndicator
import java.util.Locale

/**
Expand Down Expand Up @@ -423,10 +424,19 @@ private fun downloadLanguageItemContentDescriptionPreference(
)
}

ModelState.DOWNLOAD_IN_PROGRESS, ModelState.DELETION_IN_PROGRESS -> {
ModelState.DELETION_IN_PROGRESS -> {
contentDescription =
"$label $itemDescription " + stringResource(
id = R.string.download_languages_item_content_description_in_progress_state,
id = R.string.download_languages_item_content_description_delete_in_progress_state,
)
}

ModelState.DOWNLOAD_IN_PROGRESS -> {
contentDescription =
stringResource(
id = R.string.download_languages_item_content_description_download_in_progress_state,
item.languageModel.language?.localizedDisplayName ?: "",
item.languageModel.size?.toMegabyteOrKilobyteString() ?: "0",
)
}
}
Expand Down Expand Up @@ -462,7 +472,11 @@ private fun IconDownloadLanguageItemPreference(
)
}

ModelState.DOWNLOAD_IN_PROGRESS, ModelState.DELETION_IN_PROGRESS -> {
ModelState.DOWNLOAD_IN_PROGRESS -> {
DownloadInProgressIndicator()
}

ModelState.DELETION_IN_PROGRESS -> {
DownloadIconIndicator(
icon = painterResource(id = R.drawable.mozac_ic_sync_24),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class DownloadLanguagesPreferenceFragment : Fragment() {
onItemClick = { downloadLanguageItemPreference ->
if (downloadLanguageItemPreference.languageModel.status ==
ModelState.DOWNLOADED ||
downloadLanguageItemPreference.languageModel.status ==
ModelState.DOWNLOAD_IN_PROGRESS ||
shouldShowPrefDownloadLanguageFileDialog(
downloadLanguageItemPreference,
)
Expand Down Expand Up @@ -229,10 +231,7 @@ class DownloadLanguagesPreferenceFragment : Fragment() {
DownloadLanguageItemPreference(
languageModel = languageModel,
type = DownloadLanguageItemTypePreference.GeneralLanguage,
enabled = !(
languageModel.status == ModelState.DOWNLOAD_IN_PROGRESS ||
languageModel.status == ModelState.DELETION_IN_PROGRESS
),
enabled = languageModel.status != ModelState.DELETION_IN_PROGRESS,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,37 @@ class LanguageDialogPreferenceFragment : DialogFragment() {
savedInstanceState: Bundle?,
): View {
val view = ComposeView(requireContext())
if (args.modelState == ModelState.DOWNLOADED) {
setPrefDeleteLanguageFileDialog(view)
} else {
if (args.modelState == ModelState.NOT_DOWNLOADED) {
setDownloadLanguageFileDialog(view)
}
when (args.modelState) {
ModelState.NOT_DOWNLOADED -> setDownloadLanguageFileDialog(view)
ModelState.DOWNLOAD_IN_PROGRESS -> setCancelDownloadFileDialog(view)
ModelState.DOWNLOADED -> setPrefDeleteLanguageFileDialog(view)
ModelState.DELETION_IN_PROGRESS -> {}
ModelState.ERROR_DELETION -> {}
ModelState.ERROR_DOWNLOAD -> {}
}

return view
}

private fun setCancelDownloadFileDialog(composeView: ComposeView) {
composeView.apply {
setContent {
FirefoxTheme {
CancelDownloadFileDialog(
language = args.languageDisplayName,
onConfirmDelete = {
deleteOrDownloadModel(
modelOperation = ModelOperation.DELETE,
languageToManage = args.languageCode,
)
findNavController().popBackStack()
},
onCancel = { findNavController().popBackStack() },
)
}
}
}
}

private fun setPrefDeleteLanguageFileDialog(composeView: ComposeView) {
composeView.apply {
setContent {
Expand Down
16 changes: 15 additions & 1 deletion mobile/android/fenix/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2497,7 +2497,13 @@
<!-- Content description (not visible, for screen readers etc.): For a language list item that was downloaded, the user can now delete it. -->
<string name="download_languages_item_content_description_downloaded_state">Delete</string>
<!-- Content description (not visible, for screen readers etc.): For a language list item, downloading is in progress. -->
<string name="download_languages_item_content_description_in_progress_state">In progress</string>
<string name="download_languages_item_content_description_in_progress_state" moz:removedIn="129" tools:ignore="UnusedResources">In progress</string>
<!-- Content description (not visible, for screen readers etc.): For a language list item, deleting is in progress. -->
<string name="download_languages_item_content_description_delete_in_progress_state">In progress</string>
<!-- Content description (not visible, for screen readers etc.): For a language list item, downloading is in progress.
The first parameter is the language name, for example, "Spanish".
The second parameter is the language file size, for example, "(3.91 KB)". -->
<string name="download_languages_item_content_description_download_in_progress_state">Stop downloading %1$s (%2$s)</string>
<!-- Content description (not visible, for screen readers etc.): For a language list item that was not downloaded. -->
<string name="download_languages_item_content_description_not_downloaded_state">Download</string>
<!-- The title of the warning card informs the user that an error has occurred when fetching the list of languages. -->
Expand Down Expand Up @@ -2526,6 +2532,14 @@
<!-- Button text on the dialog used by the translations feature to cancel deleting a language. -->
<string name="delete_language_file_dialog_negative_button_text">Cancel</string>

<!-- Title for the dialog used by the translations feature to confirm canceling a download in progress for a language file.
The first parameter is the name of the language, for example, "Spanish". -->
<string name="cancel_download_language_file_dialog_title">Cancel %1$s download?</string>
<!-- Button text on the dialog used by the translations feature confirms canceling a download in progress for a language file. -->
<string name="cancel_download_language_file_dialog_positive_button_text">Yes</string>
<!-- Button text on the dialog used by the translations feature to dismiss the dialog. -->
<string name="cancel_download_language_file_negative_button_text">No</string>

<!-- Title for the data saving mode warning dialog used by the translations feature.
This dialog will be presented when the user attempts to download a language or perform
a translation without the necessary language files downloaded first when Android's data saver mode is enabled and the user is not using WiFi.
Expand Down

0 comments on commit b7cbc43

Please sign in to comment.