diff --git a/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/PromptFeature.kt b/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/PromptFeature.kt index 39870bbea97..5ac032e113e 100644 --- a/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/PromptFeature.kt +++ b/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/PromptFeature.kt @@ -43,13 +43,12 @@ import mozilla.components.concept.engine.prompt.PromptRequest.TextPrompt import mozilla.components.concept.engine.prompt.PromptRequest.TimeSelection import mozilla.components.concept.storage.CreditCardEntry import mozilla.components.concept.storage.CreditCardValidationDelegate -import mozilla.components.concept.storage.Login import mozilla.components.concept.storage.LoginEntry import mozilla.components.concept.storage.LoginValidationDelegate import mozilla.components.feature.prompts.address.AddressDelegate import mozilla.components.feature.prompts.address.AddressPicker import mozilla.components.feature.prompts.address.DefaultAddressDelegate -import mozilla.components.feature.prompts.concept.SelectablePromptView +import mozilla.components.feature.prompts.creditcard.CreditCardDelegate import mozilla.components.feature.prompts.creditcard.CreditCardPicker import mozilla.components.feature.prompts.creditcard.CreditCardSaveDialogFragment import mozilla.components.feature.prompts.dialog.AlertDialogFragment @@ -70,6 +69,7 @@ import mozilla.components.feature.prompts.dialog.TimePickerDialogFragment import mozilla.components.feature.prompts.facts.emitSuccessfulAddressAutofillFormDetectedFact import mozilla.components.feature.prompts.facts.emitSuccessfulCreditCardAutofillFormDetectedFact import mozilla.components.feature.prompts.file.FilePicker +import mozilla.components.feature.prompts.login.LoginDelegate import mozilla.components.feature.prompts.login.LoginExceptions import mozilla.components.feature.prompts.login.LoginPicker import mozilla.components.feature.prompts.share.DefaultShareDelegate @@ -126,16 +126,8 @@ internal const val FRAGMENT_TAG = "mozac_feature_prompt_dialog" * will be shown. * @property loginExceptionStorage An implementation of [LoginExceptions] that saves and checks origins * the user does not want to see a save login dialog for. - * @property loginPickerView The [SelectablePromptView] used for [LoginPicker] to display a - * selectable prompt list of login options. - * @property onManageLogins A callback invoked when a user selects "manage logins" from the - * select login prompt. - * @property creditCardPickerView The [SelectablePromptView] used for [CreditCardPicker] to display - * a selectable prompt list of credit card options. - * @property onManageCreditCards A callback invoked when a user selects "Manage credit cards" from - * the select credit card prompt. - * @property onSelectCreditCard A callback invoked when a user selects a credit card from the - * select credit card prompt. + * @property loginDelegate Delegate for login picker. + * @property creditCardDelegate Delegate for credit card picker. * @property addressDelegate Delegate for address picker. * @property onNeedToRequestPermissions A callback invoked when permissions * need to be requested before a prompt (e.g. a file picker) can be displayed. @@ -154,11 +146,8 @@ class PromptFeature private constructor( private val isCreditCardAutofillEnabled: () -> Boolean = { false }, private val isAddressAutofillEnabled: () -> Boolean = { false }, override val loginExceptionStorage: LoginExceptions? = null, - private val loginPickerView: SelectablePromptView? = null, - private val onManageLogins: () -> Unit = {}, - private val creditCardPickerView: SelectablePromptView? = null, - private val onManageCreditCards: () -> Unit = {}, - private val onSelectCreditCard: () -> Unit = {}, + private val loginDelegate: LoginDelegate = object : LoginDelegate {}, + private val creditCardDelegate: CreditCardDelegate = object : CreditCardDelegate {}, private val addressDelegate: AddressDelegate = DefaultAddressDelegate(), onNeedToRequestPermissions: OnNeedToRequestPermissions ) : LifecycleAwareFeature, @@ -195,11 +184,8 @@ class PromptFeature private constructor( isCreditCardAutofillEnabled: () -> Boolean = { false }, isAddressAutofillEnabled: () -> Boolean = { false }, loginExceptionStorage: LoginExceptions? = null, - loginPickerView: SelectablePromptView? = null, - onManageLogins: () -> Unit = {}, - creditCardPickerView: SelectablePromptView? = null, - onManageCreditCards: () -> Unit = {}, - onSelectCreditCard: () -> Unit = {}, + loginDelegate: LoginDelegate = object : LoginDelegate {}, + creditCardDelegate: CreditCardDelegate = object : CreditCardDelegate {}, addressDelegate: AddressDelegate = DefaultAddressDelegate(), onNeedToRequestPermissions: OnNeedToRequestPermissions ) : this( @@ -215,11 +201,8 @@ class PromptFeature private constructor( isAddressAutofillEnabled = isAddressAutofillEnabled, loginExceptionStorage = loginExceptionStorage, onNeedToRequestPermissions = onNeedToRequestPermissions, - loginPickerView = loginPickerView, - onManageLogins = onManageLogins, - creditCardPickerView = creditCardPickerView, - onManageCreditCards = onManageCreditCards, - onSelectCreditCard = onSelectCreditCard, + loginDelegate = loginDelegate, + creditCardDelegate = creditCardDelegate, addressDelegate = addressDelegate ) @@ -235,11 +218,8 @@ class PromptFeature private constructor( isCreditCardAutofillEnabled: () -> Boolean = { false }, isAddressAutofillEnabled: () -> Boolean = { false }, loginExceptionStorage: LoginExceptions? = null, - loginPickerView: SelectablePromptView? = null, - onManageLogins: () -> Unit = {}, - creditCardPickerView: SelectablePromptView? = null, - onManageCreditCards: () -> Unit = {}, - onSelectCreditCard: () -> Unit = {}, + loginDelegate: LoginDelegate = object : LoginDelegate {}, + creditCardDelegate: CreditCardDelegate = object : CreditCardDelegate {}, addressDelegate: AddressDelegate = DefaultAddressDelegate(), onNeedToRequestPermissions: OnNeedToRequestPermissions ) : this( @@ -255,11 +235,8 @@ class PromptFeature private constructor( isAddressAutofillEnabled = isAddressAutofillEnabled, loginExceptionStorage = loginExceptionStorage, onNeedToRequestPermissions = onNeedToRequestPermissions, - loginPickerView = loginPickerView, - onManageLogins = onManageLogins, - creditCardPickerView = creditCardPickerView, - onManageCreditCards = onManageCreditCards, - onSelectCreditCard = onSelectCreditCard, + loginDelegate = loginDelegate, + creditCardDelegate = creditCardDelegate, addressDelegate = addressDelegate ) @@ -267,18 +244,24 @@ class PromptFeature private constructor( @VisibleForTesting(otherwise = PRIVATE) internal var loginPicker = - loginPickerView?.let { LoginPicker(store, it, onManageLogins, customTabId) } + with(loginDelegate) { + loginPickerView?.let { + LoginPicker(store, it, onManageLogins, customTabId) + } + } @VisibleForTesting(otherwise = PRIVATE) internal var creditCardPicker = - creditCardPickerView?.let { - CreditCardPicker( - store = store, - creditCardSelectBar = it, - manageCreditCardsCallback = onManageCreditCards, - selectCreditCardCallback = onSelectCreditCard, - sessionId = customTabId - ) + with(creditCardDelegate) { + creditCardPickerView?.let { + CreditCardPicker( + store = store, + creditCardSelectBar = it, + manageCreditCardsCallback = onManageCreditCards, + selectCreditCardCallback = onSelectCreditCard, + sessionId = customTabId + ) + } } @VisibleForTesting(otherwise = PRIVATE) @@ -922,7 +905,7 @@ class PromptFeature private constructor( (activePromptRequest as? SelectLoginPrompt)?.let { selectLoginPrompt -> loginPicker?.let { loginPicker -> - if (loginPickerView?.asView()?.isVisible == true) { + if (loginDelegate.loginPickerView?.asView()?.isVisible == true) { loginPicker.dismissCurrentLoginSelect(selectLoginPrompt) result = true } @@ -931,7 +914,7 @@ class PromptFeature private constructor( (activePromptRequest as? SelectCreditCard)?.let { selectCreditCardPrompt -> creditCardPicker?.let { creditCardPicker -> - if (creditCardPickerView?.asView()?.isVisible == true) { + if (creditCardDelegate.creditCardPickerView?.asView()?.isVisible == true) { creditCardPicker.dismissSelectCreditCardRequest(selectCreditCardPrompt) result = true } diff --git a/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/creditcard/CreditCardDelegate.kt b/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/creditcard/CreditCardDelegate.kt new file mode 100644 index 00000000000..9723d0d521a --- /dev/null +++ b/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/creditcard/CreditCardDelegate.kt @@ -0,0 +1,34 @@ +/* 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 mozilla.components.feature.prompts.creditcard + +import mozilla.components.concept.storage.CreditCardEntry +import mozilla.components.feature.prompts.concept.SelectablePromptView + +/** + * Delegate for credit card picker and related callbacks + */ +interface CreditCardDelegate { + /** + * The [SelectablePromptView] used for [CreditCardPicker] to display a + * selectable prompt list of credit cards. + */ + val creditCardPickerView: SelectablePromptView? + get() = null + + /** + * Callback invoked when a user selects "Manage credit cards" + * from the select credit card prompt. + */ + val onManageCreditCards: () -> Unit + get() = {} + + /** + * Callback invoked when a user selects a credit card option + * from the select credit card prompt + */ + val onSelectCreditCard: () -> Unit + get() = {} +} diff --git a/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/login/LoginDelegate.kt b/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/login/LoginDelegate.kt new file mode 100644 index 00000000000..80f4d332972 --- /dev/null +++ b/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/login/LoginDelegate.kt @@ -0,0 +1,27 @@ +/* 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 mozilla.components.feature.prompts.login + +import mozilla.components.concept.storage.Login +import mozilla.components.feature.prompts.concept.SelectablePromptView + +/** + * Delegate to display the login select prompt and related callbacks + */ +interface LoginDelegate { + /** + * The [SelectablePromptView] used for [LoginPicker] to display a + * selectable prompt list of logins. + */ + val loginPickerView: SelectablePromptView? + get() = null + + /** + * Callback invoked when a user selects "Manage logins" + * from the select login prompt. + */ + val onManageLogins: () -> Unit + get() = {} +} diff --git a/components/feature/prompts/src/test/java/mozilla/components/feature/prompts/PromptFeatureTest.kt b/components/feature/prompts/src/test/java/mozilla/components/feature/prompts/PromptFeatureTest.kt index 094e93eb286..98b18cb4931 100644 --- a/components/feature/prompts/src/test/java/mozilla/components/feature/prompts/PromptFeatureTest.kt +++ b/components/feature/prompts/src/test/java/mozilla/components/feature/prompts/PromptFeatureTest.kt @@ -45,6 +45,7 @@ import mozilla.components.concept.storage.LoginEntry import mozilla.components.feature.prompts.address.AddressDelegate import mozilla.components.feature.prompts.address.AddressPicker import mozilla.components.feature.prompts.concept.SelectablePromptView +import mozilla.components.feature.prompts.creditcard.CreditCardDelegate import mozilla.components.feature.prompts.creditcard.CreditCardPicker import mozilla.components.feature.prompts.creditcard.CreditCardSaveDialogFragment import mozilla.components.feature.prompts.dialog.ChoiceDialogFragment @@ -53,6 +54,7 @@ import mozilla.components.feature.prompts.dialog.MultiButtonDialogFragment import mozilla.components.feature.prompts.dialog.PromptDialogFragment import mozilla.components.feature.prompts.dialog.SaveLoginDialogFragment import mozilla.components.feature.prompts.file.FilePicker.Companion.FILE_PICKER_ACTIVITY_REQUEST_CODE +import mozilla.components.feature.prompts.login.LoginDelegate import mozilla.components.feature.prompts.login.LoginPicker import mozilla.components.feature.prompts.share.ShareDelegate import mozilla.components.support.test.any @@ -277,7 +279,10 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - loginPickerView = loginPickerView + loginDelegate = object : LoginDelegate { + override val loginPickerView = loginPickerView + override val onManageLogins = {} + } ) { } ) val selectLoginPrompt = mock() @@ -430,7 +435,10 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - loginPickerView = loginPickerView + loginDelegate = object : LoginDelegate { + override val loginPickerView = loginPickerView + override val onManageLogins = {} + } ) { } ) val selectLoginPrompt = mock() @@ -455,7 +463,10 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - loginPickerView = loginPickerView + loginDelegate = object : LoginDelegate { + override val loginPickerView = loginPickerView + override val onManageLogins = {} + } ) { } ) val selectLoginPrompt = mock() @@ -480,7 +491,10 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - loginPickerView = loginPickerView + loginDelegate = object : LoginDelegate { + override val loginPickerView = loginPickerView + override val onManageLogins = {} + } ) { } ) val selectLoginPrompt = mock() @@ -506,7 +520,11 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + } ) { } ) val selectCreditCardRequest = mock() @@ -530,7 +548,11 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + } ) { } ) val selectCreditCardRequest = mock() @@ -553,7 +575,11 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + } ) { } ) val selectCreditCardRequest = mock() @@ -577,7 +603,11 @@ class PromptFeatureTest { mock(), store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + } ) { } ) feature.creditCardPicker = creditCardPicker @@ -971,7 +1001,11 @@ class PromptFeatureTest { activity = mock(), store = store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView, + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + }, isCreditCardAutofillEnabled = { true } ) { } feature.creditCardPicker = creditCardPicker @@ -990,7 +1024,11 @@ class PromptFeatureTest { activity = mock(), store = store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView, + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + }, isCreditCardAutofillEnabled = { true } ) { } feature.creditCardPicker = creditCardPicker @@ -1009,7 +1047,11 @@ class PromptFeatureTest { activity = mock(), store = store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView, + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + }, isCreditCardAutofillEnabled = { true } ) { } feature.creditCardPicker = creditCardPicker @@ -1027,7 +1069,11 @@ class PromptFeatureTest { activity = mock(), store = store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView, + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + }, isCreditCardAutofillEnabled = { true } ) { } feature.creditCardPicker = creditCardPicker @@ -1514,7 +1560,10 @@ class PromptFeatureTest { val feature = PromptFeature( activity = mock(), store = store, fragmentManager = fragmentManager, - loginPickerView = loginPickerView + loginDelegate = object : LoginDelegate { + override val loginPickerView = loginPickerView + override val onManageLogins = {} + } ) { } feature.loginPicker = loginPicker val onLoginDismiss: () -> Unit = {} @@ -1547,7 +1596,11 @@ class PromptFeatureTest { activity = mock(), store = store, fragmentManager = fragmentManager, - creditCardPickerView = creditCardPickerView, + creditCardDelegate = object : CreditCardDelegate { + override val creditCardPickerView = creditCardPickerView + override val onSelectCreditCard = {} + override val onManageCreditCards = {} + }, isCreditCardAutofillEnabled = { true } ) { } feature.creditCardPicker = creditCardPicker diff --git a/docs/changelog.md b/docs/changelog.md index ef267639aa5..630aa780da4 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -25,6 +25,7 @@ permalink: /changelog/ * **feature-prompts**: * Added optional `addressPickerView` and `onManageAddresses` parameters through `AddressDelegate` to `PromptFeature` for a new `AddressPicker` to display a view for selecting addresses to autofill into a site. [#12061](https://github.com/mozilla-mobile/android-components/issues/12061) + * ⚠️ **This is a breaking change**: `PromptFeature` constructor now takes `LoginDelegate` and `CreditCardDelegate` instead of login picker and credit card picker parameters. [#12257](https://github.com/mozilla-mobile/android-components/issues/12257) * **service-glean** * 🆙 Updated Glean to version 50.0.1 ([changelog](https://github.com/mozilla/glean/releases/tag/v50.0.1))