Allow users to buy Bubble+

This commit is contained in:
lumaa-dev 2024-09-23 18:30:20 +02:00
parent f64d85204c
commit 049e6b9666

View File

@ -6,13 +6,15 @@ import RevenueCat
public struct ShopView: View { public struct ShopView: View {
@Environment(AppDelegate.self) private var delegate: AppDelegate @Environment(AppDelegate.self) private var delegate: AppDelegate
@Environment(\.dismiss) private var dismiss @Environment(\.openURL) private var openURL: OpenURLAction
@Environment(\.dismiss) private var dismiss: DismissAction
@State private var showSub: Bool = false @State private var showSub: Bool = false
@State private var purchaseError: Bool = false @State private var purchaseError: Bool = false
@State private var hasSub: Bool = false
private var canPay: Bool { private var canPay: Bool {
#if targetEnvironment(simulator) #if targetEnvironment(simulator) || !DEBUG
return true return true
#else #else
return false return false
@ -38,44 +40,88 @@ public struct ShopView: View {
Spacer() Spacer()
VStack(spacing: 20) { if !self.hasSub {
Button { VStack(spacing: 20) {
showSub.toggle() Button {
} label: { showSub.toggle()
Text("shop.bubble-plus.subscription") } label: {
} Text("shop.bubble-plus.subscription")
.buttonStyle(LargeButton(filled: true, disabled: !canPay)) }
.overlay(alignment: .topTrailing) { .buttonStyle(LargeButton(filled: true, disabled: !canPay))
Text("shop.best") .overlay(alignment: .topTrailing) {
.foregroundStyle(Color.white) Text("shop.best")
.font(.title2.bold()) .foregroundStyle(Color.white)
.lineLimit(1) .font(.title2.bold())
.minimumScaleFactor(0.1) .lineLimit(1)
.padding(4.5) .minimumScaleFactor(0.1)
.background(Capsule().fill(Color.red)) .padding(4.5)
.offset(x: 20.0, y: -25.0) .background(Capsule().fill(Color.red))
.rotationEffect(.degrees(25.0)) .offset(x: 20.0, y: -25.0)
} .rotationEffect(.degrees(25.0))
.disabled(!canPay) }
.disabled(!canPay)
Button { Button {
// showLifetime.toggle() // showLifetime.toggle()
purchase(entitlement: .lifetime) purchase(entitlement: .lifetime)
} label: { } label: {
Text("shop.bubble-plus.lifetime") Text("shop.bubble-plus.lifetime")
} }
.buttonStyle(LargeButton(filled: false, disabled: !canPay)) .buttonStyle(LargeButton(filled: false, disabled: !canPay))
.disabled(!canPay) .disabled(!canPay)
Button { Button {
dismiss() dismiss()
} label: { } label: {
Text("shop.bubble-plus.dismiss") Text("shop.bubble-plus.dismiss")
}
.buttonStyle(.borderless)
.padding(.top, 50)
}
.padding(.vertical)
} else {
VStack {
Button {
Task {
#if !targetEnvironment(simulator)
if let windowScene = self.delegate.window?.windowScene {
print("accessing subs natively")
try? await AppStore.showManageSubscriptions(in: windowScene)
} else {
print("accessing subs via deeplink")
openURL(URL(string: "itms-apps://apps.apple.com/account/subscriptions")!)
}
#else
print("ACCESS SUBS but Simulator can't")
#endif
}
} label: {
VStack {
Text("shop.bubble-plus.owning")
.font(.title2.bold())
.foregroundStyle(Color(uiColor: UIColor.label))
Text("shop.bubble-plus.manage")
.font(.callout)
.foregroundStyle(Color.blue)
}
.padding(.vertical)
}
Button {
dismiss()
} label: {
Text("shop.bubble-plus.dismiss")
}
.buttonStyle(.borderless)
.padding(.top, 50)
} }
.buttonStyle(.borderless)
.padding(.top, 50)
} }
.padding(.vertical) }
.task {
AppDelegate.hasPlus { subscribed in
self.hasSub = subscribed
}
} }
.frame(width: delegate.windowWidth) .frame(width: delegate.windowWidth)
.background(Color.appBackground) .background(Color.appBackground)
@ -99,7 +145,7 @@ public struct ShopView: View {
feature(.drafts) feature(.drafts)
feature(.analytics) // feature(.analytics)
feature(.contentFilter) feature(.contentFilter)
@ -392,6 +438,7 @@ extension ShopView {
// .environment(\.locale, Locale(identifier: "en-us")) // .environment(\.locale, Locale(identifier: "en-us"))
} }
// MARK: - Entitlements
enum PlusEntitlements: String { enum PlusEntitlements: String {
case monthly case monthly
case yearly case yearly