@@ -2,51 +2,34 @@ import Foundation
22import Combine
33import struct Yosemite. SiteSetting
44import enum Yosemite. Plugin
5- import class Yosemite. PluginsService
6- import Observation
7-
5+ import struct Yosemite. SystemPlugin
6+ import protocol Yosemite. PluginsServiceProtocol
87import protocol Yosemite. PointOfSaleSettingsServiceProtocol
9- import class Yosemite. PointOfSaleSettingsService
10- import Storage
8+ import struct Yosemite. POSReceiptInformation
9+ import Observation
1110
1211protocol PointOfSaleSettingsControllerProtocol {
13- var receiptStoreName : String ? { get }
14- var receiptStoreAddress : String ? { get }
15- var receiptStorePhone : String ? { get }
16- var receiptStoreEmail : String ? { get }
17- var receiptRefundReturnsPolicy : String ? { get }
18- var isLoading : Bool { get }
19- var shouldShowReceiptInformation : Bool { get }
20- var storeName : String { get }
21- var storeAddress : String { get }
22-
2312 var connectedCardReader : CardPresentPaymentCardReader ? { get }
24-
25- func retrievePOSReceiptSettings( ) async
13+ var storeViewModel : POSSettingsStoreViewModel { get }
2614}
2715
2816@Observable final class PointOfSaleSettingsController : PointOfSaleSettingsControllerProtocol {
29- private( set) var receiptStoreName : String ?
30- private( set) var receiptStoreAddress : String ?
31- private( set) var receiptStorePhone : String ?
32- private( set) var receiptStoreEmail : String ?
33- private( set) var receiptRefundReturnsPolicy : String ?
34- private( set) var isLoading : Bool = false
35- private( set) var shouldShowReceiptInformation : Bool = false
36-
37- private let defaultSiteName : String ?
38- private let settingsService : PointOfSaleSettingsServiceProtocol
39- private let siteSettings : [ SiteSetting ]
4017 private( set) var connectedCardReader : CardPresentPaymentCardReader ?
4118 private var cancellables : AnyCancellable ?
4219
43- init ( settingsService: PointOfSaleSettingsServiceProtocol ,
20+ let storeViewModel : POSSettingsStoreViewModel
21+
22+ init ( siteID: Int64 ,
23+ settingsService: PointOfSaleSettingsServiceProtocol ,
4424 cardPresentPaymentService: CardPresentPaymentFacade ,
25+ pluginsService: PluginsServiceProtocol ,
4526 defaultSiteName: String ? = ServiceLocator . stores. sessionManager. defaultSite? . name,
4627 siteSettings: [ SiteSetting ] = ServiceLocator . selectedSiteSettings. siteSettings) {
47- self . settingsService = settingsService
48- self . defaultSiteName = defaultSiteName
49- self . siteSettings = siteSettings
28+ self . storeViewModel = POSSettingsStoreViewModel ( siteID: siteID,
29+ settingsService: settingsService,
30+ pluginsService: pluginsService,
31+ defaultSiteName: defaultSiteName,
32+ siteSettings: siteSettings)
5033
5134 observeCardReader ( from: cardPresentPaymentService)
5235 }
@@ -65,104 +48,53 @@ protocol PointOfSaleSettingsControllerProtocol {
6548 connectedCardReader = cardReader
6649 } )
6750 }
68-
69- var storeName : String {
70- if let defaultSiteName {
71- return defaultSiteName
72- } else {
73- return Localization . storeNameNotSet
74- }
75- }
76-
77- var storeAddress : String {
78- SiteAddress ( siteSettings: siteSettings) . address
79- }
80-
81- @MainActor
82- func retrievePOSReceiptSettings( ) async {
83- isLoading = true
84-
85- shouldShowReceiptInformation = await isPluginSupported ( . wooCommerce, minimumVersion: Constants . minimumWooCommerceVersion)
86-
87- guard shouldShowReceiptInformation else {
88- isLoading = false
89- return
90- }
91-
92- do {
93- let siteSettings = try await settingsService. retrievePointOfSaleSettings ( )
94- updateReceiptSettings ( from: siteSettings)
95- } catch {
96- DDLogError ( " Failed to load POS settings: \( error) " )
97- }
98- isLoading = false
99- }
100-
101- @MainActor
102- private func isPluginSupported( _ plugin: Plugin ,
103- storageManager: StorageManagerType = ServiceLocator . storageManager,
104- minimumVersion: String ) async -> Bool {
105- let pluginsService = PluginsService ( storageManager: storageManager)
106- guard let systemPlugin = pluginsService. loadPluginInStorage ( siteID: settingsService. siteID, plugin: plugin, isActive: true ) , systemPlugin. active else {
107- return false
108- }
109-
110- let isSupported = VersionHelpers . isVersionSupported ( version: systemPlugin. version,
111- minimumRequired: minimumVersion)
112- return isSupported
113- }
114-
115- private func updateReceiptSettings( from siteSettings: [ SiteSetting ] ) {
116- receiptStoreName = settingValue ( from: siteSettings, settingID: " woocommerce_pos_store_name " )
117- receiptStoreAddress = settingValue ( from: siteSettings, settingID: " woocommerce_pos_store_address " )
118- receiptStorePhone = settingValue ( from: siteSettings, settingID: " woocommerce_pos_store_phone " )
119- receiptStoreEmail = settingValue ( from: siteSettings, settingID: " woocommerce_pos_store_email " )
120- receiptRefundReturnsPolicy = settingValue ( from: siteSettings, settingID: " woocommerce_pos_refund_returns_policy " )
121- }
122-
123- private func settingValue( from siteSettings: [ SiteSetting ] , settingID: String ) -> String ? {
124- let value = siteSettings. first { $0. settingID == settingID } ? . value
125- return value? . isEmpty == true ? nil : value
126- }
127- }
128-
129- private extension PointOfSaleSettingsController {
130- enum Constants {
131- static let minimumWooCommerceVersion : String = " 10.0 "
132- }
133-
134- enum Localization {
135- static let storeNameNotSet = NSLocalizedString (
136- " pointOfSaleSettingsService.storeNameNotSet " ,
137- value: " Not set " ,
138- comment: " Text displayed on Point of Sale settings when store has not been provided. "
139- )
140- }
14151}
14252
14353#if DEBUG
14454final class PointOfSaleSettingsPreviewController : PointOfSaleSettingsControllerProtocol {
145- var receiptStoreName : String ? = " Sample Store "
146- var receiptStoreAddress : String ? = " 123 Main Street \n Anytown, ST 12345 "
147- var receiptStorePhone : String ? = " +1 (555) 123-4567 "
148- var receiptStoreEmail : String ? = " [email protected] " 149- var receiptRefundReturnsPolicy : String ? = " 30-day return policy "
150- var isLoading : Bool = false
151- var shouldShowReceiptInformation : Bool = true
152- var storeName : String = " Sample Store "
153-
15455 var connectedCardReader : CardPresentPaymentCardReader ? = CardPresentPaymentCardReader (
15556 name: " WisePad 3 " ,
15657 batteryLevel: 0.75
15758 )
15859
159- var storeAddress : String {
160- " 123 Main Street \n Anytown, ST 12345 "
60+ var storeViewModel : POSSettingsStoreViewModel = POSSettingsStoreViewModel ( siteID: 123 ,
61+ settingsService: MockPointOfSaleSettingsService ( ) ,
62+ pluginsService: PluginsServicePreview ( ) ,
63+ defaultSiteName: " Sample Store " ,
64+ siteSettings: [ ] )
65+ }
66+
67+ final class MockPointOfSaleSettingsService : PointOfSaleSettingsServiceProtocol {
68+ func retrievePointOfSaleSettings( ) async throws -> POSReceiptInformation {
69+ return . empty
16170 }
71+ }
16272
163- func retrievePOSReceiptSettings( ) async {
164- // no-op
73+ final class PluginsServicePreview : PluginsServiceProtocol {
74+ func waitForPluginInStorage( siteID: Int64 , pluginPath: String , isActive: Bool ) async -> SystemPlugin {
75+ return SystemPlugin ( siteID: 1234 ,
76+ plugin: " " ,
77+ name: " " ,
78+ version: " " ,
79+ versionLatest: " " ,
80+ url: " " ,
81+ authorName: " " ,
82+ authorUrl: " " ,
83+ networkActivated: false ,
84+ active: true )
16585 }
16686
87+ func loadPluginInStorage( siteID: Int64 , plugin: Plugin , isActive: Bool ? ) -> SystemPlugin ? {
88+ return SystemPlugin ( siteID: 1234 ,
89+ plugin: " " ,
90+ name: " " ,
91+ version: " " ,
92+ versionLatest: " " ,
93+ url: " " ,
94+ authorName: " " ,
95+ authorUrl: " " ,
96+ networkActivated: false ,
97+ active: true )
98+ }
16799}
168100#endif
0 commit comments