Allow users to buy Bubble+

This commit is contained in:
lumaa-dev 2024-09-23 18:30:20 +02:00
parent f64d85204c
commit 049e6b9666
1 changed files with 92 additions and 45 deletions

View File

@ -6,19 +6,21 @@ 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
#endif #endif
} }
public var body: some View { public var body: some View {
VStack { VStack {
Image("HeroPlus") Image("HeroPlus")
@ -26,56 +28,100 @@ public struct ShopView: View {
.scaledToFit() .scaledToFit()
.frame(width: 100, height: 100) .frame(width: 100, height: 100)
.padding(.vertical) .padding(.vertical)
if canPay { if canPay {
features features
.padding(.bottom) .padding(.bottom)
} else { } else {
Spacer() Spacer()
ComingSoonView() ComingSoonView()
} }
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) {
Text("shop.best")
.foregroundStyle(Color.white)
.font(.title2.bold())
.lineLimit(1)
.minimumScaleFactor(0.1)
.padding(4.5)
.background(Capsule().fill(Color.red))
.offset(x: 20.0, y: -25.0)
.rotationEffect(.degrees(25.0))
}
.disabled(!canPay)
Button {
// showLifetime.toggle()
purchase(entitlement: .lifetime)
} label: {
Text("shop.bubble-plus.lifetime")
}
.buttonStyle(LargeButton(filled: false, disabled: !canPay))
.disabled(!canPay)
Button {
dismiss()
} label: {
Text("shop.bubble-plus.dismiss")
}
.buttonStyle(.borderless)
.padding(.top, 50)
} }
.buttonStyle(LargeButton(filled: true, disabled: !canPay)) .padding(.vertical)
.overlay(alignment: .topTrailing) { } else {
Text("shop.best") VStack {
.foregroundStyle(Color.white) Button {
.font(.title2.bold()) Task {
.lineLimit(1) #if !targetEnvironment(simulator)
.minimumScaleFactor(0.1) if let windowScene = self.delegate.window?.windowScene {
.padding(4.5) print("accessing subs natively")
.background(Capsule().fill(Color.red)) try? await AppStore.showManageSubscriptions(in: windowScene)
.offset(x: 20.0, y: -25.0) } else {
.rotationEffect(.degrees(25.0)) 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)
} }
.disabled(!canPay)
Button {
// showLifetime.toggle()
purchase(entitlement: .lifetime)
} label: {
Text("shop.bubble-plus.lifetime")
}
.buttonStyle(LargeButton(filled: false, disabled: !canPay))
.disabled(!canPay)
Button {
dismiss()
} label: {
Text("shop.bubble-plus.dismiss")
}
.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