Skip to content

Commit

Permalink
Paywalls: added OfferingsList to preview all paywalls (#2916)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Jul 31, 2023
1 parent 4961262 commit af23838
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
4F4EE7CF2A572F5D00D7EAE1 /* DebugView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FC046C32A572E3700A28BCF /* DebugView.swift */; };
4F4EE7D22A5731E800D7EAE1 /* RevenueCat in Frameworks */ = {isa = PBXBuildFile; productRef = 4F4EE7D12A5731E800D7EAE1 /* RevenueCat */; };
4F6E5A502A660DD500C573C7 /* SampleChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F6E5A4F2A660DD500C573C7 /* SampleChart.swift */; };
4FC6F8B22A7403D3002139B2 /* OfferingsList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FC6F8B12A7403D3002139B2 /* OfferingsList.swift */; };
4FC80B202A5DE8E300E95DB0 /* RevenueCatUI in Frameworks */ = {isa = PBXBuildFile; productRef = 4FC80B1F2A5DE8E300E95DB0 /* RevenueCatUI */; };
4FCA01FB2A3A1CBD00B262C0 /* StoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4FCA01FA2A3A1CBD00B262C0 /* StoreKit.framework */; };
4FDF11202A7270F3004F3680 /* SamplePaywallsList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FDF111F2A7270F3004F3680 /* SamplePaywallsList.swift */; };
Expand Down Expand Up @@ -48,6 +49,7 @@
4FC046BF2A572E3700A28BCF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4FC046C12A572E3700A28BCF /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
4FC046C32A572E3700A28BCF /* DebugView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugView.swift; sourceTree = "<group>"; };
4FC6F8B12A7403D3002139B2 /* OfferingsList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OfferingsList.swift; sourceTree = "<group>"; };
4FC882E02A5870C6005BE85E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
4FCA01FA2A3A1CBD00B262C0 /* StoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = StoreKit.framework; path = System/Library/Frameworks/StoreKit.framework; sourceTree = SDKROOT; };
4FDF111F2A7270F3004F3680 /* SamplePaywallsList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SamplePaywallsList.swift; sourceTree = "<group>"; };
Expand All @@ -73,6 +75,7 @@
children = (
4F34FF622A60AD9A00AADF11 /* AppContentView.swift */,
4FC046C32A572E3700A28BCF /* DebugView.swift */,
4FC6F8B12A7403D3002139B2 /* OfferingsList.swift */,
4F6E5A4F2A660DD500C573C7 /* SampleChart.swift */,
4FDF111F2A7270F3004F3680 /* SamplePaywallsList.swift */,
);
Expand Down Expand Up @@ -219,6 +222,7 @@
4F6E5A502A660DD500C573C7 /* SampleChart.swift in Sources */,
4FDF11222A72714C004F3680 /* SamplePaywalls.swift in Sources */,
4F34FF652A60ADBD00AADF11 /* Configuration.swift in Sources */,
4FC6F8B22A7403D3002139B2 /* OfferingsList.swift in Sources */,
4FDF11202A7270F3004F3680 /* SamplePaywallsList.swift in Sources */,
4F34FF632A60AD9A00AADF11 /* AppContentView.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ struct AppContentView: View {
.buttonStyle(.borderedProminent)
.tint(.mint)

NavigationLink {
OfferingsList()
} label: {
Text("All offerings")
}
.buttonStyle(.borderedProminent)
.tint(.indigo)

Spacer()

BarChartView(data: (0..<10).map { _ in Double.random(in: 0..<100)})
Expand Down
116 changes: 116 additions & 0 deletions Tests/TestingApps/SimpleApp/SimpleApp/Views/OfferingsList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// SamplePaywallsList.swift
// SimpleApp
//
// Created by Nacho Soto on 7/27/23.
//

import RevenueCat
import RevenueCatUI
import SwiftUI

struct OfferingsList: View {

@State
private var offerings: Result<Offerings, NSError>?

@State
private var selectedOffering: Offering?

var body: some View {
self.content
.navigationTitle("Offerings")
.task {
do {
self.offerings = .success(try await Purchases.shared.offerings())
} catch let error as NSError {
self.offerings = .failure(error)
}
}
}

@ViewBuilder
private var content: some View {
switch self.offerings {
case let .success(offerings):
self.list(with: offerings.all.values)
.sheet(item: self.$selectedOffering) { offering in
PaywallView(offering: offering)
}

case let .failure(error):
Text(error.description)

case .none:
ProgressView()
}
}

@ViewBuilder
private func list(with offerings: some Collection<Offering>) -> some View {
List {
let offeringsWithPaywall = Self.offeringsWithPaywall(from: offerings)

Section {
ForEach(offeringsWithPaywall, id: \.offering.id) { offering, paywall in
Button {
self.selectedOffering = offering
} label: {
VStack(alignment: .leading) {
Text(offering.serverDescription)
Text(verbatim: "Template: \(paywall.template.name)")
}
}
.buttonStyle(.plain)
.contentShape(Rectangle())
}
} header: {
Text(verbatim: "With paywall")
} footer: {
if offeringsWithPaywall.isEmpty {
Text(verbatim: "No offerings with paywall")
}
}

Section("Without paywall") {
ForEach(Self.offeringsWithNoPaywall(from: offerings), id: \.id) { offering in
Text(offering.serverDescription)
}
}
}
}

}

private extension OfferingsList {

static func offeringsWithPaywall(
from offerings: some Collection<Offering>
) -> [(offering: Offering, paywall: PaywallData)] {
return offerings
.compactMap { offering in
if let paywall = offering.paywall {
return (offering, paywall)
} else {
return nil
}
}
}

static func offeringsWithNoPaywall(from offerings: some Collection<Offering>) -> [Offering] {
return offerings.filter { $0.paywall == nil }
}

}

#if DEBUG

struct OfferingsList_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
SamplePaywallsList()
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension PaywallTemplate: Identifiable {

}

private extension PaywallTemplate {
extension PaywallTemplate {

var name: String {
switch self {
Expand Down

0 comments on commit af23838

Please sign in to comment.