Skip to content

Commit 90c88a7

Browse files
authored
[Woo POS] Modularization: move shared dependencies to shared frameworks in preparation for POS modularization (#16159)
2 parents 82c7879 + 6ae4441 commit 90c88a7

File tree

19 files changed

+152
-145
lines changed

19 files changed

+152
-145
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Foundation
2+
3+
/// DateFormatter Extensions
4+
///
5+
extension DateFormatter {
6+
/// Date formatter used for creating a **localized** date and time string.
7+
///
8+
/// Example output in English: "Jan 28, 2018, 11:23 AM"
9+
///
10+
public static let dateAndTimeFormatter: DateFormatter = {
11+
let formatter = DateFormatter()
12+
formatter.setLocalizedDateFormatFromTemplate("MMM d yyyy hh:mm a")
13+
14+
return formatter
15+
}()
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
import Foundation
22

33
extension Decimal {
4-
54
/// Returns the int value of a decimal. We ensure we round up our Decimal before converting it to an Int, using NSDecimalRound.
65
///
7-
var intValue: Int {
6+
public var intValue: Int {
87
NSDecimalNumber(decimal: whole).intValue
98
}
109

11-
func rounded(_ roundingMode: NSDecimalNumber.RoundingMode = .up, scale: Int = 0) -> Self {
10+
public func rounded(_ roundingMode: NSDecimalNumber.RoundingMode = .up, scale: Int = 0) -> Self {
1211
var result = Self()
1312
var number = self
1413
NSDecimalRound(&result, &number, scale, roundingMode)
1514
return result
1615
}
1716

1817
private var whole: Self { rounded( self < 0 ? .down : .up) }
19-
20-
private var fraction: Self { self - whole }
21-
2218
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import SwiftUI
33
import SafariServices
44
import UIKit
55

6-
struct SafariSheetView: UIViewControllerRepresentable {
7-
let url: URL
6+
public struct SafariSheetView: UIViewControllerRepresentable {
7+
private let url: URL
88

9-
func makeUIViewController(context: Context) -> UIViewController {
9+
public init(url: URL) {
10+
self.url = url
11+
}
12+
13+
public func makeUIViewController(context: Context) -> UIViewController {
1014
SFSafariViewController(url: url)
1115
}
1216

13-
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
17+
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
1418
// nothing to do here
1519
}
1620
}
@@ -20,7 +24,7 @@ extension View {
2024
/// Does nothing if the input URL is nil.
2125
///
2226
@ViewBuilder
23-
func safariSheet(isPresented: Binding<Bool>, url: URL?, onDismiss: (() -> Void)? = nil) -> some View {
27+
public func safariSheet(isPresented: Binding<Bool>, url: URL?, onDismiss: (() -> Void)? = nil) -> some View {
2428
if let url = url {
2529
sheet(isPresented: isPresented, onDismiss: onDismiss) {
2630
SafariSheetView(url: url)
@@ -33,7 +37,7 @@ extension View {
3337
///
3438
/// When the sheet is dismissed, the binding's value will be set to nil.
3539
///
36-
func safariSheet(url: Binding<URL?>, onDismiss: (() -> Void)? = nil) -> some View {
40+
public func safariSheet(url: Binding<URL?>, onDismiss: (() -> Void)? = nil) -> some View {
3741
sheet(isPresented: url.notNil(), onDismiss: onDismiss) {
3842
if let url = url.wrappedValue {
3943
SafariSheetView(url: url)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import SwiftUI
2+
import SafariServices
3+
4+
/// SwiftUI interface for UIKit SFSafariViewController
5+
/// Provides a visible interface for web browsing, and Safari features
6+
///
7+
public struct SafariView: UIViewControllerRepresentable {
8+
9+
private let url: URL
10+
11+
public init(url: URL) {
12+
self.url = url
13+
}
14+
15+
public func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
16+
return SFSafariViewController(url: url)
17+
}
18+
19+
public func updateUIViewController(_ uiViewController: SFSafariViewController,
20+
context: UIViewControllerRepresentableContext<SafariView>) {
21+
22+
}
23+
}

WooCommerce/Classes/Tools/SiteAddress.swift renamed to Modules/Sources/Yosemite/Model/Extensions/CountryCode+readableCountry.swift

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,13 @@
11
import Foundation
2-
import Yosemite
32
import WooFoundation
43

5-
/// Represent and parse the Address of the store, returned in the SiteSettings API `/settings/general/`
6-
///
7-
final class SiteAddress {
8-
9-
private let siteSettings: [SiteSetting]
10-
11-
var address: String {
12-
return getValueFromSiteSettings(Constants.address) ?? ""
13-
}
14-
15-
var address2: String {
16-
return getValueFromSiteSettings(Constants.address2) ?? ""
17-
}
18-
19-
var city: String {
20-
return getValueFromSiteSettings(Constants.city) ?? ""
21-
}
22-
23-
var postalCode: String {
24-
return getValueFromSiteSettings(Constants.postalCode) ?? ""
25-
}
26-
27-
var countryCode: CountryCode {
28-
guard let countryComponent = getCountryAndStateComponents().first else {
29-
DDLogError("⛔️ Could not determine country code for site address: no country component found.")
30-
return .unknown
31-
}
32-
guard let countryCode = CountryCode(rawValue: countryComponent) else {
33-
DDLogError("⛔️ Could not determine country code for country: \(countryComponent)")
34-
return .unknown
35-
}
36-
return countryCode
37-
}
38-
39-
/// Returns the name of the country associated with the current store.
40-
/// The default store country is provided in a format like `HK:KOWLOON`
41-
/// This method will transform `HK:KOWLOON` into `Hong Kong`
42-
/// Will return nil if it can not figure out a valid country name
43-
var countryName: String? {
44-
guard countryCode != .unknown else {
45-
return nil
46-
}
47-
48-
return countryCode.readableCountry
49-
}
50-
51-
var state: String {
52-
return getCountryAndStateComponents().last ?? ""
53-
}
54-
55-
init(siteSettings: [SiteSetting] = ServiceLocator.selectedSiteSettings.siteSettings) {
56-
self.siteSettings = siteSettings
57-
}
58-
59-
private func getCountryAndStateComponents() -> [String] {
60-
getValueFromSiteSettings(Constants.countryAndState)?.components(separatedBy: ":") ?? []
61-
}
62-
63-
private func getValueFromSiteSettings(_ settingID: String) -> String? {
64-
return siteSettings.first { (setting) -> Bool in
65-
return setting.settingID == settingID
66-
}?.value
67-
}
68-
}
69-
70-
// MARK: - Constants.
71-
//
72-
private extension SiteAddress {
73-
/// The key of the SiteSetting containing the store address
74-
enum Constants {
75-
static let address = "woocommerce_store_address"
76-
static let address2 = "woocommerce_store_address_2"
77-
static let city = "woocommerce_store_city"
78-
static let postalCode = "woocommerce_store_postcode"
79-
static let countryAndState = "woocommerce_default_country"
80-
}
81-
}
82-
834
// MARK: - Mapping between country codes and readable names
845
// The country names were extracted from the response to `/wp-json/wc/v3/settings/general`
856
// The default countries are listed under `woocommerce_default_country`
867
// in one of the following formats:
878
// - `"COUNTRY_CODE": "READABALE_COUNTRY_NAME"
889
// - `"COUNTRY_CODE:COUNTRY_REGION": "READABLE_COUNTRY_NAME - READABLE_COUNTRY_REGION"
89-
extension CountryCode {
10+
public extension CountryCode {
9011
var readableCountry: String {
9112
switch self {
9213
// A
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import Foundation
2+
import WooFoundation
3+
4+
/// Represent and parse the Address of the store, returned in the SiteSettings API `/settings/general/`
5+
///
6+
public class SiteAddress {
7+
8+
private let siteSettings: [SiteSetting]
9+
10+
public var address: String {
11+
return getValueFromSiteSettings(Constants.address) ?? ""
12+
}
13+
14+
public var address2: String {
15+
return getValueFromSiteSettings(Constants.address2) ?? ""
16+
}
17+
18+
public var city: String {
19+
return getValueFromSiteSettings(Constants.city) ?? ""
20+
}
21+
22+
public var postalCode: String {
23+
return getValueFromSiteSettings(Constants.postalCode) ?? ""
24+
}
25+
26+
public var countryCode: CountryCode {
27+
guard let countryComponent = getCountryAndStateComponents().first else {
28+
DDLogError("⛔️ Could not determine country code for site address: no country component found.")
29+
return .unknown
30+
}
31+
guard let countryCode = CountryCode(rawValue: countryComponent) else {
32+
DDLogError("⛔️ Could not determine country code for country: \(countryComponent)")
33+
return .unknown
34+
}
35+
return countryCode
36+
}
37+
38+
/// Returns the name of the country associated with the current store.
39+
/// The default store country is provided in a format like `HK:KOWLOON`
40+
/// This method will transform `HK:KOWLOON` into `Hong Kong`
41+
/// Will return nil if it can not figure out a valid country name
42+
public var countryName: String? {
43+
guard countryCode != .unknown else {
44+
return nil
45+
}
46+
47+
return countryCode.readableCountry
48+
}
49+
50+
public var state: String {
51+
return getCountryAndStateComponents().last ?? ""
52+
}
53+
54+
public init(siteSettings: [SiteSetting]) {
55+
self.siteSettings = siteSettings
56+
}
57+
58+
private func getCountryAndStateComponents() -> [String] {
59+
getValueFromSiteSettings(Constants.countryAndState)?.components(separatedBy: ":") ?? []
60+
}
61+
62+
private func getValueFromSiteSettings(_ settingID: String) -> String? {
63+
return siteSettings.first { (setting) -> Bool in
64+
return setting.settingID == settingID
65+
}?.value
66+
}
67+
}
68+
69+
// MARK: - Constants.
70+
//
71+
private extension SiteAddress {
72+
/// The key of the SiteSetting containing the store address
73+
enum Constants {
74+
static let address = "woocommerce_store_address"
75+
static let address2 = "woocommerce_store_address_2"
76+
static let city = "woocommerce_store_city"
77+
static let postalCode = "woocommerce_store_postcode"
78+
static let countryAndState = "woocommerce_default_country"
79+
}
80+
}

WooCommerce/WooCommerceTests/Tools/SiteAddressTests.swift renamed to Modules/Tests/YosemiteTests/Tools/Settings/SiteAddressTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import XCTest
2-
@testable import WooCommerce
32
@testable import Networking
3+
@testable import Yosemite
44

55
final class SiteAddressTests: XCTestCase {
66

WooCommerce/Classes/Extensions/DateFormatter+Helpers.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,4 @@ extension DateFormatter {
8181
formatter.setLocalizedDateFormatFromTemplate("hh:mm a")
8282
return formatter
8383
}()
84-
85-
/// Date formatter used for creating a **localized** date and time string.
86-
///
87-
/// Example output in English: "Jan 28, 2018, 11:23 AM"
88-
///
89-
public static let dateAndTimeFormatter: DateFormatter = {
90-
let formatter = DateFormatter()
91-
formatter.setLocalizedDateFormatFromTemplate("MMM d yyyy hh:mm a")
92-
93-
return formatter
94-
}()
9584
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import class Yosemite.SiteAddress
2+
3+
extension SiteAddress {
4+
convenience init() {
5+
self.init(siteSettings: ServiceLocator.selectedSiteSettings.siteSettings)
6+
}
7+
}

WooCommerce/Classes/POS/Presentation/Settings/POSSettingsStoreViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import SwiftUI
2+
import class Yosemite.SiteAddress
23
import protocol Yosemite.PluginsServiceProtocol
34
import protocol Yosemite.PointOfSaleSettingsServiceProtocol
45
import enum Yosemite.Plugin

0 commit comments

Comments
 (0)