Skip to content

Commit 8a508b2

Browse files
authored
Merge pull request #5008 from woocommerce/issue/4922-disable-disconnect-during-update
[Mobile Payments] [WIP] Disable disconnect button while checking for reader software update
2 parents d25171f + d0dc00b commit 8a508b2

File tree

2 files changed

+77
-37
lines changed

2 files changed

+77
-37
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Settings/CardReadersV2/CardReaderSettingsConnectedViewController.swift

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ private extension CardReaderSettingsConnectedViewController {
7979
configureUpdateView()
8080
}
8181

82-
func shouldShowUpdateControls() -> Bool {
83-
guard let viewModel = viewModel else {
84-
return false
85-
}
86-
return viewModel.readerUpdateAvailable == .isTrue
87-
}
88-
8982
/// Set the title and back button.
9083
///
9184
func configureNavigation() {
@@ -97,31 +90,27 @@ private extension CardReaderSettingsConnectedViewController {
9790
func configureSections() {
9891
sections = []
9992

100-
/// This section, if present, displays a prompt to update a reader running old software
93+
/// This section displays whether or not there is update for the reader software
10194
///
102-
if shouldShowUpdateControls() {
103-
sections.append(
104-
Section(title: nil,
105-
rows: [
106-
.updatePrompt
107-
]
108-
)
109-
)
95+
let checkForReaderUpdateInProgress = viewModel?.checkForReaderUpdateInProgress ?? false
96+
var rows = [Row]()
97+
if checkForReaderUpdateInProgress {
98+
rows = [.checkingForUpdate]
99+
} else {
100+
rows = [.updatePrompt]
110101
}
111102

103+
sections.append(Section(title: nil, rows: rows))
104+
112105
/// This section displays details about the connected reader
113106
///
114-
var rows: [Row] = [.connectedReader]
115-
116-
if shouldShowUpdateControls() {
117-
rows.append(.updateButton)
118-
}
119-
120-
rows.append(.disconnectButton)
121-
122107
sections.append(
123108
Section(title: Localization.sectionHeaderTitle.uppercased(),
124-
rows: rows
109+
rows: [
110+
.connectedReader,
111+
.updateButton,
112+
.disconnectButton
113+
]
125114
)
126115
)
127116
}
@@ -178,6 +167,8 @@ private extension CardReaderSettingsConnectedViewController {
178167
///
179168
func configure(_ cell: UITableViewCell, for row: Row, at indexPath: IndexPath) {
180169
switch cell {
170+
case let cell as ActivitySpinnerAndLabelTableViewCell where row == .checkingForUpdate:
171+
configureCheckingForUpdate(cell: cell)
181172
case let cell as LeftImageTableViewCell where row == .updatePrompt:
182173
configureUpdatePrompt(cell: cell)
183174
case let cell as ConnectedReaderTableViewCell where row == .connectedReader:
@@ -191,11 +182,29 @@ private extension CardReaderSettingsConnectedViewController {
191182
}
192183
}
193184

185+
private func configureCheckingForUpdate(cell: ActivitySpinnerAndLabelTableViewCell) {
186+
cell.configure(labelText: Localization.updateChecking)
187+
cell.selectionStyle = .none
188+
}
189+
194190
private func configureUpdatePrompt(cell: LeftImageTableViewCell) {
195-
cell.configure(image: .infoOutlineImage, text: Localization.updatePromptText)
191+
guard let readerUpdateAvailable = viewModel?.readerUpdateAvailable else {
192+
return
193+
}
194+
195+
if readerUpdateAvailable == .isFalse {
196+
cell.configure(image: .infoOutlineImage, text: Localization.updateNotNeeded)
197+
cell.backgroundColor = .none
198+
cell.imageView?.tintColor = .info
199+
}
200+
201+
if readerUpdateAvailable == .isTrue {
202+
cell.configure(image: .infoOutlineImage, text: Localization.updateAvailable)
203+
cell.backgroundColor = .warningBackground
204+
cell.imageView?.tintColor = .warning
205+
}
206+
196207
cell.selectionStyle = .none
197-
cell.backgroundColor = .warningBackground
198-
cell.imageView?.tintColor = .warning
199208
cell.textLabel?.numberOfLines = 0
200209
cell.textLabel?.textColor = .text
201210
}
@@ -210,29 +219,39 @@ private extension CardReaderSettingsConnectedViewController {
210219
cell.selectionStyle = .none
211220
}
212221

222+
/// If a reader update is available, make the update button primary
223+
/// If a reader update is available and a disconnect or update isn't already in progress, enable the button
224+
///
213225
private func configureUpdateButton(cell: ButtonTableViewCell) {
214-
cell.configure(style: .primary, title: Localization.updateButtonTitle, bottomSpacing: 0) {
226+
let readerUpdateAvailable = viewModel?.readerUpdateAvailable == .isTrue
227+
let style: ButtonTableViewCell.Style = readerUpdateAvailable ? .primary : .secondary
228+
cell.configure(style: style, title: Localization.updateButtonTitle, bottomSpacing: 0) {
215229
self.viewModel?.startCardReaderUpdate()
216230
}
217231

218232
let readerDisconnectInProgress = viewModel?.readerDisconnectInProgress ?? false
219233
let readerUpdateInProgress = viewModel?.readerUpdateInProgress ?? false
220-
cell.enableButton(!readerDisconnectInProgress && !readerUpdateInProgress)
234+
cell.enableButton(readerUpdateAvailable && !readerDisconnectInProgress && !readerUpdateInProgress)
221235
cell.showActivityIndicator(readerUpdateInProgress)
222236

223237
cell.selectionStyle = .none
224238
cell.backgroundColor = .clear
225239
}
226240

241+
/// If a reader update is not available, make the disconnect button primary
242+
/// If a check for updates, a disconnect or an update isn't already in progress, enable the disconnect button
243+
///
227244
private func configureDisconnectButton(cell: ButtonTableViewCell) {
228-
let style: ButtonTableViewCell.Style = shouldShowUpdateControls() ? .secondary : .primary
245+
let checkForReaderUpdateInProgress = viewModel?.checkForReaderUpdateInProgress ?? false
246+
let readerUpdateAvailable = viewModel?.readerUpdateAvailable == .isTrue
247+
let style: ButtonTableViewCell.Style = readerUpdateAvailable ? .secondary : .primary
229248
cell.configure(style: style, title: Localization.disconnectButtonTitle) { [weak self] in
230249
self?.viewModel?.disconnectReader()
231250
}
232251

233252
let readerDisconnectInProgress = viewModel?.readerDisconnectInProgress ?? false
234253
let readerUpdateInProgress = viewModel?.readerUpdateInProgress ?? false
235-
cell.enableButton(!readerDisconnectInProgress && !readerUpdateInProgress)
254+
cell.enableButton(!checkForReaderUpdateInProgress && !readerDisconnectInProgress && !readerUpdateInProgress)
236255
cell.showActivityIndicator(readerDisconnectInProgress)
237256

238257
cell.selectionStyle = .none
@@ -262,10 +281,7 @@ extension CardReaderSettingsConnectedViewController: UITableViewDataSource {
262281
}
263282

264283
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
265-
if shouldShowUpdateControls() {
266-
return section == 0 ? CGFloat.leastNonzeroMagnitude : UITableView.automaticDimension
267-
}
268-
return UITableView.automaticDimension
284+
return section == 0 ? CGFloat.leastNonzeroMagnitude : UITableView.automaticDimension
269285
}
270286

271287
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
@@ -317,13 +333,16 @@ private struct Section {
317333
}
318334

319335
private enum Row: CaseIterable {
336+
case checkingForUpdate
320337
case updatePrompt
321338
case connectedReader
322339
case updateButton
323340
case disconnectButton
324341

325342
var type: UITableViewCell.Type {
326343
switch self {
344+
case .checkingForUpdate:
345+
return ActivitySpinnerAndLabelTableViewCell.self
327346
case .updatePrompt:
328347
return LeftImageTableViewCell.self
329348
case .connectedReader:
@@ -349,11 +368,21 @@ private extension CardReaderSettingsConnectedViewController {
349368
comment: "Settings > Manage Card Reader > Title for the reader connected screen in settings."
350369
)
351370

352-
static let updatePromptText = NSLocalizedString(
371+
static let updateChecking = NSLocalizedString(
372+
"Checking for reader software updates",
373+
comment: "Settings > Manage Card Reader > Connected Reader > A prompt to indicate we are checking for reader updates"
374+
)
375+
376+
static let updateAvailable = NSLocalizedString(
353377
"Please update your reader software to keep accepting payments",
354378
comment: "Settings > Manage Card Reader > Connected Reader > A prompt to update a reader running older software"
355379
)
356380

381+
static let updateNotNeeded = NSLocalizedString(
382+
"Congratulations! Your reader is running the latest software",
383+
comment: "Settings > Manage Card Reader > Connected Reader > A prompt to update a reader running older software"
384+
)
385+
357386
static let sectionHeaderTitle = NSLocalizedString(
358387
"Connected Reader",
359388
comment: "Settings > Manage Card Reader > Connected Reader Table Section Heading"

WooCommerce/Classes/ViewRelated/Dashboard/Settings/CardReadersV2/CardReaderSettingsConnectedViewModel.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ final class CardReaderSettingsConnectedViewModel: CardReaderSettingsPresentedVie
1010
private var connectedReaders = [CardReader]()
1111
private let knownReadersProvider: CardReaderSettingsKnownReadersProvider?
1212

13+
private(set) var checkForReaderUpdateInProgress: Bool = false
1314
private(set) var readerUpdateAvailable: CardReaderSettingsTriState = .isUnknown
1415
private(set) var readerUpdateInProgress: Bool = false
1516
private(set) var readerUpdateCompletedSuccessfully: Bool = false
@@ -47,6 +48,7 @@ final class CardReaderSettingsConnectedViewModel: CardReaderSettingsPresentedVie
4748
updateReaderID()
4849
updateBatteryLevel()
4950
updateSoftwareVersion()
51+
didUpdate?()
5052
}
5153

5254
private func updateReaderID() {
@@ -76,6 +78,14 @@ final class CardReaderSettingsConnectedViewModel: CardReaderSettingsPresentedVie
7678
/// Dispatch a request to check for reader updates
7779
///
7880
func checkForCardReaderUpdate() {
81+
guard !checkForReaderUpdateInProgress else {
82+
return
83+
}
84+
85+
readerUpdateAvailable = .isUnknown
86+
checkForReaderUpdateInProgress = true
87+
didUpdate?()
88+
7989
let action = CardPresentPaymentAction.checkForCardReaderUpdate() { [weak self] result in
8090
guard let self = self else {
8191
return
@@ -90,6 +100,7 @@ final class CardReaderSettingsConnectedViewModel: CardReaderSettingsPresentedVie
90100
DDLogError("Unexpected error when checking for reader update")
91101
self.readerUpdateAvailable = .isFalse
92102
}
103+
self.checkForReaderUpdateInProgress = false
93104
self.didUpdate?()
94105
}
95106
ServiceLocator.stores.dispatch(action)

0 commit comments

Comments
 (0)