Skip to content

Commit

Permalink
PurchaseTesterSwiftUI: fixed warnings and simplified code using `as…
Browse files Browse the repository at this point in the history
…ync` methods (#1985)

Fixing warnings before working on a few improvements
  • Loading branch information
NachoSoto authored Oct 26, 2022
1 parent 42d7ad6 commit d146112
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,18 @@ class RevenueCatCustomerData: ObservableObject {

}

extension RevenueCat.StoreProduct: Identifiable {}
extension RevenueCat.StoreProduct: Identifiable {

public var id: String {
return self.productIdentifier
}

}

extension RevenueCat.NonSubscriptionTransaction: Identifiable {

public var id: String {
return self.productIdentifier
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import RevenueCat
struct TransactionsView: View {
let customerInfo: RevenueCat.CustomerInfo

let dateFormatter: DateFormatter = {
var df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
return df
}()

var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 10) {
ForEach(self.customerInfo.nonSubscriptionTransactions) { transaction in
ForEach(self.customerInfo.nonSubscriptions) { transaction in
VStack(alignment: .leading, spacing: 0) {
Text("\(transaction.productIdentifier)")
.bold()
Text("\(dateFormatter.string(from: transaction.purchaseDate))")
Text("\(Self.dateFormatter.string(from: transaction.purchaseDate))")
}
}
}
}.padding(.horizontal, 20)
}

private static let dateFormatter: DateFormatter = {
var df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
return df
}()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct HomeView: View {
CustomerInfoHeaderView() { action in
switch action {
case .login: self.showLogin()
case .logout: self.logout()
case .logout: await self.logout()
}
}.padding(.horizontal, 20)

Expand Down Expand Up @@ -83,7 +83,7 @@ struct HomeView: View {
Button {
Task<Void, Never> {
do {
try await Purchases.shared.beginRefundRequestForActiveEntitlement()
_ = try await Purchases.shared.beginRefundRequestForActiveEntitlement()
} catch {
print("🚀 Info 💁‍♂️ - Error: \(error)")
}
Expand All @@ -105,7 +105,7 @@ struct HomeView: View {
return
}

Task {
Task<Void, Never> {
do {
let (customerInfo, created) = try await Purchases.shared.logIn(self.newAppUserID)
print("🚀 Info 💁‍♂️ - Customer Info: \(customerInfo)")
Expand Down Expand Up @@ -133,18 +133,20 @@ struct HomeView: View {
self.showingAlert = true
}

private func logout() {
Purchases.shared.logOut { customerInfo, error in
private func logout() async {
do {
let customerInfo = try await Purchases.shared.logOut()
print("🚀 Info 💁‍♂️ - Customer Info: \(customerInfo)")
print("🚀 Info 💁‍♂️ - Error: \(error)")
} catch {
print("🚀 Failed logging out 💁‍♂️ - Error: \(error)")
}
}
}

private struct CustomerInfoHeaderView: View {
@EnvironmentObject var revenueCatCustomerData: RevenueCatCustomerData

typealias Completion = (Action) -> ()
typealias Completion = (Action) async -> ()
enum Action {
case login, logout
}
Expand Down Expand Up @@ -194,13 +196,17 @@ private struct CustomerInfoHeaderView: View {
Spacer()
if Purchases.shared.isAnonymous {
Button {
self.completion(.login)
Task<Void, Never> {
await self.completion(.login)
}
} label: {
Text("Login")
}
} else {
Button {
self.completion(.logout)
Task<Void, Never> {
await self.completion(.logout)
}
} label: {
Text("Logout")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ struct OfferingDetailView: View {
Text("**Pkg Id:** \(package.identifier)")
Text("**Sub Group:** \(package.storeProduct.subscriptionGroupIdentifier ?? "-")")
Text("**Package type:** \(package.display)")




if let period = package.storeProduct.sk1Product?.subscriptionPeriod?.unit.rawValue {
Text("**Sub Period:** \(period)")
} else {
Expand All @@ -75,7 +73,9 @@ struct OfferingDetailView: View {
.foregroundColor(.blue)
.padding(.vertical, 10)
.onTapGesture {
purchaseAsPackage()
Task<Void, Never> {
await self.purchaseAsPackage()
}
}

Divider()
Expand All @@ -84,7 +84,9 @@ struct OfferingDetailView: View {
.foregroundColor(.blue)
.padding(.vertical, 10)
.onTapGesture {
purchaseAsProduct()
Task<Void, Never> {
await self.purchaseAsProduct()
}
}

Divider()
Expand All @@ -101,22 +103,28 @@ struct OfferingDetailView: View {
}
}

private func purchaseAsPackage() {
Purchases.shared.purchase(package: self.package) { transaction, info, error, userCancelled in
print("🚀 Info 💁‍♂️ - Transactions: \(transaction)")
print("🚀 Info 💁‍♂️ - Info: \(info)")
print("🚀 Info 💁‍♂️ - Error: \(error)")
print("🚀 Info 💁‍♂️ - User Cancelled: \(userCancelled)")
private func purchaseAsPackage() async {
do {
let result = try await Purchases.shared.purchase(package: self.package)
self.completedPurchase(result)
} catch {
print("🚀 Failed purchase: 💁‍♂️ - Error: \(error)")
}
}

private func purchaseAsProduct() {
Purchases.shared.purchase(product: self.package.storeProduct) { transaction, info, error, userCancelled in
print("🚀 Info 💁‍♂️ - Transactions: \(transaction)")
print("🚀 Info 💁‍♂️ - Info: \(info)")
print("🚀 Info 💁‍♂️ - Error: \(error)")
print("🚀 Info 💁‍♂️ - User Cancelled: \(userCancelled)")
private func purchaseAsProduct() async {
do {
let result = try await Purchases.shared.purchase(product: self.package.storeProduct)
self.completedPurchase(result)
} catch {
print("🚀 Purchase failed: 💁‍♂️ - Error: \(error)")
}
}

private func completedPurchase(_ data: PurchaseResultData) {
print("🚀 Info 💁‍♂️ - Transaction: \(data.transaction?.description ?? "")")
print("🚀 Info 💁‍♂️ - Info: \(data.customerInfo)")
print("🚀 Info 💁‍♂️ - User Cancelled: \(data.userCancelled)")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ struct PromoOfferDetailsView: View {
.foregroundColor(.blue)
.padding(.vertical, 10)
.onTapGesture {
purchasePromo(promotionalOffer: promotionalOffer)
Task<Void, Never> {
await self.purchasePromo(promotionalOffer: promotionalOffer)
}
}
}
}
Expand All @@ -107,12 +109,15 @@ struct PromoOfferDetailsView: View {
}
}

func purchasePromo(promotionalOffer: PromotionalOffer) {
Purchases.shared.purchase(package: self.package, promotionalOffer: promotionalOffer) { transaction, info, error, userCancelled in
print("🚀 Info 💁‍♂️ - Transactions: \(transaction)")
print("🚀 Info 💁‍♂️ - Info: \(info)")
print("🚀 Info 💁‍♂️ - Error: \(error)")
print("🚀 Info 💁‍♂️ - User Cancelled: \(userCancelled)")
private func purchasePromo(promotionalOffer: PromotionalOffer) async {
do {
let result = try await Purchases.shared.purchase(package: self.package, promotionalOffer: promotionalOffer)

print("🚀 Info 💁‍♂️ - Transactions: \(result.transaction?.description ?? "")")
print("🚀 Info 💁‍♂️ - Info: \(result.customerInfo)")
print("🚀 Info 💁‍♂️ - User Cancelled: \(result.userCancelled)")
} catch {
print("🚀 Purchase failed: 💁‍♂️ - Error: \(error)")
}
}
}

0 comments on commit d146112

Please sign in to comment.