Skip to content

Commit

Permalink
refactor: move stageTag and devTouchpoint options to Environment enum (
Browse files Browse the repository at this point in the history
  • Loading branch information
yeul authored Dec 14, 2023
1 parent 3eeebea commit d4a9d4a
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 175 deletions.
1 change: 0 additions & 1 deletion Demo/Demo/DefaultMessageConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ let defaultMessageConfig: PayPalMessageConfig = {
)
// Override defaults for ease of development
config.data.ignoreCache = false
config.data.devTouchpoint = false

return config
}()
52 changes: 10 additions & 42 deletions Demo/Demo/SwiftUIContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ struct SwiftUIContentView: View {
@State private var amount: Double? = defaultMessageConfig.data.amount
@State private var placement: PayPalMessagePlacement? = defaultMessageConfig.data.placement
@State private var offerType: PayPalMessageOfferType? = defaultMessageConfig.data.offerType
@State private var stageTag: String = defaultMessageConfig.data.stageTag ?? ""
@State private var buyerCountry: String = defaultMessageConfig.data.buyerCountry ?? ""
@State private var ignoreCache: Bool = defaultMessageConfig.data.ignoreCache
@State private var devTouchpoint: Bool = defaultMessageConfig.data.devTouchpoint

@State private var messageState: String = ""
@State private var debounceTimerInterval: TimeInterval = 1
Expand Down Expand Up @@ -46,7 +44,6 @@ struct SwiftUIContentView: View {

messageConfig.data.buyerCountry = buyerCountry
messageConfig.data.ignoreCache = ignoreCache
messageConfig.data.devTouchpoint = devTouchpoint

return messageConfig
}
Expand Down Expand Up @@ -143,47 +140,20 @@ struct SwiftUIContentView: View {
debounceConfigUpdate()
}
}

HStack {
// Stage Tag
ReusableTextView(text: "Stage Tag", font: .subheadline, weight: .semibold)

ReusableTextField(text: $stageTag)
.onChange(of: stageTag) { _ in
debounceConfigUpdate()
}
}
}

HStack {
HStack {
// Ignore Cache
ReusableToggle(isOn: $ignoreCache, label: "ignoreCache")

ReusableTextView(
text: "Ignore Cache",
font: .system(size: 14),
weight: .semibold,
padding: .init(top: 0, leading: 16, bottom: 0, trailing: 0)
)
.onChange(of: ignoreCache) { _ in
debounceConfigUpdate()
}
}
HStack {

// Dev Touchpoint
ReusableToggle(isOn: $devTouchpoint, label: "devTouchpoint")
// Ignore Cache
ReusableToggle(isOn: $ignoreCache, label: "ignoreCache")

ReusableTextView(
text: "Dev Touchpoint",
font: .system(size: 14),
weight: .semibold,
padding: .init(top: 0, leading: 16, bottom: 0, trailing: 0)
)
.onChange(of: devTouchpoint) { _ in
debounceConfigUpdate()
}
ReusableTextView(
text: "Ignore Cache",
font: .system(size: 14),
weight: .semibold,
padding: .init(top: 0, leading: 16, bottom: 0, trailing: 0)
)
.onChange(of: ignoreCache) { _ in
debounceConfigUpdate()
}
}

Expand Down Expand Up @@ -224,9 +194,7 @@ struct SwiftUIContentView: View {
offerType = defaultData.offerType
amount = defaultData.amount
buyerCountry = defaultData.buyerCountry ?? ""
stageTag = defaultData.stageTag ?? ""
ignoreCache = defaultData.ignoreCache
devTouchpoint = defaultData.devTouchpoint
clientID = defaultData.clientID
}

Expand Down
40 changes: 1 addition & 39 deletions Demo/Demo/UIKitContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ class UIKitContentViewController: UIViewController {

lazy var buyerCountryLabel = getLabel(text: "Buyer Country")

lazy var stageTagLabel = getLabel(text: "Stage Tag")

lazy var ignoreCacheLabel = getLabel(text: "Ignore Cache")

lazy var devTouchpointLabel = getLabel(text: "Dev Touchpoint")

lazy var logoTypePicker: UISegmentedControl = getSegmentedControl(
action: #selector(updatePayPalMessageMessage),
forType: PayPalMessageLogoType.self
Expand Down Expand Up @@ -69,22 +65,11 @@ class UIKitContentViewController: UIViewController {
autoCapitalizationType: .allCharacters
)

lazy var stageTagField: UITextField = getTextField(
action: #selector(updatePayPalMessageMessage),
keyboardType: .default,
autoCapitalizationType: .none
)

lazy var ignoreCacheSwitch: UISwitch = getSwitch(
isOn: defaultMessageConfig.data.ignoreCache,
action: #selector(updatePayPalMessageMessage)
)

lazy var devTouchpointSwitch: UISwitch = getSwitch(
isOn: defaultMessageConfig.data.devTouchpoint,
action: #selector(updatePayPalMessageMessage)
)

lazy var clientIDField: UITextField = getTextField(
action: #selector(updatePayPalMessageMessage),
keyboardType: .default,
Expand Down Expand Up @@ -146,19 +131,10 @@ class UIKitContentViewController: UIViewController {
],
axis: .horizontal
),
getStackView(
subviews: [
stageTagLabel,
stageTagField
],
axis: .horizontal
),
getStackView(
subviews: [
ignoreCacheSwitch,
ignoreCacheLabel,
devTouchpointSwitch,
devTouchpointLabel
ignoreCacheLabel
],
axis: .horizontal
),
Expand Down Expand Up @@ -199,9 +175,7 @@ class UIKitContentViewController: UIViewController {
loadSegmentedIndex(item: defaultMessageConfig.style.color, picker: colorTypePicker)
loadSegmentedIndex(item: defaultMessageConfig.style.textAlignment, picker: alignmentTypePicker)
buyerCountryField.text = defaultMessageConfig.data.buyerCountry
stageTagField.text = defaultMessageConfig.data.stageTag
ignoreCacheSwitch.isOn = defaultMessageConfig.data.ignoreCache
devTouchpointSwitch.isOn = defaultMessageConfig.data.devTouchpoint

if let amount = defaultMessageConfig.data.amount {
amountTextField.text = String(format: "%f", amount)
Expand Down Expand Up @@ -268,8 +242,6 @@ class UIKitContentViewController: UIViewController {

config.data.buyerCountry = getCurrentBuyerCountry()
config.data.ignoreCache = getCurrentIgnoreCache()
config.data.stageTag = getCurrentStageTag()
config.data.devTouchpoint = getCurrentDevTouchpoint()

return config
}
Expand Down Expand Up @@ -302,20 +274,10 @@ class UIKitContentViewController: UIViewController {
return text
}

private func getCurrentStageTag() -> String? {
guard let text = stageTagField.text, !text.isEmpty else { return nil }

return text
}

private func getCurrentIgnoreCache() -> Bool {
ignoreCacheSwitch.isOn
}

private func getCurrentDevTouchpoint() -> Bool {
devTouchpointSwitch.isOn
}

private func getCurrentClientID() -> String? {
guard let text = clientIDField.text, !text.isEmpty else { return nil }

Expand Down
4 changes: 0 additions & 4 deletions Sources/PayPalMessages/Config/PayPalMessageConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public class PayPalMessageData: NSObject {
public var buyerCountry: String?
/// Skips the caching layer
public var ignoreCache = false
/// Uses the content set that is currently under active development. For development purposes only.
public var devTouchpoint = false
/// Allows the message to pull up a development build of the web modal. For development purposes only.
public var stageTag: String?

/// Standard integration
public init(
Expand Down
6 changes: 0 additions & 6 deletions Sources/PayPalMessages/Config/PayPalMessageModalConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class PayPalMessageModalDataConfig: NSObject {
var placement: PayPalMessagePlacement?
var channel: String?
var ignoreCache: Bool? // swiftlint:disable:this discouraged_optional_boolean
var devTouchpoint: Bool? // swiftlint:disable:this discouraged_optional_boolean
var stageTag: String?
var modalCloseButton: ModalCloseButtonConfig

/// Standard integration
Expand Down Expand Up @@ -122,8 +120,6 @@ class PayPalMessageModalConfig: NSObject, Encodable {
case channel
case placement
case ignoreCache
case devTouchpoint
case stageTag
}

func encode(to encoder: Encoder) throws {
Expand All @@ -138,7 +134,5 @@ class PayPalMessageModalConfig: NSObject, Encodable {
try container.encodeIfPresent(data.channel, forKey: .channel)
try container.encodeIfPresent(data.placement?.rawValue, forKey: .placement)
try container.encodeIfPresent(data.ignoreCache, forKey: .ignoreCache)
try container.encodeIfPresent(data.devTouchpoint, forKey: .devTouchpoint)
try container.encodeIfPresent(data.stageTag, forKey: .stageTag)
}
}
42 changes: 23 additions & 19 deletions Sources/PayPalMessages/Enums/Environment.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Foundation

public enum Environment: Equatable {
case local(port: String = "8443")
case stage(host: String)
case stage(host: String, devTouchpoint: Bool = false, stageTag: String? = nil)
case sandbox
case live

Expand All @@ -14,16 +13,14 @@ public enum Environment: Equatable {
return "sandbox"
case .stage:
return "stage"
case .local:
return "local"
}
}

public var isProduction: Bool {
switch self {
case .live, .sandbox:
return true
case .stage, .local:
case .stage:
return false
}
}
Expand All @@ -32,7 +29,7 @@ public enum Environment: Equatable {
switch self {
case .live, .sandbox:
return URLSession.shared
case .stage, .local:
case .stage:
return URLSession(
configuration: .default,
delegate: DevelopmentSession(),
Expand All @@ -44,9 +41,7 @@ public enum Environment: Equatable {
// swiftlint:disable force_unwrapping
private var baseURL: URL {
switch self {
case .local(let port):
return URL(string: "https://localhost.paypal.com:\(port)")!
case .stage(let host):
case .stage(let host, _, _):
return URL(string: "https://www.\(host)")!
case .sandbox:
return URL(string: "https://www.sandbox.paypal.com")!
Expand All @@ -58,14 +53,12 @@ public enum Environment: Equatable {
// swiftlint:disable force_unwrapping
private var loggerBaseURL: URL {
switch self {
case .stage(let host):
case .stage(let host, _, _):
return URL(string: "https://api.\(host)")!
case .sandbox:
return URL(string: "https://api.sandbox.paypal.com")!
case .live:
return URL(string: "https://api.paypal.com")!
default:
return baseURL
}
}

Expand All @@ -80,24 +73,35 @@ public enum Environment: Equatable {

func url(_ path: PayPalMessagePath, _ queryParams: [String: String?]? = nil) -> URL? {
var parts = URLComponents()
var queryItems: [URLQueryItem]?

if let queryParams, !queryParams.isEmpty {
queryItems = queryParams.map { URLQueryItem(name: $0.key, value: $0.value) }
}
var queryItems = queryParams?.compactMap { key, value in
value != nil ? URLQueryItem(name: key, value: value) : nil
} ?? []

let basePath: URL
if path == .log {
basePath = loggerBaseURL
} else {
basePath = baseURL

// Append dev_touchpoint and stage_tag query parameters only for .stage case
if case .stage(_, let devTouchpoint, let stageTag) = self {
if devTouchpoint {
queryItems.append(URLQueryItem(name: "dev_touchpoint", value: "\(devTouchpoint)"))
}
if let stageTag, !stageTag.isEmpty {
queryItems.append(URLQueryItem(name: "stage_tag", value: stageTag))
}
}
}

parts.scheme = basePath.scheme
parts.host = basePath.host
parts.port = basePath.port
parts.path = path.rawValue
parts.queryItems = queryItems

if !queryItems.isEmpty {
parts.queryItems = queryItems
}


return parts.url
}
Expand Down
4 changes: 0 additions & 4 deletions Sources/PayPalMessages/IO/MessageRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ struct MessageRequestParameters {
let offerType: PayPalMessageOfferType?
let merchantProfileHash: String?
let ignoreCache: Bool
let devTouchpoint: Bool
let stageTag: String?
let instanceID: String
}

Expand Down Expand Up @@ -49,9 +47,7 @@ class MessageRequest: MessageRequestable {
"amount": parameters.amount?.description,
"offer": parameters.offerType?.rawValue,
"merchant_config": parameters.merchantProfileHash,
"stage_tag": parameters.stageTag,
"ignore_cache": parameters.ignoreCache.description,
"dev_touchpoint": parameters.devTouchpoint.description,
"instance_id": parameters.instanceID,
"integration_version": Logger.integrationVersion,
"device_id": Logger.deviceID,
Expand Down
8 changes: 0 additions & 8 deletions Sources/PayPalMessages/PayPalMessageModal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ final class PayPalMessageModal: UIViewController, WKUIDelegate {
@Proxy(\.viewModel.ignoreCache)
var ignoreCache: Bool?

// Development content
@Proxy(\.viewModel.devTouchpoint)
var devTouchpoint: Bool?

// Custom development stage modal bundle
@Proxy(\.viewModel.stageTag)
var stageTag: String?

// Standalone modal
@Proxy(\.viewModel.integrationIdentifier)
var integrationIdentifier: String?
Expand Down
Loading

0 comments on commit d4a9d4a

Please sign in to comment.