[fenix] For https://github.com/mozilla-mobile/fenix/issues/24847 - Part 1: Refactor CreditCardsFragmentStore to AutofillFragmentStore

pull/600/head
Gabriel Luong 2 years ago committed by mergify[bot]
parent 49fb546ec3
commit 8ab524c2eb

@ -36,9 +36,9 @@ import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.settings.SharedPreferenceUpdater import org.mozilla.fenix.settings.SharedPreferenceUpdater
import org.mozilla.fenix.settings.SyncPreferenceView import org.mozilla.fenix.settings.SyncPreferenceView
import org.mozilla.fenix.settings.biometric.BiometricPromptPreferenceFragment import org.mozilla.fenix.settings.biometric.BiometricPromptPreferenceFragment
import org.mozilla.fenix.settings.creditcards.CreditCardsAction import org.mozilla.fenix.settings.creditcards.AutofillAction
import org.mozilla.fenix.settings.creditcards.CreditCardsFragmentStore import org.mozilla.fenix.settings.creditcards.AutofillFragmentStore
import org.mozilla.fenix.settings.creditcards.CreditCardsListState import org.mozilla.fenix.settings.creditcards.AutofillFragmentState
import org.mozilla.fenix.settings.requirePreference import org.mozilla.fenix.settings.requirePreference
/** /**
@ -48,7 +48,7 @@ import org.mozilla.fenix.settings.requirePreference
@SuppressWarnings("TooManyFunctions") @SuppressWarnings("TooManyFunctions")
class AutofillSettingFragment : BiometricPromptPreferenceFragment() { class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
private lateinit var creditCardsStore: CreditCardsFragmentStore private lateinit var store: AutofillFragmentStore
private var isCreditCardsListLoaded: Boolean = false private var isCreditCardsListLoaded: Boolean = false
/** /**
@ -75,8 +75,8 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
creditCardsStore = StoreProvider.get(this) { store = StoreProvider.get(this) {
CreditCardsFragmentStore(CreditCardsListState(creditCards = emptyList())) AutofillFragmentStore(AutofillFragmentState(creditCards = emptyList()))
} }
loadCreditCards() loadCreditCards()
} }
@ -108,7 +108,7 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
consumeFrom(creditCardsStore) { state -> consumeFrom(store) { state ->
updateCardManagementPreference(state.creditCards.isNotEmpty(), findNavController()) updateCardManagementPreference(state.creditCards.isNotEmpty(), findNavController())
} }
@ -188,7 +188,7 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
} }
/** /**
* Fetches all the credit cards from autofillStorage and updates the [CreditCardsListState] * Fetches all the credit cards from autofillStorage and updates the [AutofillFragmentState]
* with the list of credit cards. * with the list of credit cards.
*/ */
private fun loadCreditCards() { private fun loadCreditCards() {
@ -199,7 +199,7 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
val creditCards = requireComponents.core.autofillStorage.getAllCreditCards() val creditCards = requireComponents.core.autofillStorage.getAllCreditCards()
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
creditCardsStore.dispatch(CreditCardsAction.UpdateCreditCards(creditCards)) store.dispatch(AutofillAction.UpdateCreditCards(creditCards))
} }
} }

@ -10,11 +10,11 @@ import mozilla.components.lib.state.State
import mozilla.components.lib.state.Store import mozilla.components.lib.state.Store
/** /**
* The [Store] for holding the [CreditCardsListState] and applying [CreditCardsAction]s. * The [Store] for holding the [AutofillFragmentState] and applying [AutofillAction]s.
*/ */
class CreditCardsFragmentStore(initialState: CreditCardsListState) : class AutofillFragmentStore(initialState: AutofillFragmentState) :
Store<CreditCardsListState, CreditCardsAction>( Store<AutofillFragmentState, AutofillAction>(
initialState, ::creditCardsFragmentStateReducer initialState, ::autofillFragmentStateReducer
) )
/** /**
@ -24,37 +24,37 @@ class CreditCardsFragmentStore(initialState: CreditCardsListState) :
* @property isLoading True if the credit cards are still being loaded from storage, * @property isLoading True if the credit cards are still being loaded from storage,
* otherwise false. * otherwise false.
*/ */
data class CreditCardsListState( data class AutofillFragmentState(
val creditCards: List<CreditCard>, val creditCards: List<CreditCard>,
val isLoading: Boolean = true val isLoading: Boolean = true
) : State ) : State
/** /**
* Actions to dispatch through the [CreditCardsFragmentStore] to modify the [CreditCardsListState] * Actions to dispatch through the [AutofillFragmentStore] to modify the [AutofillFragmentState]
* through the [creditCardsFragmentStateReducer]. * through the [autofillFragmentStateReducer].
*/ */
sealed class CreditCardsAction : Action { sealed class AutofillAction : Action {
/** /**
* Updates the list of credit cards with the provided [creditCards]. * Updates the list of credit cards with the provided [creditCards].
* *
* @param creditCards The list of [CreditCard]s to display in the credit card list. * @param creditCards The list of [CreditCard]s to display in the credit card list.
*/ */
data class UpdateCreditCards(val creditCards: List<CreditCard>) : CreditCardsAction() data class UpdateCreditCards(val creditCards: List<CreditCard>) : AutofillAction()
} }
/** /**
* Reduces the credit cards state from the current state with the provided [action] to be performed. * Reduces the autofill state from the current state with the provided [action] to be performed.
* *
* @param state The current credit cards state. * @param state The current autofill state.
* @param action The action to be performed on the state. * @param action The action to be performed on the state.
* @return the new [CreditCardsListState] with the [action] executed. * @return the new [AutofillFragmentState] with the [action] executed.
*/ */
private fun creditCardsFragmentStateReducer( private fun autofillFragmentStateReducer(
state: CreditCardsListState, state: AutofillFragmentState,
action: CreditCardsAction action: AutofillAction
): CreditCardsListState { ): AutofillFragmentState {
return when (action) { return when (action) {
is CreditCardsAction.UpdateCreditCards -> { is AutofillAction.UpdateCreditCards -> {
state.copy( state.copy(
creditCards = action.creditCards, creditCards = action.creditCards,
isLoading = false isLoading = false

@ -30,7 +30,7 @@ import org.mozilla.fenix.settings.creditcards.view.CreditCardsManagementView
*/ */
class CreditCardsManagementFragment : SecureFragment() { class CreditCardsManagementFragment : SecureFragment() {
private lateinit var creditCardsStore: CreditCardsFragmentStore private lateinit var store: AutofillFragmentStore
private lateinit var interactor: CreditCardsManagementInteractor private lateinit var interactor: CreditCardsManagementInteractor
private lateinit var creditCardsView: CreditCardsManagementView private lateinit var creditCardsView: CreditCardsManagementView
@ -41,8 +41,8 @@ class CreditCardsManagementFragment : SecureFragment() {
): View? { ): View? {
val view = inflater.inflate(CreditCardsManagementView.LAYOUT_ID, container, false) val view = inflater.inflate(CreditCardsManagementView.LAYOUT_ID, container, false)
creditCardsStore = StoreProvider.get(this) { store = StoreProvider.get(this) {
CreditCardsFragmentStore(CreditCardsListState(creditCards = emptyList())) AutofillFragmentStore(AutofillFragmentState(creditCards = emptyList()))
} }
interactor = DefaultCreditCardsManagementInteractor( interactor = DefaultCreditCardsManagementInteractor(
@ -60,7 +60,7 @@ class CreditCardsManagementFragment : SecureFragment() {
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
consumeFrom(creditCardsStore) { state -> consumeFrom(store) { state ->
if (!state.isLoading && state.creditCards.isEmpty()) { if (!state.isLoading && state.creditCards.isEmpty()) {
findNavController().popBackStack() findNavController().popBackStack()
return@consumeFrom return@consumeFrom
@ -91,14 +91,14 @@ class CreditCardsManagementFragment : SecureFragment() {
/** /**
* Fetches all the credit cards from the autofill storage and updates the * Fetches all the credit cards from the autofill storage and updates the
* [CreditCardsFragmentStore] with the list of credit cards. * [AutofillFragmentStore] with the list of credit cards.
*/ */
private fun loadCreditCards() { private fun loadCreditCards() {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
val creditCards = requireContext().components.core.autofillStorage.getAllCreditCards() val creditCards = requireContext().components.core.autofillStorage.getAllCreditCards()
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
creditCardsStore.dispatch(CreditCardsAction.UpdateCreditCards(creditCards)) store.dispatch(AutofillAction.UpdateCreditCards(creditCards))
} }
} }
} }

@ -8,7 +8,7 @@ import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.ComponentCreditCardsBinding import org.mozilla.fenix.databinding.ComponentCreditCardsBinding
import org.mozilla.fenix.settings.creditcards.CreditCardsListState import org.mozilla.fenix.settings.creditcards.AutofillFragmentState
import org.mozilla.fenix.settings.creditcards.interactor.CreditCardsManagementInteractor import org.mozilla.fenix.settings.creditcards.interactor.CreditCardsManagementInteractor
/** /**
@ -31,9 +31,9 @@ class CreditCardsManagementView(
} }
/** /**
* Updates the display of the credit cards based on the given [CreditCardsListState]. * Updates the display of the credit cards based on the given [AutofillFragmentState].
*/ */
fun update(state: CreditCardsListState) { fun update(state: AutofillFragmentState) {
binding.progressBar.isVisible = state.isLoading binding.progressBar.isVisible = state.isLoading
binding.creditCardsList.isVisible = state.creditCards.isNotEmpty() binding.creditCardsList.isVisible = state.creditCards.isNotEmpty()

@ -23,8 +23,8 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.settings.creditcards.CreditCardsFragmentStore import org.mozilla.fenix.settings.creditcards.AutofillFragmentStore
import org.mozilla.fenix.settings.creditcards.CreditCardsListState import org.mozilla.fenix.settings.creditcards.AutofillFragmentState
import org.robolectric.Robolectric import org.robolectric.Robolectric
@RunWith(FenixRobolectricTestRunner::class) @RunWith(FenixRobolectricTestRunner::class)
@ -58,11 +58,11 @@ class AutofillSettingFragmentTest {
val creditCards: List<CreditCard> = listOf(mockk(), mockk()) val creditCards: List<CreditCard> = listOf(mockk(), mockk())
val creditCardsState = CreditCardsListState(creditCards = creditCards) val state = AutofillFragmentState(creditCards = creditCards)
val creditCardsStore = CreditCardsFragmentStore(creditCardsState) val store = AutofillFragmentStore(state)
autofillSettingFragment.updateCardManagementPreference( autofillSettingFragment.updateCardManagementPreference(
creditCardsStore.state.creditCards.isNotEmpty(), store.state.creditCards.isNotEmpty(),
navController navController
) )
@ -82,11 +82,11 @@ class AutofillSettingFragmentTest {
AutofillSettingFragmentDirections AutofillSettingFragmentDirections
.actionAutofillSettingFragmentToCreditCardEditorFragment() .actionAutofillSettingFragmentToCreditCardEditorFragment()
val creditCardsState = CreditCardsListState(creditCards = emptyList()) val state = AutofillFragmentState(creditCards = emptyList())
val creditCardsStore = CreditCardsFragmentStore(creditCardsState) val store = AutofillFragmentStore(state)
autofillSettingFragment.updateCardManagementPreference( autofillSettingFragment.updateCardManagementPreference(
creditCardsStore.state.creditCards.isNotEmpty(), store.state.creditCards.isNotEmpty(),
navController navController
) )

@ -13,25 +13,25 @@ import org.junit.Assert.assertTrue
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
class CreditCardsFragmentStoreTest { class AutofillFragmentStoreTest {
private lateinit var creditCardsState: CreditCardsListState private lateinit var state: AutofillFragmentState
private lateinit var creditCardsStore: CreditCardsFragmentStore private lateinit var store: AutofillFragmentStore
@Before @Before
fun setup() { fun setup() {
creditCardsState = CreditCardsListState(creditCards = emptyList()) state = AutofillFragmentState(creditCards = emptyList())
creditCardsStore = CreditCardsFragmentStore(creditCardsState) store = AutofillFragmentStore(state)
} }
@Test @Test
fun testUpdateCreditCards() = runBlocking { fun testUpdateCreditCards() = runBlocking {
assertTrue(creditCardsStore.state.isLoading) assertTrue(store.state.isLoading)
val creditCards: List<CreditCard> = listOf(mockk(), mockk()) val creditCards: List<CreditCard> = listOf(mockk(), mockk())
creditCardsStore.dispatch(CreditCardsAction.UpdateCreditCards(creditCards)).join() store.dispatch(AutofillAction.UpdateCreditCards(creditCards)).join()
assertEquals(creditCards, creditCardsStore.state.creditCards) assertEquals(creditCards, store.state.creditCards)
assertFalse(creditCardsStore.state.isLoading) assertFalse(store.state.isLoading)
} }
} }

@ -41,14 +41,14 @@ class CreditCardsManagementViewTest {
@Test @Test
fun testUpdate() { fun testUpdate() {
creditCardsView.update(CreditCardsListState(creditCards = emptyList())) creditCardsView.update(AutofillFragmentState(creditCards = emptyList()))
assertTrue(componentCreditCardsBinding.progressBar.isVisible) assertTrue(componentCreditCardsBinding.progressBar.isVisible)
assertFalse(componentCreditCardsBinding.creditCardsList.isVisible) assertFalse(componentCreditCardsBinding.creditCardsList.isVisible)
val creditCards: List<CreditCard> = listOf(mockk(), mockk()) val creditCards: List<CreditCard> = listOf(mockk(), mockk())
creditCardsView.update( creditCardsView.update(
CreditCardsListState( AutofillFragmentState(
creditCards = creditCards, creditCards = creditCards,
isLoading = false isLoading = false
) )

Loading…
Cancel
Save