Skip to content

Commit

Permalink
Debug: add Offering metadata to debug screen (#3137)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Sep 6, 2023
1 parent d7ebbda commit e5e4f29
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions Sources/Support/DebugUI/DebugContentViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct DebugSwiftUIRootView: View {
case let .offering(offering):
DebugOfferingView(offering: offering)

case let .offeringMetadata(offering):
DebugOfferingMetadataView(offering: offering)

case let .package(package):
DebugPackageView(package: package)

Expand Down Expand Up @@ -75,6 +78,7 @@ struct DebugSwiftUIRootView: View {
private enum DebugViewPath: Hashable {

case offering(Offering)
case offeringMetadata(Offering)
case package(Package)
case paywall(PaywallData)

Expand Down Expand Up @@ -147,7 +151,7 @@ internal struct DebugSummaryView: View {
}
#endif

ShareLink(item: config, preview: .init("Configuration")) {
ShareLink(item: AnyEncodable(config), preview: .init("Configuration")) {
Label("Share", systemImage: "square.and.arrow.up")
}
}
Expand Down Expand Up @@ -236,6 +240,22 @@ private struct DebugOfferingView: View {
Section("Data") {
LabeledContent("Identifier", value: self.offering.id)
LabeledContent("Description", value: self.offering.serverDescription)

if !self.offering.metadata.isEmpty {
NavigationLink(value: DebugViewPath.offeringMetadata(self.offering)) {
Text("Metadata")
}
} else {
LabeledContent("Metadata", value: "{}")
}

if let paywall = self.offering.paywall {
NavigationLink(value: DebugViewPath.paywall(paywall)) {
Text("RevenueCatUI paywall")
}
} else {
LabeledContent("RevenueCatUI", value: "No paywall")
}
}

Section("Packages") {
Expand All @@ -246,14 +266,6 @@ private struct DebugOfferingView: View {
}
}

if let paywall = self.offering.paywall {
Section("RevenueCatUI paywall") {
NavigationLink(value: DebugViewPath.paywall(paywall)) {
Text("View data")
}
}
}

if #available(iOS 17.0, macOS 14.0, tvOS 17.0, *) {
Section("Paywalls") {
Button {
Expand Down Expand Up @@ -321,6 +333,18 @@ private struct DebugOfferingView: View {

}

@available(iOS 16.0, macOS 13.0, *)
private struct DebugOfferingMetadataView: View {

var offering: Offering

var body: some View {
DebugJSONView(value: AnyEncodable(self.offering.metadata))
.navigationTitle("Offering Metadata")
}

}

@available(iOS 16.0, macOS 13.0, *)
private struct DebugPackageView: View {

Expand Down Expand Up @@ -392,54 +416,51 @@ private struct DebugPaywallJSONView: View {

let paywall: PaywallData

var body: some View {
DebugJSONView(value: AnyEncodable(self.paywall))
.navigationTitle("RevenueCatUI Paywall")
}

}

@available(iOS 16.0, macOS 13.0, *)
private struct DebugJSONView<Value: Encodable & Transferable>: View {

let value: Value

var body: some View {
ScrollView(.vertical) {
Text(self.json)
.multilineTextAlignment(.leading)
.font(.caption)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.padding()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.navigationTitle("RevenueCatUI Paywall")
.toolbar {
ToolbarItem(placement: .primaryAction) {
ShareLink(item: self.paywall, preview: .init("Paywall")) {
ShareLink(item: self.value, preview: .init("JSON")) {
Label("Export", systemImage: "square.and.arrow.up")
}
}
}
}

private var json: String {
return (try? self.paywall.prettyPrintedJSON) ?? ""
return (try? self.value.prettyPrintedJSON) ?? "{}"
}

}

// MARK: - Transferable

@available(iOS 16.0, macOS 13.0, *)
extension DebugViewModel.Configuration: Transferable {
extension AnyEncodable: Transferable {

static var transferRepresentation: some TransferRepresentation {
return CodableRepresentation(
for: DebugViewModel.Configuration.self,
contentType: .plainText,
encoder: JSONEncoder.prettyPrinted,
decoder: JSONDecoder.default
)
}

}

@available(iOS 16.0, macOS 13.0, *)
extension PaywallData: Transferable {

// swiftlint:disable:next missing_docs
public static var transferRepresentation: some TransferRepresentation {
return CodableRepresentation(
for: PaywallData.self,
for: Self.self,
contentType: .plainText,
encoder: JSONEncoder.prettyPrinted,
decoder: JSONDecoder.default
Expand Down

0 comments on commit e5e4f29

Please sign in to comment.