1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/* 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.settings.creditcards
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.lib.state.ext.consumeFrom
import org.mozilla.fenix.R
import org.mozilla.fenix.SecureFragment
import org.mozilla.fenix.components.StoreProvider
import org.mozilla.fenix.databinding.ComponentCreditCardsBinding
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.redirectToReAuth
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.settings.autofill.AutofillAction
import org.mozilla.fenix.settings.autofill.AutofillFragmentState
import org.mozilla.fenix.settings.autofill.AutofillFragmentStore
import org.mozilla.fenix.settings.creditcards.controller.DefaultCreditCardsManagementController
import org.mozilla.fenix.settings.creditcards.interactor.CreditCardsManagementInteractor
import org.mozilla.fenix.settings.creditcards.interactor.DefaultCreditCardsManagementInteractor
import org.mozilla.fenix.settings.creditcards.view.CreditCardsManagementView
/**
* Displays a list of saved credit cards.
*/
class CreditCardsManagementFragment : SecureFragment() {
private lateinit var store: AutofillFragmentStore
private lateinit var interactor: CreditCardsManagementInteractor
private lateinit var creditCardsView: CreditCardsManagementView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view = inflater.inflate(CreditCardsManagementView.LAYOUT_ID, container, false)
store = StoreProvider.get(this) {
AutofillFragmentStore(AutofillFragmentState())
}
interactor = DefaultCreditCardsManagementInteractor(
controller = DefaultCreditCardsManagementController(
navController = findNavController(),
),
)
val binding = ComponentCreditCardsBinding.bind(view)
creditCardsView = CreditCardsManagementView(binding, interactor)
loadCreditCards()
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
consumeFrom(store) { state ->
if (!state.isLoading && state.creditCards.isEmpty()) {
findNavController().popBackStack()
return@consumeFrom
}
creditCardsView.update(state)
}
}
override fun onResume() {
super.onResume()
showToolbar(getString(R.string.credit_cards_saved_cards))
}
/**
* When the fragment is paused, navigate back to the settings page to reauthenticate.
*/
override fun onPause() {
// Don't redirect if the user is navigating to the credit card editor fragment.
redirectToReAuth(
listOf(R.id.creditCardEditorFragment),
findNavController().currentDestination?.id,
R.id.creditCardsManagementFragment,
)
super.onPause()
}
/**
* Fetches all the credit cards from the autofill storage and updates the
* [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) {
store.dispatch(AutofillAction.UpdateCreditCards(creditCards))
}
}
}
}
|