iap work
This commit is contained in:
@@ -9,12 +9,6 @@ import SwiftUI
|
||||
import StoreKit
|
||||
|
||||
struct PurchaseButtonView: View {
|
||||
enum ViewStatus: String {
|
||||
case needToBuy
|
||||
case error
|
||||
case success
|
||||
case subscribed
|
||||
}
|
||||
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
|
||||
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
|
||||
@AppStorage(UserDefaultsStore.Keys.firstLaunchDate.rawValue, store: GroupUserDefaults.groupDefaults) private var firstLaunchDate = Date()
|
||||
@@ -24,15 +18,7 @@ struct PurchaseButtonView: View {
|
||||
private let height: Float
|
||||
private let showCountdownTimer: Bool
|
||||
private let showManageSubClosure: (() -> Void)?
|
||||
|
||||
@State var isPurchasing: Bool = false
|
||||
|
||||
@State private var viewStatus: ViewStatus = .needToBuy {
|
||||
didSet {
|
||||
isPurchasing = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public init(height: Float, iapManager: IAPManager, showManageSubClosure: (() -> Void)? = nil, showCountdownTimer: Bool = false) {
|
||||
self.height = height
|
||||
self.showManageSubClosure = showManageSubClosure
|
||||
@@ -42,24 +28,15 @@ struct PurchaseButtonView: View {
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
switch viewStatus {
|
||||
case .needToBuy, .error:
|
||||
switch iapManager.showIAP {
|
||||
case true:
|
||||
buyOptionsView
|
||||
.frame(height: CGFloat(height))
|
||||
.background(theme.currentTheme.secondaryBGColor)
|
||||
case .success:
|
||||
case false:
|
||||
subscribedView
|
||||
.frame(height: CGFloat(height))
|
||||
.background(theme.currentTheme.secondaryBGColor)
|
||||
case .subscribed:
|
||||
subscribedView
|
||||
.frame(height: CGFloat(height))
|
||||
.background(theme.currentTheme.secondaryBGColor)
|
||||
}
|
||||
}
|
||||
.onAppear{
|
||||
if let _ = iapManager.currentSubscription {
|
||||
viewStatus = .subscribed
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,21 +150,7 @@ struct PurchaseButtonView: View {
|
||||
.frame(minWidth: 0, maxWidth: .infinity)
|
||||
.background(.clear)
|
||||
}
|
||||
|
||||
private var successView: some View {
|
||||
HStack {
|
||||
Text("it worked")
|
||||
}
|
||||
.background(.green)
|
||||
}
|
||||
|
||||
private var errorView: some View {
|
||||
HStack {
|
||||
Text("something broke")
|
||||
}
|
||||
.background(.red)
|
||||
}
|
||||
|
||||
|
||||
private var subscribedView: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Text(String(localized: "purchase_view_current_subscription"))
|
||||
@@ -253,23 +216,9 @@ struct PurchaseButtonView: View {
|
||||
.padding([.leading, .trailing])
|
||||
}
|
||||
|
||||
private var purchasingView: some View {
|
||||
HStack {
|
||||
Text("purcasing")
|
||||
}
|
||||
.background(.yellow)
|
||||
}
|
||||
|
||||
private func purchase(product: Product) {
|
||||
isPurchasing = true
|
||||
Task {
|
||||
do {
|
||||
if let _ = try await iapManager.purchase(product) {
|
||||
viewStatus = .success
|
||||
}
|
||||
} catch {
|
||||
viewStatus = .error
|
||||
}
|
||||
try await iapManager.purchase(product)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user