Skip to content

Commit

Permalink
Bug 1862289 - Translations UI Integration Low Data Translate
Browse files Browse the repository at this point in the history
  • Loading branch information
iorgamgabriel authored and mergify[bot] committed Mar 4, 2024
1 parent 33306d0 commit 1304472
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ class TranslationsDialogBinding(

// Session Translations State Behavior (Tab)
val sessionTranslationsState = state.sessionState.translationsState

val fromSelected =
sessionTranslationsState.translationEngineState?.initialFromLanguage(
translateFromLanguages,
)

fromSelected?.let {
// Dispatch initialFrom Language only the first time when it is null.
if (fromSelected != null && translationsDialogStore.state.initialFrom == null) {
translationsDialogStore.dispatch(
TranslationsDialogAction.UpdateFromSelectedLanguage(
fromSelected,
Expand All @@ -94,7 +96,9 @@ class TranslationsDialogBinding(
sessionTranslationsState.translationEngineState?.initialToLanguage(
translateToLanguages,
)
toSelected?.let {

// Dispatch initialTo Language only the first time when it is null.
if (toSelected != null && translationsDialogStore.state.initialTo == null) {
translationsDialogStore.dispatch(
TranslationsDialogAction.UpdateToSelectedLanguage(
toSelected,
Expand Down Expand Up @@ -130,6 +134,12 @@ class TranslationsDialogBinding(
TranslationsDialogAction.UpdateTranslationError(sessionTranslationsState.translationError),
)
}

sessionTranslationsState.translationDownloadSize?.let {
translationsDialogStore.dispatch(
TranslationsDialogAction.UpdateDownloadTranslationDownloadSize(it),
)
}
}
}

Expand All @@ -153,11 +163,13 @@ class TranslationsDialogBinding(
),
)

translationsDialogStore.dispatch(
TranslationsDialogAction.UpdateTranslated(
true,
),
)
if (!translationsDialogStore.state.isTranslated) {
translationsDialogStore.dispatch(
TranslationsDialogAction.UpdateTranslated(
true,
),
)
}

if (translationsDialogStore.state.dismissDialogState == DismissDialogState.WaitingToBeDismissed) {
translationsDialogStore.dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.theme.FirefoxTheme
import org.mozilla.fenix.translations.preferences.downloadlanguages.DownloadLanguageFileDialog
import org.mozilla.fenix.translations.preferences.downloadlanguages.DownloadLanguageFileDialogType
import org.mozilla.fenix.translations.preferences.downloadlanguages.DownloadLanguagesFeature

/**
* The enum is to know what bottom sheet to open.
Expand All @@ -56,8 +60,11 @@ class TranslationsDialogFragment : BottomSheetDialogFragment() {
private val args by navArgs<TranslationsDialogFragmentArgs>()
private val browserStore: BrowserStore by lazy { requireComponents.core.store }
private val translationDialogBinding = ViewBoundFeatureWrapper<TranslationsDialogBinding>()
private val downloadLanguagesFeature =
ViewBoundFeatureWrapper<DownloadLanguagesFeature>()
private lateinit var translationsDialogStore: TranslationsDialogStore
private var isTranslationInProgress: Boolean? = null
private var isDataSaverEnabledAndWifiDisabled = false

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
super.onCreateDialog(savedInstanceState).apply {
Expand Down Expand Up @@ -192,9 +199,22 @@ class TranslationsDialogFragment : BottomSheetDialogFragment() {
view = view,
)
translationsDialogStore.dispatch(TranslationsDialogAction.InitTranslationsDialog)

downloadLanguagesFeature.set(
feature = DownloadLanguagesFeature(
context = requireContext(),
wifiConnectionMonitor = requireContext().components.wifiConnectionMonitor,
onDataSaverAndWifiChanged = {
isDataSaverEnabledAndWifiDisabled = it
},
),
owner = this,
view = view,
)
}

@Composable
@Suppress("LongMethod")
private fun TranslationsDialogContent(learnMoreUrl: String, onSettingClicked: () -> Unit) {
val translationsDialogState =
translationsDialogStore.observeAsComposableState { it }.value
Expand All @@ -206,6 +226,10 @@ class TranslationsDialogFragment : BottomSheetDialogFragment() {
dismissDialog()
}

var showDownloadLanguageFileDialog by remember {
mutableStateOf(false)
}

TranslationsDialog(
translationsDialogState = translationsDialogState,
learnMoreUrl = learnMoreUrl,
Expand All @@ -216,7 +240,15 @@ class TranslationsDialogFragment : BottomSheetDialogFragment() {
if (state.error is TranslationError.CouldNotLoadLanguagesError) {
translationsDialogStore.dispatch(TranslationsDialogAction.FetchSupportedLanguages)
} else {
translationsDialogStore.dispatch(TranslationsDialogAction.TranslateAction)
if (
isDataSaverEnabledAndWifiDisabled &&
!requireContext().settings().ignoreTranslationsDataSaverWarning &&
state.translationDownloadSize != null
) {
showDownloadLanguageFileDialog = true
} else {
translationsDialogStore.dispatch(TranslationsDialogAction.TranslateAction)
}
}
},
onNegativeButtonClicked = {
Expand All @@ -225,21 +257,58 @@ class TranslationsDialogFragment : BottomSheetDialogFragment() {
}
dismiss()
},
onFromSelected = {
onFromSelected = { fromLanguage ->
state.initialTo?.let {
translationsDialogStore.dispatch(
TranslationsDialogAction.FetchDownloadFileSizeAction(
toLanguage = it,
fromLanguage = fromLanguage,
),
)
}

translationsDialogStore.dispatch(
TranslationsDialogAction.UpdateFromSelectedLanguage(
it,
fromLanguage,
),
)
},
onToSelected = {
onToSelected = { toLanguage ->
state.initialFrom?.let {
translationsDialogStore.dispatch(
TranslationsDialogAction.FetchDownloadFileSizeAction(
toLanguage = toLanguage,
fromLanguage = it,
),
)
}

translationsDialogStore.dispatch(
TranslationsDialogAction.UpdateToSelectedLanguage(
it,
toLanguage,
),
)
},
)

var checkBoxEnabled by remember { mutableStateOf(false) }
if (showDownloadLanguageFileDialog) {
state.translationDownloadSize?.size?.let { fileSize ->
DownloadLanguageFileDialog(
downloadLanguageDialogType = DownloadLanguageFileDialogType.TranslationRequest,
fileSize = fileSize,
isCheckBoxEnabled = checkBoxEnabled,
onSavingModeStateChange = { checkBoxEnabled = it },
onConfirmDownload = {
requireContext().settings().ignoreTranslationsDataSaverWarning =
checkBoxEnabled
showDownloadLanguageFileDialog = false
translationsDialogStore.dispatch(TranslationsDialogAction.TranslateAction)
},
onCancel = { showDownloadLanguageFileDialog = false },
)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class TranslationsDialogMiddleware(
action: TranslationsDialogAction,
) {
when (action) {
is TranslationsDialogAction.FetchDownloadFileSizeAction -> {
browserStore.dispatch(
TranslationsAction.FetchTranslationDownloadSizeAction(
tabId = sessionId,
fromLanguage = action.fromLanguage,
toLanguage = action.toLanguage,
),
)
}

is TranslationsDialogAction.FetchSupportedLanguages -> {
browserStore.dispatch(
TranslationsAction.OperationRequestedAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package org.mozilla.fenix.translations
import mozilla.components.browser.state.action.TranslationsAction
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.translate.Language
import mozilla.components.concept.engine.translate.TranslationDownloadSize
import mozilla.components.concept.engine.translate.TranslationError
import mozilla.components.lib.state.Action
import mozilla.components.lib.state.Middleware
Expand Down Expand Up @@ -33,6 +34,8 @@ class TranslationsDialogStore(
* @property isTranslated The page is currently translated.
* @property isTranslationInProgress The page is currently attempting a translation.
* @property positiveButtonType Can be enabled,disabled or in progress.
* @property translationDownloadSize A data class to contain information
* related to the download size required for a given translation to/from pair.
* @property error An error that can occur during the translation process.
* @property dismissDialogState Whether the dialog bottom sheet should be dismissed.
* @property initialFrom Initial "from" language, based on the translation state and page state.
Expand All @@ -45,6 +48,7 @@ data class TranslationsDialogState(
var isTranslated: Boolean = false,
val isTranslationInProgress: Boolean = false,
val positiveButtonType: PositiveButtonType? = null,
val translationDownloadSize: TranslationDownloadSize? = null,
val error: TranslationError? = null,
val dismissDialogState: DismissDialogState? = null,
val initialFrom: Language? = null,
Expand Down Expand Up @@ -139,6 +143,18 @@ sealed class TranslationsDialogAction : Action {
* Updates the dialog title if the page was translated.
*/
data class UpdateTranslatedPageTitle(val title: String) : TranslationsDialogAction()

/**
* Updates the translation download file size.
*/
data class UpdateDownloadTranslationDownloadSize(val translationDownloadSize: TranslationDownloadSize? = null) :
TranslationsDialogAction()

/**
* Fetch the translation download file size.
*/
data class FetchDownloadFileSizeAction(val toLanguage: Language, val fromLanguage: Language) :
TranslationsDialogAction()
}

/**
Expand Down Expand Up @@ -248,9 +264,7 @@ internal object TranslationsDialogReducer {
is TranslationsDialogAction.UpdateTranslationError -> {
state.copy(
error = action.translationError,
positiveButtonType = if (
action.translationError is TranslationError.LanguageNotSupportedError
) {
positiveButtonType = if (action.translationError is TranslationError.LanguageNotSupportedError) {
PositiveButtonType.Disabled
} else {
PositiveButtonType.Enabled
Expand All @@ -269,13 +283,32 @@ internal object TranslationsDialogReducer {
state.copy(translatedPageTitle = action.title)
}

is TranslationsDialogAction.UpdateDownloadTranslationDownloadSize -> {
state.copy(
translationDownloadSize = if (
action.translationDownloadSize?.fromLanguage == state.initialFrom &&
action.translationDownloadSize?.toLanguage == state.initialTo &&
isTranslationDownloadSizeValid(action.translationDownloadSize)
) {
action.translationDownloadSize
} else {
null
},
)
}

is TranslationsDialogAction.TranslateAction,
TranslationsDialogAction.FetchSupportedLanguages,
TranslationsDialogAction.RestoreTranslation,
is TranslationsDialogAction.FetchDownloadFileSizeAction,
-> {
// handled by [TranslationsDialogMiddleware]
state
}
}
}

private fun isTranslationDownloadSizeValid(translationDownloadSize: TranslationDownloadSize?) =
translationDownloadSize?.size != 0L &&
translationDownloadSize?.error == null
}
Loading

0 comments on commit 1304472

Please sign in to comment.