Skip to content

Commit

Permalink
PurchaseTesterSwiftUI: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Mar 17, 2023
1 parent d4ecc7b commit 5f6d7a7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public final class ConfiguredPurchases {

if let proxyURL {
Purchases.proxyURL = URL(string: proxyURL)!
} else {
Purchases.proxyURL = nil
}

let purchases = Purchases.configure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ struct PurchaseTesterApp: App {
@State
private var configuration: ConfiguredPurchases?

@StateObject
private var revenueCatCustomerData: RevenueCatCustomerData = .init()

var body: some Scene {
WindowGroup(id: Windows.default.rawValue) {
Group {
if let configuration {
NavigationView {
NavigationSplitView {
ContentView(configuration: configuration)
.environmentObject(self.revenueCatCustomerData)
.toolbar {
ToolbarItem(placement: .principal) {
Button {
Expand All @@ -32,6 +36,20 @@ struct PurchaseTesterApp: App {
}
}
}
} detail: {
DynamicCustomerView(customerInfo: self.$revenueCatCustomerData.customerInfo)
}
.navigationSplitViewStyle(.balanced)
.navigationSplitViewColumnWidth(100)
.task(id: self.configuration?.purchases) {
self.revenueCatCustomerData.customerInfo = nil

if let configuration = self.configuration {
for await customerInfo in configuration.purchases.customerInfoStream {
self.revenueCatCustomerData.customerInfo = customerInfo
self.revenueCatCustomerData.appUserID = configuration.purchases.appUserID
}
}
}
} else {
#if os(macOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct ConfigurationView: View {
} label: {
Text("Continue")
}
.keyboardShortcut(.return)
.disabled(!self.contentIsValid)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,19 @@ struct ContentView: View {

let configuration: ConfiguredPurchases

@StateObject
private var revenueCatCustomerData: RevenueCatCustomerData

@StateObject
private var observerModeManager: ObserverModeManager

init(configuration: ConfiguredPurchases) {
self.configuration = configuration
self._revenueCatCustomerData = .init(wrappedValue: .init())
self._observerModeManager = .init(
wrappedValue: .init(observerModeEnabled: !configuration.purchases.finishTransactions)
)
}

var body: some View {
HomeView()
.environmentObject(self.revenueCatCustomerData)
.environmentObject(self.observerModeManager)
.task(id: self.configuration.purchases) {
for await customerInfo in self.configuration.purchases.customerInfoStream {
self.revenueCatCustomerData.customerInfo = customerInfo
self.revenueCatCustomerData.appUserID = self.configuration.purchases.appUserID
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ import Foundation
import SwiftUI
import RevenueCat

struct CustomerView: View {
struct DynamicCustomerView: View {

let customerInfo: CustomerInfo
@Binding
var customerInfo: CustomerInfo?

var body: some View {
Group {
if let customerInfo = self.customerInfo {
CustomerView(customerInfo: customerInfo)
} else {
Text("No CustomerInfo")
}
}
}

}

struct CustomerView: View {

var customerInfo: CustomerInfo

var body: some View {
TabView {
CustomerInfoView(customerInfo: self.customerInfo)
Expand All @@ -37,6 +54,7 @@ struct CustomerView: View {
}
}
.navigationTitle("Customer Info")
.padding(.top)
}

}
30 changes: 15 additions & 15 deletions Tests/TestingApps/PurchaseTesterSwiftUI/Shared/Views/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct HomeView: View {
CustomerInfoHeaderView() { action in
switch action {
case .login: self.showLogin()
case .logout: await self.logout()
case .logout: await self.logOut()
}
}.padding(.horizontal, 20)

Expand Down Expand Up @@ -84,7 +84,7 @@ struct HomeView: View {
do {
_ = try await Purchases.shared.customerInfo(fetchPolicy: self.cacheFetchPolicy)
} catch {
print("🚀 Info 💁‍♂️ - Error: \(error)")
self.error = error
}
}
} label: {
Expand Down Expand Up @@ -180,7 +180,7 @@ struct HomeView: View {
self.showingAlert = true
}

private func logout() async {
private func logOut() async {
do {
let customerInfo = try await Purchases.shared.logOut()
print("🚀 Info 💁‍♂️ - Customer Info: \(customerInfo)")
Expand All @@ -189,6 +189,7 @@ struct HomeView: View {
self.error = error
}
}

}

private struct CustomerInfoHeaderView: View {
Expand All @@ -206,16 +207,13 @@ private struct CustomerInfoHeaderView: View {
self.completion = completion
}

var customerInfo: RevenueCat.CustomerInfo? {
return self.revenueCatCustomerData.customerInfo
}

var appUserID: String? {
return self.revenueCatCustomerData.appUserID ?? self.customerInfo?.originalAppUserId
return self.revenueCatCustomerData.appUserID
?? self.revenueCatCustomerData.customerInfo?.originalAppUserId
}

var activeEntitlementInfos: [RevenueCat.EntitlementInfo] {
guard let customerInfo = customerInfo else {
guard let customerInfo = self.revenueCatCustomerData.customerInfo else {
return []
}
return Array(customerInfo.entitlements.all.values)
Expand All @@ -228,16 +226,14 @@ private struct CustomerInfoHeaderView: View {
if activeEntitlementInfos.isEmpty {
Text("No active entitlements")
} else {
Text(activeEntitlementInfos.map({$0.identifier}).joined(separator: ", "))
Text(activeEntitlementInfos.map(\.identifier).joined(separator: ", "))
}

HStack {
Spacer()

if let customerInfo = self.customerInfo {
NavigationLink(destination: CustomerView(customerInfo: customerInfo)) {
Text("View Info")
}

NavigationLink(value: self.revenueCatCustomerData.customerInfo) {
Text("View Info")
}

Spacer()
Expand Down Expand Up @@ -284,7 +280,11 @@ private struct CustomerInfoHeaderView: View {

}.padding(.top, 20)
}
.navigationDestination(for: CustomerInfo.self) {
CustomerView(customerInfo: $0)
}
}

}

private struct OfferingItemView: View {
Expand Down

0 comments on commit 5f6d7a7

Please sign in to comment.