Skip to content

Commit 686f911

Browse files
committed
Load and set the order creation beta switch from GeneralAppSettings
1 parent 9ff1da2 commit 686f911

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

Storage/Storage/Model/Copiable/Models+Copiable.generated.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ extension GeneralAppSettings {
1010
feedbacks: CopiableProp<[FeedbackType: FeedbackSettings]> = .copy,
1111
isViewAddOnsSwitchEnabled: CopiableProp<Bool> = .copy,
1212
isSimplePaymentsSwitchEnabled: CopiableProp<Bool> = .copy,
13+
isOrderCreationSwitchEnabled: CopiableProp<Bool> = .copy,
1314
knownCardReaders: CopiableProp<[String]> = .copy,
1415
lastEligibilityErrorInfo: NullableCopiableProp<EligibilityErrorInfo> = .copy
1516
) -> GeneralAppSettings {
1617
let installationDate = installationDate ?? self.installationDate
1718
let feedbacks = feedbacks ?? self.feedbacks
1819
let isViewAddOnsSwitchEnabled = isViewAddOnsSwitchEnabled ?? self.isViewAddOnsSwitchEnabled
1920
let isSimplePaymentsSwitchEnabled = isSimplePaymentsSwitchEnabled ?? self.isSimplePaymentsSwitchEnabled
21+
let isOrderCreationSwitchEnabled = isOrderCreationSwitchEnabled ?? self.isOrderCreationSwitchEnabled
2022
let knownCardReaders = knownCardReaders ?? self.knownCardReaders
2123
let lastEligibilityErrorInfo = lastEligibilityErrorInfo ?? self.lastEligibilityErrorInfo
2224

@@ -25,6 +27,7 @@ extension GeneralAppSettings {
2527
feedbacks: feedbacks,
2628
isViewAddOnsSwitchEnabled: isViewAddOnsSwitchEnabled,
2729
isSimplePaymentsSwitchEnabled: isSimplePaymentsSwitchEnabled,
30+
isOrderCreationSwitchEnabled: isOrderCreationSwitchEnabled,
2831
knownCardReaders: knownCardReaders,
2932
lastEligibilityErrorInfo: lastEligibilityErrorInfo
3033
)

Storage/Storage/Model/GeneralAppSettings.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public struct GeneralAppSettings: Codable, Equatable, GeneratedCopiable {
2828
///
2929
public let isSimplePaymentsSwitchEnabled: Bool
3030

31+
/// The state(`true` or `false`) for the Order Creation feature switch.
32+
///
33+
public let isOrderCreationSwitchEnabled: Bool
34+
3135
/// A list (possibly empty) of known card reader IDs - i.e. IDs of card readers that should be reconnected to automatically
3236
/// e.g. ["CHB204909005931"]
3337
///
@@ -41,12 +45,14 @@ public struct GeneralAppSettings: Codable, Equatable, GeneratedCopiable {
4145
feedbacks: [FeedbackType: FeedbackSettings],
4246
isViewAddOnsSwitchEnabled: Bool,
4347
isSimplePaymentsSwitchEnabled: Bool,
48+
isOrderCreationSwitchEnabled: Bool,
4449
knownCardReaders: [String],
4550
lastEligibilityErrorInfo: EligibilityErrorInfo? = nil) {
4651
self.installationDate = installationDate
4752
self.feedbacks = feedbacks
4853
self.isViewAddOnsSwitchEnabled = isViewAddOnsSwitchEnabled
4954
self.isSimplePaymentsSwitchEnabled = isSimplePaymentsSwitchEnabled
55+
self.isOrderCreationSwitchEnabled = isOrderCreationSwitchEnabled
5056
self.knownCardReaders = knownCardReaders
5157
self.lastEligibilityErrorInfo = lastEligibilityErrorInfo
5258
}
@@ -73,6 +79,7 @@ public struct GeneralAppSettings: Codable, Equatable, GeneratedCopiable {
7379
feedbacks: updatedFeedbacks,
7480
isViewAddOnsSwitchEnabled: isViewAddOnsSwitchEnabled,
7581
isSimplePaymentsSwitchEnabled: isSimplePaymentsSwitchEnabled,
82+
isOrderCreationSwitchEnabled: isOrderCreationSwitchEnabled,
7683
knownCardReaders: knownCardReaders,
7784
lastEligibilityErrorInfo: lastEligibilityErrorInfo
7885
)
@@ -90,6 +97,7 @@ extension GeneralAppSettings {
9097
self.feedbacks = try container.decodeIfPresent([FeedbackType: FeedbackSettings].self, forKey: .feedbacks) ?? [:]
9198
self.isViewAddOnsSwitchEnabled = try container.decodeIfPresent(Bool.self, forKey: .isViewAddOnsSwitchEnabled) ?? false
9299
self.isSimplePaymentsSwitchEnabled = try container.decodeIfPresent(Bool.self, forKey: .isSimplePaymentsSwitchEnabled) ?? false
100+
self.isOrderCreationSwitchEnabled = try container.decodeIfPresent(Bool.self, forKey: .isOrderCreationSwitchEnabled) ?? false
93101
self.knownCardReaders = try container.decodeIfPresent([String].self, forKey: .knownCardReaders) ?? []
94102
self.lastEligibilityErrorInfo = try container.decodeIfPresent(EligibilityErrorInfo.self, forKey: .lastEligibilityErrorInfo)
95103

WooCommerce/Classes/ViewRelated/Dashboard/Settings/Beta features/BetaFeaturesViewController.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ private extension BetaFeaturesViewController {
215215
func configureOrderCreationSwitch(cell: SwitchTableViewCell) {
216216
configureCommonStylesForSwitchCell(cell)
217217
cell.title = Localization.orderCreationTitle
218+
219+
// Fetch switch's state stored value.
220+
let action = AppSettingsAction.loadOrderCreationSwitchState() { result in
221+
guard let isEnabled = try? result.get() else {
222+
return cell.isOn = false
223+
}
224+
cell.isOn = isEnabled
225+
}
226+
ServiceLocator.stores.dispatch(action)
227+
228+
// Change switch's state stored value
229+
cell.onChange = { isSwitchOn in
230+
let action = AppSettingsAction.setOrderCreationFeatureSwitchState(isEnabled: isSwitchOn, onCompletion: { result in
231+
// Roll back toggle if an error occurred
232+
if result.isFailure {
233+
cell.isOn.toggle()
234+
}
235+
})
236+
ServiceLocator.stores.dispatch(action)
237+
}
218238
cell.accessibilityIdentifier = "beta-features-order-order-creation-cell"
219239
}
220240

Yosemite/Yosemite/Actions/AppSettingsAction.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ public enum AppSettingsAction: Action {
110110
///
111111
case setSimplePaymentsFeatureSwitchState(isEnabled: Bool, onCompletion: (Result<Void, Error>) -> Void)
112112

113+
/// Loads the most recent state for the Order Creation beta feature switch
114+
///
115+
case loadOrderCreationSwitchState(onCompletion: (Result<Bool, Error>) -> Void)
116+
117+
/// Sets the state for the Order Creation beta feature switch.
118+
///
119+
case setOrderCreationFeatureSwitchState(isEnabled: Bool, onCompletion: (Result<Void, Error>) -> Void)
120+
113121
/// Remember the given card reader (to support automatic reconnection)
114122
/// where `cardReaderID` is a String e.g. "CHB204909005931"
115123
///

Yosemite/Yosemite/Stores/AppSettingsStore.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public class AppSettingsStore: Store {
146146
setSimplePaymentsFeatureSwitchState(isEnabled: isEnabled, onCompletion: onCompletion)
147147
case .loadSimplePaymentsSwitchState(onCompletion: let onCompletion):
148148
loadSimplePaymentsSwitchState(onCompletion: onCompletion)
149+
case .setOrderCreationFeatureSwitchState(isEnabled: let isEnabled, onCompletion: let onCompletion):
150+
setOrderCreationFeatureSwitchState(isEnabled: isEnabled, onCompletion: onCompletion)
151+
case .loadOrderCreationSwitchState(onCompletion: let onCompletion):
152+
loadOrderCreationSwitchState(onCompletion: onCompletion)
149153
case .rememberCardReader(cardReaderID: let cardReaderID, onCompletion: let onCompletion):
150154
rememberCardReader(cardReaderID: cardReaderID, onCompletion: onCompletion)
151155
case .forgetCardReader(onCompletion: let onCompletion):
@@ -261,6 +265,26 @@ private extension AppSettingsStore {
261265

262266
}
263267

268+
/// Loads the current Order Creation beta feature switch state from `GeneralAppSettings`
269+
///
270+
func loadOrderCreationSwitchState(onCompletion: (Result<Bool, Error>) -> Void) {
271+
let settings = loadOrCreateGeneralAppSettings()
272+
onCompletion(.success(settings.isOrderCreationSwitchEnabled))
273+
}
274+
275+
/// Sets the provided Order Creation beta feature switch state into `GeneralAppSettings`
276+
///
277+
func setOrderCreationFeatureSwitchState(isEnabled: Bool, onCompletion: (Result<Void, Error>) -> Void) {
278+
do {
279+
let settings = loadOrCreateGeneralAppSettings().copy(isOrderCreationSwitchEnabled: isEnabled)
280+
try saveGeneralAppSettings(settings)
281+
onCompletion(.success(()))
282+
} catch {
283+
onCompletion(.failure(error))
284+
}
285+
286+
}
287+
264288
/// Loads the last persisted eligibility error information from `GeneralAppSettings`
265289
///
266290
func loadEligibilityErrorInfo(onCompletion: (Result<EligibilityErrorInfo, Error>) -> Void) {
@@ -290,6 +314,7 @@ private extension AppSettingsStore {
290314
feedbacks: [:],
291315
isViewAddOnsSwitchEnabled: false,
292316
isSimplePaymentsSwitchEnabled: false,
317+
isOrderCreationSwitchEnabled: false,
293318
knownCardReaders: [],
294319
lastEligibilityErrorInfo: nil)
295320
}

0 commit comments

Comments
 (0)