For #24847 - Part 1: Refactor CreditCardsFragmentStore to AutofillFragmentStore

This commit is contained in:
Gabriel Luong 2022-04-15 19:27:26 -04:00 committed by mergify[bot]
parent 644c4cec5c
commit 22056752e4
7 changed files with 54 additions and 54 deletions

View File

@ -36,9 +36,9 @@ import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.settings.SharedPreferenceUpdater
import org.mozilla.fenix.settings.SyncPreferenceView
import org.mozilla.fenix.settings.biometric.BiometricPromptPreferenceFragment
import org.mozilla.fenix.settings.creditcards.CreditCardsAction
import org.mozilla.fenix.settings.creditcards.CreditCardsFragmentStore
import org.mozilla.fenix.settings.creditcards.CreditCardsListState
import org.mozilla.fenix.settings.creditcards.AutofillAction
import org.mozilla.fenix.settings.creditcards.AutofillFragmentStore
import org.mozilla.fenix.settings.creditcards.AutofillFragmentState
import org.mozilla.fenix.settings.requirePreference
/**
@ -48,7 +48,7 @@ import org.mozilla.fenix.settings.requirePreference
@SuppressWarnings("TooManyFunctions")
class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
private lateinit var creditCardsStore: CreditCardsFragmentStore
private lateinit var store: AutofillFragmentStore
private var isCreditCardsListLoaded: Boolean = false
/**
@ -75,8 +75,8 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
creditCardsStore = StoreProvider.get(this) {
CreditCardsFragmentStore(CreditCardsListState(creditCards = emptyList()))
store = StoreProvider.get(this) {
AutofillFragmentStore(AutofillFragmentState(creditCards = emptyList()))
}
loadCreditCards()
}
@ -108,7 +108,7 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
consumeFrom(creditCardsStore) { state ->
consumeFrom(store) { state ->
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.
*/
private fun loadCreditCards() {
@ -199,7 +199,7 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
lifecycleScope.launch(Dispatchers.IO) {
val creditCards = requireComponents.core.autofillStorage.getAllCreditCards()
lifecycleScope.launch(Dispatchers.Main) {
creditCardsStore.dispatch(CreditCardsAction.UpdateCreditCards(creditCards))
store.dispatch(AutofillAction.UpdateCreditCards(creditCards))
}
}

View File

@ -10,11 +10,11 @@ import mozilla.components.lib.state.State
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) :
Store<CreditCardsListState, CreditCardsAction>(
initialState, ::creditCardsFragmentStateReducer
class AutofillFragmentStore(initialState: AutofillFragmentState) :
Store<AutofillFragmentState, AutofillAction>(
initialState, ::autofillFragmentStateReducer
)
/**
@ -24,37 +24,37 @@ class CreditCardsFragmentStore(initialState: CreditCardsListState) :
* @property isLoading True if the credit cards are still being loaded from storage,
* otherwise false.
*/
data class CreditCardsListState(
data class AutofillFragmentState(
val creditCards: List<CreditCard>,
val isLoading: Boolean = true
) : State
/**
* Actions to dispatch through the [CreditCardsFragmentStore] to modify the [CreditCardsListState]
* through the [creditCardsFragmentStateReducer].
* Actions to dispatch through the [AutofillFragmentStore] to modify the [AutofillFragmentState]
* through the [autofillFragmentStateReducer].
*/
sealed class CreditCardsAction : Action {
sealed class AutofillAction : Action {
/**
* Updates the list of credit cards with the provided [creditCards].
*
* @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.
* @return the new [CreditCardsListState] with the [action] executed.
* @return the new [AutofillFragmentState] with the [action] executed.
*/
private fun creditCardsFragmentStateReducer(
state: CreditCardsListState,
action: CreditCardsAction
): CreditCardsListState {
private fun autofillFragmentStateReducer(
state: AutofillFragmentState,
action: AutofillAction
): AutofillFragmentState {
return when (action) {
is CreditCardsAction.UpdateCreditCards -> {
is AutofillAction.UpdateCreditCards -> {
state.copy(
creditCards = action.creditCards,
isLoading = false

View File

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

View File

@ -8,7 +8,7 @@ import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import org.mozilla.fenix.R
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
/**
@ -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.creditCardsList.isVisible = state.creditCards.isNotEmpty()

View File

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

View File

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

View File

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