-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
![image](https://github.com/RevenueCat/purchases-ios/assets/685609/86e1f203-8d5e-47d4-9069-a7bdd91f0b5a)
- Loading branch information
Showing
4 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
Tests/TestingApps/SimpleApp/SimpleApp/Views/OfferingsList.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: PreviewProvid |